Avoid defining classes in this assignment. Your program source file name should be othello. Insert your name, student ID, and e-mail as comments at the beginning of your source file 3. Your program output should be exactly the same as the sample program same text, symbols, letter case, spacings, etc. Your program should be free of compilation errors and warnings 5.
Your program should include suitable comments as documentation. Do NOT plagiarize. Come on, the assignment is not that hard. Since the board array is a local variable, your functions should pass the board array as a parameter for more details please go to lecture 8, P. Knowing more ways to program makes you stronger P. Open navigation menu. Close suggestions Search Search. User Settings. Skip carousel. Carousel Previous. Carousel Next.
What is Scribd? Tutorial Uploaded by Wu Chun Ricardo. Did you find this document useful? Is this content inappropriate? Report this Document. Flag for inappropriate content. Download now. Save Save Tutorial 07 For Later. Related titles. Carousel Previous Carousel Next. Jump to Page. Search inside document. Player X's turn: E3 P. A1, a1 are both valid and the same position P. You can do both lower and upper cases, or convert the lowercase to uppercase P.
After the player inputs the position, we need to check if the position is valid or not P. After the player inputs the position, we need to check if the position is valid or not Three types of invalid position: 1. Come on, the assignment is not that hard P. Documents Similar To Tutorial Maria Andreea. Ali Marco-nan. Lafangey Parindey. Saif Abid. Sites Procergs. Hui Han. Ray Al. Daemon Salvatore. Pramod Kumar Mandal. Obaid Awan. Derek Boomy Price. Utkarsh Kashyap. Dinesh Lasantha. Hrishav Shome.
An Ish. Operational Research Notes 2 - TutorialsDuniya. Senthil Lakshmi. Marvin Pineda. A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. For example, function strcat to concatenate two strings, function memcpy to copy one memory location to another location, and many more functions.
A function is known with various names like a method or a sub-routine or a procedure etc. Some functions perform the desired operations without returning a value.
The function name and the parameter list together constitute the function signature. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters. Example: Following is the source code for a function called max. The actual body of the function can be defined separately.
In such case, you should declare the function at the top of the file calling the function. To use a function, you will have to call or invoke that function. When a program calls a function, program control is transferred to the called function. To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you can store returned value.
While running final executable, it would produce the following result: Max value is : Function Arguments If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function.
The formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit. While calling a function, there are two ways that arguments can be passed to a function: Call Type Description Call by value This method copies the actual value of an argument into the formal parameter of the function.
In this case, changes made to the parameter inside the function have no effect on the argument. Call by pointer This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call.
This means that changes made to the parameter affect the argument. Call by reference This method copies the reference of an argument into the formal parameter. Call by Value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In general, this means that code within a function cannot alter the arguments used to call the function. Consider the function swap definition as follows. Call by Pointer The call by pointer method of passing arguments to a function copies the address of an argument into the formal parameter.
This means that changes made to the parameter affect the passed argument. To pass the value by pointer, argument pointers are passed to the functions just like any other value. So accordingly you need to declare the function parameters as pointer types as in the following function swap , which exchanges the values of the two integer variables pointed to by its arguments. Inside the function, the reference is used to access the actual argument used in the call.
To pass the value by reference, argument reference is passed to the functions just like any other value. So accordingly you need to declare the function parameters as reference types as in the following function swap , which exchanges the values of the two integer variables pointed to by its arguments.
In general, this means that code within a function cannot alter the arguments used to call the function and above mentioned example while calling max function used the same method. Default Values for Parameters When you define a function, you can specify a default value for each of the last parameters.
This value will be used if the corresponding argument is left blank when calling to the function. If a value for that parameter is not passed when the function is called, the default given value is used, but if a value is specified, this default value is ignored and the passed value is used instead.
These are functions that can be included in your program and then use. There are actually two functions you will need to know about random number generation. The first is rand , this function will only return a pseudo random number. The way to fix this is to first call the srand function.
Following is a simple example to generate few random numbers. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Following is an example to assign a single element of the array: If you omit the size of the array, an array just big enough to hold the initialization is created.
Array with 4th index will be 5th, i. Following is the pictorial representation of the same array we discussed above: Accessing Array Elements An element is accessed by indexing the array name.
This is done by placing the index of the element within square brackets after the name of the array. Following is an example, which will use all the above- mentioned three concepts viz. The simplest form of the multidimensional array is the two-dimensional array. Pointer to an array You can generate a pointer to the first element of an array by simply specifying the array name, without any index.
Here is the general form of a multidimensional array declaration: type name[size1][size2] A two-dimensional array is, in essence, a list of one-dimensional arrays. A two-dimensional array can be think as a table, which will have x number of rows and y number of columns. A 2-dimensional array a, which contains three rows and four columns can be shown as below: Thus, every element in array a is identified by an element name of the form a[ i ][ j ], where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a.
Following is an array with 3 rows and each row have 4 columns. You can verify it in the above digram. However, You can pass a pointer to an array by specifying the array's name without an index. If you want to pass a single-dimension array as an argument in a function, you would have to declare function formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received.
However, you can return a pointer to an array by specifying the array's name without an index. Thus a null-terminated string contains the characters that comprise the string followed by a null.
The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello. A pointer is a variable whose value is the address of another variable. Like any variable or constant, you must declare a pointer before you can work with it. The asterisk you used to declare a pointer is the same asterisk that you use for multiplication.
However, in this statement the asterisk is being used to designate a variable as a pointer. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.
Passing pointers to functions Passing an argument by reference or by address both enable the passed argument to be changed in the calling function by the called function. Null Pointers It is always a good practice to assign the pointer NULL to a pointer variable in case you do not have exact address to be assigned.
This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer. The NULL pointer is a constant with a value of zero defined in several standard libraries, including iostream. However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. Many times, uninitialized variables hold some junk values and it becomes difficult to debug the program. Pointer Arithmetic As you understood pointer is an address which is a numeric value; therefore, you can perform arithmetic operations on a pointer just as you can a numeric value.
This operation will move the pointer to next memory location without impacting actual value at the memory location. If ptr points to a character whose address is , then above operation will point to the location because next character will be available at Incrementing a Pointer We prefer using a pointer in our program instead of an array because the variable pointer can be incremented, unlike the array name which cannot be incremented because it is a constant pointer.
If p1 and p2 point to variables that are related to each other, such as elements of the same array, then p1 and p2 can be meaningfully compared. In fact, pointers and arrays are interchangeable in many cases. For example, a pointer that points to the beginning of an array can access that array by using either pointer arithmetic or array-style indexing.
The reason for this is that var is a constant that points to the beginning of an array and can not be used as l-value. Because an array name generates a pointer constant, it can still be used in pointer-style expressions, as long as it is not modified. Thus, each element in ptr, now holds a pointer to an int value. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below.
A variable that is a pointer to a pointer must be declared as such. This is done by placing an additional asterisk in front of its name. To do so, simply declare the function parameter as a pointer type. Now, consider the following function, which will generate 10 random numbers and return them using an array name which represents a pointer i.
Once a reference is initialized with a variable, either the variable name or the reference name may be used to refer to the variable. You must always be able to assume that a reference is connected to a legitimate piece of storage. Pointers can be pointed to another object at any time. Pointers can be initialized at any time. You can then think of a reference as a second label attached to that memory location. Therefore, you can access the contents of the variable through either the original variable name or the reference.
Thus, read the first declaration as "r is an integer reference initialized to i" and read the second declaration as "s is a double reference initialized to d. References as Parameters We have discussed how we implement call by reference concept using pointers.
When a function returns a reference, it returns an implicit pointer to its return value. This way, a function can be used on the left side of an assignment statement. So it is not legal to return a reference to local var. But you can always return a reference on a static variable.
If the system has no time,. A value of. This structure holds the date and time in the form of a C structure as mentioned above. Most of the time related functions makes use of tm structure. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. We will discuss about it in detail in File and Stream related chapter. The Standard Output Stream cout The predefined object cout is an instance of ostream class. The cout object is said to be "connected to" the standard output device, which usually is the display screen.
The Standard Input Stream cin The predefined object cin is an instance of istream class. The cin object is said to be attached to the standard input device, which usually is the keyboard.
The cerr object is said to be attached to the standard error device, which is also a display screen but the object cerr is un-buffered and each stream insertion to cerr causes its output to appear immediately. The cerr is also used in conjunction with the stream insertion operator as shown in the following example. The Standard Log Stream clog The predefined object clog is an instance of ostream class.
The clog object is said to be attached to the standard error device, which is also a display screen but the object clog is buffered. This means that each insertion to clog could cause its output to be held in a buffer until the buffer is filled or until the buffer is flushed.
The clog is also used in conjunction with the stream insertion operator as shown in the following example. You would not be able to see any difference in cout, cerr and clog with these small examples, but while writing and executing big programs the difference becomes obvious. So it is good practice to display error messages using cerr stream and while displaying other log messages then clog should be used.
Structures are used to represent a record, suppose you want to keep track of your books in a library. The struct statement defines a new data type, with more than one member, for your program.
At the end of the structure's definition, before the final semicolon, you can specify one or more structure variables but it is optional. The member access operator is coded as a period between the structure variable name and the structure member that we wish to access.
You would use struct keyword to define variables of structure type. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.
A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. A public member can be accessed from outside the class anywhere within the scope of the class object.
You can also specify the members of a class as private or protected which we will discuss in a sub-section. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Accessing the Data Members The public data members of objects of a class can be accessed using the direct member access operator. We will learn how private and protected members can be accessed.
Class access modifiers A class member can be defined as public, private or protected. By default members would be assumed as private. A destructor is also a special function which is called when created object is deleted. In fact a class is really just a structure with functions in it. Static members of a class Both data members and function members of a class can be declared as static. Class member functions A member function of a class is a function that has its definition or its prototype within the class definition like any other variable.
It operates on any object of the class of which it is a member, and has access to all the members of a class for that object.
A member function will be called using a dot operator. The access restriction to the class members is specified by the labeled public, private, and protected sections within the class body. The keywords public, private, and protected are called access specifiers. A class can have multiple public, protected, or private labeled sections. Each section remains in effect until either another section label or the closing right brace of the class body is seen.
The default access for members and classes is private. Only the class and friend functions can access private members. For now you can check following example where I have derived one child class SmallBox from a parent class Box. Following example is similar to above example and here width member will be accessible by any member function of its derived class SmallBox.
A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables. Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc.
If a copy constructor is not defined in a class, the compiler itself defines one. If the class has pointer variables and has some dynamic memory allocations, then it is a must to have a copy constructor. Length of line : 10 Freeing memory! Freeing memory! Copy constructor allocating ptr. Friend Functions A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.
A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time.
Any change to an inline function could require all clients of the function to be recompiled because compiler would need to replace all the code once again otherwise it will continue with old functionality. To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function.
The compiler can ignore the inline qualifier in case defined function is more than a line. A function definition in a class definition is an inline function definition, even without the use of the inline specifier. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object.
Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a this pointer.
Also as with all pointers, you must initialize the pointer before using it. Constructor called. Volume of Box1: 5. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. We can't put it in the class definition but it can be initialized outside the class as done in the following example by redeclaring the static variable, using the scope resolution operator :: to identify which class it belongs to.
Total objects: 2 Static Function Members By declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator A static member function can only access static data member, other static member functions and any other functions from outside the class.
Static member functions have a class scope and they do not have access to the this pointer of the class. You could use a static member function to determine whether some objects of the class have been created or not. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application.
This also provides an opportunity to reuse the code functionality and fast implementation time. When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class.
This existing class is called the base class, and the new class is referred to as the derived class. The idea of inheritance implements the is a relationship. To define a derived class, we use a class derivation list to specify the base class es.
A class derivation list names one or more base classes and has the form: class derived-class: access-specifier base-class Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.
Type of Inheritance When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. The type of inheritance is specified by the access-specifier as explained above. We hardly use protected or private inheritance, but public inheritance is commonly used. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
Where access is one of public, protected, or private and would be given for every base class and they will be separated by comma as shown above.
An overloaded declaration is a declaration that is declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition implementation. When you call an overloaded function or operator, the compiler determines the most appropriate definition to use, by comparing the argument types you have used to call the function or operator with the parameter types specified in the definitions.
The process of selecting the most appropriate overloaded function or operator is called overload resolution. You cannot overload function declarations that differ only by return type. Thus, a programmer can use operators with user-defined types as well. Overloaded operators are functions with special names the keyword operator followed by the symbol for the operator being defined.
Like any other function, an overloaded operator has a return type and a parameter list. Most overloaded operators may be defined as ordinary non-member functions or as class member functions. The unary operators operate on the object for which they were called and normally, this operator appears on the left side of the object, as in! Following example explain how minus - operator can be overloaded for prefix as well as postfix usage. Similar way, you can overload operator You can overload any of these operators, which can be used to compare the objects of a class.
The stream insertion and stream extraction operators also can be overloaded to perform input and output for user-defined types like an object. Here, it is important to make operator overloading function a friend of the class because it would be called without creating an object.
Following example explains how an assignment operator can be overloaded. When you overload , you are not creating a new way to call a function. Rather, you are creating an operator function that can be passed an arbitrary number of parameters. Following example explains how a function call operator can be overloaded. Following example explains how a subscript operator [] can be overloaded.
It is defined to give a class type a "pointer-like" behavior. If used, its return type must be a pointer or an object of a class to which you can apply. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance. This is called static resolution of the function call, or static linkage - the function call is fixed before the program is executed. This is also sometimes called early binding because the area function is set during the compilation of the program.
This is how polymorphism is generally used. You have different classes with a function of the same name, and even the same parameters, but with different implementations. Virtual Function A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don't want static linkage for this function.
What we do want is the selection of the function to be called at any given point in the program to be based on the kind of object for which it is called. This sort of operation is referred to as dynamic linkage, or late binding. Pure Virtual Functions It is possible that you want to include a virtual function in a base class so that it may be redefined in a derived class to suit the objects of that class, but that there is no meaningful definition you could give for the function in the base class.
Data abstraction is a programming and design technique that relies on the separation of interface and implementation. Let's take one real life example of a TV, which you can turn on and off, change the channel, adjust the volume, and add external components such as speakers, VCRs, and DVD players, BUT you do not know its internal details, that is, you do not know how it receives signals over the air or through a cable, how it translates them, and finally displays them on the screen.
Thus, we can say a television clearly separates its internal implementation from its external interface and you can play with its interfaces like the power button, channel changer, and volume control without having zero knowledge of its internals. They provide sufficient public methods to the outside world to play with the functionality of the object and to manipulate object data, i.
For example, your program can make a call to the sort function without knowing what algorithm the function actually uses to sort the given values.
In fact, the underlying implementation of the sorting functionality could change between releases of the library, and as long as the interface stays the same, your function call will still work.
The data-abstraction view of a type is defined by its public members. The private sections hide the implementation from code that uses the type. There are no restrictions on how often an access label may appear. Each access label specifies the access level of the succeeding member definitions.
The specified access level remains in effect until the next access label is encountered or the closing right brace of the class body is seen. By defining data members only in the private section of the class, the class author is free to make changes in the data. If the implementation changes, only the class code needs to be examined to see what affect the change may have. If data is public, then any function that directly access the data members of the old representation might be broken.
The public members - addNum and getTotal are the interfaces to the outside world and a user needs to know them to use the class. The private member total is something that the user doesn't need to know about, but is needed for the class to operate properly.
So while designing your component, you must keep interface independent of the implementation so that if you change underlying implementation then interface would remain intact. In this case whatever programs are using these interfaces, they would not be impacted and would just need a recompilation with the latest implementation.
Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse.
Data encapsulation led to the important OOP concept of data hiding. Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.
We already have studied that a class can contain private, protected and public members. By default, all items defined in a class are private. This means that they can be accessed only by other members of the Box class, and not by any other part of your program. This is one way encapsulation is achieved. To make parts of a class public i. Making one class a friend of another, exposes the implementation details and reduces encapsulation.
The ideal is to keep as many of the details of each class hidden from all other classes as possible. The private member total is something that is hidden from the outside world, but is needed for the class to operate properly. Designing Strategy Most of us have learnt to make class members private by default unless we really need to expose them.
That's just good encapsulation. This is applied most frequently to data members, but it applies equally to all members, including virtual functions. A class is made abstract by declaring at least one of its functions as pure virtual function.
Abstract classes cannot be used to instantiate objects and serves only as an interface. Attempting to instantiate an object of an abstract class causes a compilation error. Thus, if a subclass of an ABC needs to be instantiated, it has to implement each of the virtual functions, which means that it supports the interface declared by the ABC.
Failure to override a pure virtual function in a derived class, then attempting to instantiate objects of that class, is a compilation error. Classes that can be used to instantiate objects are called concrete classes. Designing Strategy An object-oriented system might use an abstract base class to provide a common and standardized interface appropriate for all the external applications.
Then, through inheritance from that abstract base class, derived classes are formed that operate similarly. The implementations of these pure virtual functions are provided in the derived classes that correspond to the specific types of the application. This architecture also allows new applications to be added to a system easily, even after the system has been defined. This tutorial will teach you how to read and write from a file. Opening a File A file must be opened before you can read from it or write to it.
Either ofstream or fstream object may be used to open a file for writing. And ifstream object is used to open a file for reading purpose only. Following is the standard syntax for open function, which is a member of fstream, ifstream, and ofstream objects.
All output to that file to be appended to the end. You can combine two or more of these values by ORing them together. For example if you want to open a file in write mode and want to truncate it in case that already exists, following will be the syntax: ofstream outfile; outfile.
But it is always a good practice that a programmer should close all the opened files before program termination. Following is the standard syntax for close function, which is a member of fstream, ifstream, and ofstream objects. The only difference is that you use an ofstream or fstream object instead of the cout object.
The only difference is that you use an ifstream or fstream object instead of the cin object. After writing information entered by the user to a file named afile. These member functions are seekg "seek get" for istream and seekp "seek put" for ostream. The argument to seekg and seekp normally is a long integer. A second argument can be specified to indicate the seek direction. The seek direction can be ios::beg the default for positioning relative to the beginning of a stream, ios::cur for positioning relative to the current position in a stream or ios::end for positioning relative to the end of a stream.
The file-position pointer is an integer value that specifies the location in the file as a number of bytes from the file's starting location. Exceptions provide a way to transfer control from one part of a program to another. This is done using a throw keyword. The catch keyword indicates the catching of an exception. It is followed by one or more catch blocks.
Assuming a block will raise an exception, a method catches an exception using a combination of the try and catch keywords. The operand of the throw statement determines a type for the exception and can be any expression and the type of the result of the expression determines the type of exception thrown.
You can specify what type of exception you want to catch and this is determined by the exception declaration that appears in parentheses following the keyword catch. If you want to specify that a catch block should handle any type of exception that is thrown in a try block, you must put an ellipsis, If we compile and run above code, this would produce the following result: Division by zero condition!
Define New Exceptions You can define your own exceptions by inheriting and overriding exception class functionality. This returns the cause of an exception. Many times, you are not aware in advance how much memory you will need to store particular information in a defined variable and the size of required memory can be determined at run time. This operator is called new operator. If you are not in need of dynamically allocated memory anymore, you can use delete operator, which de-allocates memory previously allocated by new operator.
0コメント