7.7 END OF CHAPTER PROGRAMS

7.7.1 Overloading of assignment operator [case 1]

Consider that we have two objects of class Point, pt1 and pt2. If data members of pt1 are initialized to some values and if we say pt2 = pt1 then data members of pt2 will have the same value as that of pt1. It means object pt2 is copied into object pt1 (Implicit overloading of = operator by the compiler). It is also possible to write a friend function to this effect. Program 7.9 illustrates how to do it explicitly.

Problem: Write a program to overload assignment (=) operator.

Solution: We cannot have implicit and explicit overloading simultaneously. Hence, we will first demonstrate explicit overloading. See Program 7.9.

Program 7.9 Overloading assignment operator

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.