7.2 UNARY OPERATOR OVERLOADING

It is possible to overload unary operators. A unary operator operates on only single object. It is logical that such an operator (other than ++ or -- operator) does not modify that object. It should modify a copy2 and return it. Let us study a simple program.

Problem: Write a program to show overloading of unary operator.

Solution: Let us take an example of complex numbers. Many times, we have to find the complex conjugate of this number. Let us overload operator ~ to convert into its complex conjugate. See Program 7.1.

Program 7.1 Overloading unary operator

  // over1A.cpp  #include<iostream.h>  class Complex    { double x, y ;     public:      Complex( double a, double b) ;      void show() ;      Complex ...

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.