Operator Overloading: Z X+y Long Etc
Operator Overloading: Z X+y Long Etc
• The term operator overloading means giving the C++ operators such
as ( -, +, --, ++, *, /, >, <, >=, <= etc.) , additional meanings while
applying on user-defined data types.
+ - * / % ^ & |
~
! = < > += -= /=
*= %=
^= &= |= << >> >= <=
== !=
>>= <<= && || ++ -- ->* ,
->
[] () new new[] delete delete[]
The users can’t redefine the following
operators.
• Here, return_type is the type of the value returned by the specified operation.
• The combination of the keyword operator and the operator_type i.e. operator
operator_type represents the name of the function.
• For example, in the above program the declarative of the operator function is
given as:
• void operator – ( ) ;
Cont.
• This declarative tells the compiler to call this member function, whenever the –
operator is encountered, provided the operand is of type Minus.
• In the main( ) function of this program, the object M of type Minus is declared
and initialized to ( 5, 10, -15 ) as:
Minus M( 5, 10, -15 ) ;
• Before activating the operator function, the values of a, b and c are displayed
as:
a = 5
b = 10
c = -15
-M;
the operator function gets activated and the values of a, b and c are displayed
as:
a = -5
b = -10
c = 15
Cont.
• In the above program, the function operator – ( )
takes no argument.
M2 = -M1 ;
• in the main( ), the compiler will complain! The reason is that the operator
function operator – ( ) has been defined to have a return type of void, while the
assignment statement is being asked to return a variable of type Minus. Thus,
the statements such as
M2 = -M1 ;
//*******Main Functiion************
void main(void)
{
Minus M1(5, 10, -15); // Initialization of the object M1 of type Minus
cout << "\n Before activating the operator -( ):\n" ;
M1.display( ) ;
Minus M2 ; // Definining the object M2 of type Minus
M2 = -M1 ; // Activates the function operator -( ) and
// assigns the value of -M1 to M2
cout << "\n After activating the operator -( ):\n" ;
M2.display( ) ;
}
Output:
Cont.
• In the above program, the operator – ( ) function creates a new object,
named as temp of type Minus, for returning the values.
temp.a = -a ;
temp.b = -b ;
temp.c = -c ;
return temp ;
M2 = -M1 ;
I1++ ;
++I2 ;
and
I3 = I1++ ;
I4 = ++I2 ;
have the same effect i.e. they are incremented using the prefix notation
only.
• Thus, both the objects are available for the function. For example, in the
statement:
real refers to the object c1 and c.real refers to the object c2. temp.real is the
real part of the complex that has been created to hold the result of addition of
c1 and c2.
Nameless temporary object:
• One can avoid the creation of the object temp by replacing
the entire function body by the following statement:
• But remember that this statement requires a constructor that takes two
arguments.
• Using nameless temporary objects can make the code shorter, more
efficient and better to read.
Program:
// This program illustrates the overloading of the binary operators + and -
#include<iostream.h>
class complex
{
private:
float real, imaginary ;
public:
complex( ) // Default Constructor without arguments
{ }
complex(float r, float i) // Constructor with arguments
{
real = r ;
imaginary = i ;
}
void enterdata(void)
{
cout << "\n Enter the real part of the second complex number: ” ;
cin >> real ;
cout << "\n Enter the imaginary part of the second complex number: " ;
cin >> imaginary ;
}
Cont.
void display(void)
{
if (imaginary >= 0)
cout << real << "+" << imaginary << " i" << endl ;
else
cout << real << imaginary << " i" << endl ;
}
// Operator Function Definition for overloading the binary operator +
complex operator + (complex C)
{
return complex( (real + C.real), (imaginary + C.imaginary) ) ;
}
cout << "\n The first complex number i.e. c1 is: " ;
c1.display( ) ; // Displays the first complex number
c2.enterdata( ) ;
cout << "\n The second complex number i.e. c2 is: " ;
c2.display( ) ; // Displays the second complex number continued
complex c3 = c1 + c2 ;
cout << "\n The sum of two complex numbers i.e. c1 and c2 is: " ;
c3.display( ); // Displays the third complex number
complex c4 = c1 - c2 ;
cout << "\The subtraction of the complex numbers, c2 from c1 is: " ;
c4.display( ); // Displays the fourth complex number
}
Output:
Overloading arithmetic assignment operators:
// This program illustrates the overloading of arithmetic assignment operator
#include<iostream.h>
class Arith_asign
{
private:
int num ;
public:
Arith_asign( )
{ num = 0; }
Arith_asign(int n)
{ num = n ; }
void Enter(void)
{
cout << "\n Enter the number: " ; cin >> num ;
}
void Display(void)
{
cout << num << endl;
}
Cont.
// Operator function definitions
void operator += (Arith_asign& a)
{
cout << "\n The function operator += is invoked: " ;
num += a.num ;
}
Second run:
int i ;
float f = 7.5 ;
i=f;
• In the above first two statements, two variables i of integer type and f of
float type are declared.
• The third statement converts the variable f to an integer before its value
is assigned to i.
• There are many other such conversions possible i.e. from float to
double, char to float and so on.
Cont.
• Each such conversion has its own routine built into the
compiler.
float a = 7.5 ;
int b ;
b = int( a ) ;
Cont.
// ********Conversion Function************
operator float( ) // converts user-defined type i.e. Distconv to the
{ // basic-type i.e. meter
float K = float(meter)/1000.0 ; // Converts the meters to Kilometers
K += float(Kmeter) ; // Adds the Kmeter
return K / M ; // Converts to mile
}
}; // End of the Class Definition
Cont.
void main(void)
{
float mile = 5.0;
Distance d1(mile); // Uses the constructor with one argument
//Distance d1 = 5.0 ;
//Distance d1 = Distance(5.0);
d1 = 2.0 ; // This form also uses the constructor with one argument
cout << "\n d1 = " ;
d1.Display( ) ;
Distconv(float mile)
{
float Km = M * mile ;
Kmeter += int(Km) ;
meter = (Km – Kmeter) * 1000 ;
}
• Distconv d1 = 5.0 ;
• The function assumes that this argument represents mile. It converts the
argument to Km and meter.
Cont.
• Thus the conversion from mile to Distconv is carried out
along with the creation of an object in the statement:
Distconv d1 = 5.0 ;
• d1 = 2.0 ;
operator typeneme( )
{
statement(s) ;
}
object_A = object_B ;
void main(void)
{
Distance1 d1 ; // Object of the destination class
Distance2 d2(1000, 100) ; // Object of the source class
d1 = d2 ; // Conversion of type Distance2 to the type Distance1
cout << "\n Distance2 = " ;
d2.display ( ) ;
cout << "\n Distance1 = " ;
d1.display( ) ;
Output:
Explanation
The above program, contains two classes: Distance1 and Distance2. In the
main( ) function the first statement is given as:
Distance d1 ;
d1 = d2 ;
converts the object d2 of the source class Distance2 to the object d1 of the destination class
Distance1.
// Converstion routine
Distance1(Distance2 d2) // Constructor with an argument of type Distance2
{
float a = d2.getm( ); // Access “meter” from the class Distance2, using the
//member function getm( )
float b = d2.getcm( ); // Access “cmete”r from the class Distance2, using the
// member function getcm()
Kmeter = a/1000.0 + b/100000.0 ;
}
void display(void)
{ cout << Kmeter << " Kilometers" ; }
};
Cont.
void main(void)
{
Distance1 d1 ;
Distance2 d2(1000, 100);
d1 = d2 ;
cout << "\n Distance2 = " ; d2.display( );
cout << "\n Distance1 = " ; d1.display( );
}
Output:
Explanation
• Above program, uses the constructor with one
argument, to convert an object d2 of class Distance2 to
the object d1 of the class Distance1. Thus, the
conversion constructor is being used in the destination
class Distance1. To perform the conversion, this
constructor must be able to access the data members
(meter and cmeter) of the Distance1 object sent as an
argument. For this, the Distance1 class contains the
following member functions, defined in the public area:
int getm(void)
{ return meter ; }
int getcm(void)
{ return cmeter ; }
Cont.
• The one argument constructor is specified in the
destination class Distance1 as: