KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OPERATOR OVERLOADING KOM3191 Object-Oriented Programming
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Dynamic Memory Management Allocation and deallocation of memory in a program is dynamic memory management and is performed with operators new and delete. Consider the following declaration and statement: Time *timeptr = new Time; The new operator allocates storage of the proper size for an object of type Time, calls the default constructor to initialize the object and returns a pointer of the type specified to the right of the new operator (i.e., a Time *). Note that new can be used to dynamically allocate any fundamental type (such as int or double) or class type. If new is unable to find sufficient space in memory for the object, it indicates that an error occurred by "throwing an exception." The new operator returns a 0 pointer in versions of C++ prior to the ANSI/ISO standard. To destroy a dynamically allocated object and free the space for the object, use the delete operator as follows: delete timeptr;
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 3 Dynamic Memory Management C++ allows you to provide an initializer for a newly created fundamental-type variable, as in double *ptr = new double( 3.14159 ); which initializes a newly created double to 3.14159 and assigns the resulting pointer to ptr. The same syntax can be used to specify a comma-separated list of arguments to the constructor of an object. For example, Time *timeptr = new Time( 12, 45, 0 ); initializes a newly created Time object to 12:45 PM and assigns the resulting pointer to timeptr. 10-element integer array can be allocated and assigned to gradesarray as follows: int *gradesarray = new int[ 10 ];
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 4 Dynamic Memory Management size of an array created at compile time must be specified using a constant integral expression. const int size=10; int gradesarray[size]; However, the size of a dynamically allocated array can be specified using any integral expression that can be evaluated at execution time. When allocating an array of objects dynamically, the programmer cannot pass arguments to each object's constructor. Instead, each object in the array is initialized by its default constructor. To delete the dynamically allocated array to which gradesarray points, use the statement delete [ ] gradesarray; The preceding statement deallocates the array to which gradesarray points. If the preceding statement did not include the square brackets ([ ]) and gradesarray pointed to an array of objects, only the first object in the array would receive a destructor call.
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 5 static Class Members There is an important exception to the rule that each object of a class has its own copy of all the data members of the class. In certain cases, only one copy of a variable should be shared by all objects of a class. A static data member is used for these and other reasons. Such a variable represents "class-wide" information (i.e., a property of the class shared by all instances, not a property of a specific object of the class). The declaration of a static member begins with keyword static. A class's static data members have class scope. Also, static members can be declared public, private or protected. A fundamental-type static data member is initialized by default to 0. If you want a different initial value, a static data member can be initialized once (and only once). A const static data member of int or enum type can be initialized in its declaration in the class definition. However, all other static data members must be defined at file scope (i.e., outside the body of the class definition) and can be initialized only in those definitions. Note that static data members of class types (i.e., static member objects) that have default constructors need not be initialized because their default constructors will be called.
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 6 static Class Members A class's private and protected static members are normally accessed through public member functions of the class or through friends of the class. 1. A class's public class members can be accessed through any object of that class using the object's name, the dot operator and the name of the member e.g: mygradebook.studentnumber 2. A class's public static class members can also be accessed through any object of that class using the object's name, the dot operator and the name of the member e.g: mygradebook.studentnumber 3. To access a private or protected static class member when no objects of the class exist, provide a public static member function and call the function by prefixing its name with the class name and binary scope resolution operator
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 7 static Class Members
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 8 static Class Members
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 9 static Class Members
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 10 //Example 2 //Test.h
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 11 //Example 2 //Test.cpp
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 12 //Example 2 //mymain.cpp
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 13 //Example 2 //mymain.cpp
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 14
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 15 Operator overloading C++ can give special meanings to operators, when they are used with user-defined classes. This is called operator overloading. You can implement C++ operator overloads by providing special member-functions To overload the + operator for your class, you would provide a member-function named operator + on your class. To overload the = for your class you would provide a member function named operator = on your class Point point1(1,2); Point point2(1,2); Point point3 = point1 + point2; Here two operators are used The following set of operators overloaded mostly for user-defined classes: = (assignment operator) + - * (binary arithmetic operators) += -= *= (compound assignment operators) ==!= (comparison operators)
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 16 Operator overloading Operators That Must Be Overloaded as Member Functions When overloading (), [], -> or any of the assignment operators, the operator overloading function must be declared as a class member. For the other operators, the operator overloading functions can be class members or global functions. Operators as Member Functions and Global Functions Whether an operator function is implemented as a member function or as a global function, the operator is still used the same way in expressions. So which implementation is best? When an operator function is implemented as a member function, the leftmost (or only) operand must be an object (or a reference to an object) of the operator's class. If the left operand must be an object of a different class or a fundamental type, this operator function must be implemented as a global function A global operator function can be made a friend of a class if that function must access private or protected members of that class directly. Operator member functions of a specific class are called (implicitly by the compiler) only when the left operand of a binary operator is specifically an object of that class, or when the single operand of a unary operator is an object of that class.
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 17 Point Class Example
Similarly, the overloaded stream extraction operator (>>) is used In an expression in which the left operand has type istream &, as in cin >> classobject, and the right operand is an object of a user-defined class, so it, too, must be a global function. Also, each of these overloaded operator functions may require access to the private data members of the class object being output or input, so these overloaded operator functions can be made friend functions of the class for performance reasons. KOM3191 Object Oriented Programming Dr Muharrem Mercimek 18 Point Class Example The overloaded stream insertion operator (<<) is used in an expression in which the left operand has type ostream &, as in cout << classobject. To use the operator in this manner where the right operand is an object of a userdefined class, it must be overloaded as a global function.
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 19 Point Class Example A binary operator can be overloaded as a non-static member function with one argument or as a global function with two arguments (one of those arguments must be either a class object or a reference to a class object).
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 20 Point Class Example
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 21 Point Class Example