The document summarizes operator overloading in C++. It discusses how operators like + can be used with user-defined types by overloading functions with the operator keyword. It provides examples of overloading unary operators like increment ++ by defining a Counter class and overloading its ++ operator. The overloaded ++ operator returns a temporary object to avoid changing the original object.
The document summarizes operator overloading in C++. It discusses how operators like + can be used with user-defined types by overloading functions with the operator keyword. It provides examples of overloading unary operators like increment ++ by defining a Counter class and overloading its ++ operator. The overloaded ++ operator returns a temporary object to avoid changing the original object.
Object oriented programming in C++ by Robert Lafore 1
Recap.. • Static and const attributes and functions • Array of pointers • Pointers to objects • This pointer • Array of pointers to objects • Cascading function calls
Object oriented programming in C++ by Robert Lafore 2
Recommended reads Object oriented programming in C++ by Robert Lafore: Chapter 8: Operator Overloading
Object oriented programming in C++ by Robert Lafore 3
Copy constructor [Revisiting] • C++ compiler provide default copy constructor (and assignment operator) with class. • When we don’t provide implementation of copy constructor, default copy constructor gets called • It works well with non-dynamic attributes • Problems: • When we have members which dynamically gets initialized at run time, default copy constructor copies this members with address of dynamically allocated memory and not real copy of this memory. • Now both the objects points to the same memory and changes in one reflects in another object • Further, when we delete one of this object other object still points to same memory, which will be dangling pointer • in such cases, we should always write our own copy constructor
Object oriented programming in C++ by Robert Lafore 4
ClassName (const ClassName &old_obj);
class Point int main()
{ { private: Point p1(10, 15); // Normal constructor is called here int x, y; Point p2 = p1; // Copy constructor is called here public: } Point(int x1, int y1) { x = x1; y = y1; }
// Copy constructor Point(const Point &p2) {x = p2.x; y = p2.y; }
int getX() { return x; }
int getY() { return y; } };
Object oriented programming in C++ by Robert Lafore 5
• Default constructor does only shallow copy • Deep copy is possible only with user defined copy constructor. In user defined copy constructor, we make sure that pointers (or references) of copied object point to new memory locations. • Copy constructor vs Assignment Operator • MyClass t1, t2; • MyClass t3 = t1; // ----> (1) • t2 = t1; // -----> (2) • Copy constructor is called when a new object is created from an existing object, as a copy of the existing object. • Assignment operator is called when an already initialized object is assigned a new value from another existing object. • In the above example (1) calls copy constructor and (2) calls assignment operator.
Object oriented programming in C++ by Robert Lafore 6
Default assignment operator= in c++ is a shallow copy class Test { public: int main() Test() {} { Test(const Test &t) Test t1, t2; { t2 = t1; //Default Assignment operator cout<<"Copy constructor called "<<endl; Test t3 = t1; //copy constructor } return 0; } Test& operator = (const Test &t) { cout<<"Assignment operator called "<<endl; return *this; t2 = t1; // calls assignment operator, same as "t2.operator=(t1);" } Test t3 = t1; // calls copy constructor, same as "Test t3(t1);" };
Object oriented programming in C++ by Robert Lafore 7
Shallow copy Class Dummy public: int main() Int *x; { Dummy(int m) Dummy d1(4); { Dummy d2 = d1; //Copy constructor x = new int; d1.display(); *x = m; d2.display(); } d1.set(10); int get() const { d1.display(); return *x; d2.display(); } return 0; void set(int m) } { *x = m; } void display() { cout << “Value of ptr : " << *x << endl; cout << “Address of ptr : " << x << endl; } }; Object oriented programming in C++ by Robert Lafore 8 Deep copy Class Dummy public: int main() Int *x; { Dummy(int m) Dummy(const Dummy &obj) Dummy d1(4); { { Dummy d2 = d1; //Copy constructor x = new int; x = new int; d1.display(); *x = m; *x = obj.get(); d2.display(); } } d1.set(10); int get() const { d1.display(); return *x; d2.display(); } return 0; void set(int m) } { *x = m; } void display() { cout << “Value of ptr : " << *x << endl; cout << “Address of ptr : " << x << endl; } }; Object oriented programming in C++ by Robert Lafore 9 Operator overloading • statements like d3.addobjects(d1, d2); Or d3 = d1.addobjects(d2); can be changed to the much more readable d3 = d1 + d2; • normal C++ operators works for basic data types such as int, double, char.. a=b+c • Using overloading, you can make this statement legal even when a, b, and c are user-defined types. • Overloaded function with operator keyword: void operator ++ ()
Object oriented programming in C++ by Robert Lafore 10
Overloading Unary Operators • increment and decrement operators ++ and –
Object oriented programming in C++ by Robert Lafore 11