O'Reilly logo

Object Oriented Programming with C++, Second Edition by Sunil Patekar, Mahesh Bhave

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required