Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Unit-3 Operator Overloading and Type Conversion &Inheritance

Operator overloading is one of the important and useful features of C++.


C++ permits to add two variables of user-defined data types with same Syntax that is applied to the
basic types .this means that C++ has the ability to provide the operator with a special meaning.
this mechanism of giving such meaning to an operator overloading. operator overloading is one
of the most valuable concept introduced by C++ languages. C++ has a number of standard data
types such as int, float and char operators+, −, *and = are used to carry operation with these data
types operator overloading helps the programmer to use these operators with the object of classes
.
Defining operator overloading:
The keyboard operator defines a new action or operation to the operator.

Syntax:
return type operator symbol (parameters)
{
statement1;
statement2;
}

 Return type is the type of value returned by the specified operation.


 Symbol is the operator being guava loaded.
 Symbol is proceeded by the keyboard operator.
 Operator functions must be either member function or friend function.
 The basic difference between them is that a friend function will have only one argument
for unary operators and two for binary operators.
 While a member function has no arguments for unary operator and only one for binary
operator

Process of overloading involves the following steps

1. create a class that defines a data type that is to be used in the overloading operation
2. declare the operator function in the public part of class. it may be either Member function
or friend function
3. defined the operator function to implement the required operations

Overloading unary operators


Let us consider unary minus operator in minus operator, a minus operator when used as a
unary takes one operand. This operator changes the sign of an operand. When applied to
a basic data item. unary minus when applied to an object should change the sign of each
of its data items.
Program to overload unary minus

#include<iostream>
using namespace std;
class space
{
private:int x,y,z;
public:void getdata()
{
cout<<"enter the values for x,y and z";
cin>>x>>y>>z;
}
void display()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"z="<<z<<endl;
}
void operator-()
{
x=-x;
y=-y;
z=-z;
}
};
int main()
{
space s;
s.getdata();
s.display();
-s;
s.display();
}

Output:
enter the values for x,y and z 10 -20 30
x=10
y=-20
z=30
x=-10
y=20
z=-30
Program to overload unary minus using friend function

#include<iostream>
using namespace std;
class space
{
private:int x,y,z;
public:void getdata()
{
cout<<"enter the values for x,y and z";
cin>>x>>y>>z;
}
void display()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"z="<<z<<endl;
}
friend void operator-(space &sp);
};
void operator-(space &sp)
{
sp.x=-sp.x;
sp.y=-sp.y;
sp.z=-sp.z;
}
int main()
{
space s;
s.getdata();
s.display();
operator-(s);
s.display();
}

Output:
enter the values for x,y and z 10 -20 30
x=10
y=-20
z=30
x=-10
y=20
z=-30
Overloading Binary operator
Just like unary operators, binary operators can overloaded. Binary operators require two operands.
they can be overloaded by using member functions and friend functions.

1. Overloading binary operators using member functions


If overloaded as a member function, binary operators require one argument. The member
function can be called with help of an object.
One object invokes the function operator, another object is used as argument for the
function.

Syntax:
Object1.operator symbol+(object2)

2. Overloading binary operators using friend functions


friend function can be used for overloading of binary operator. friend function required
two operands to be passed as an arguments
Syntax
operator symbol (object1,object2)

Program to overload binary operator using member function

#include<iostream>
using namespace std;
class complex
{
private:float real,imag;
public:void getdata()
{
cout<<"enter the values for real and imaginary";
cin>>real>>imag;
}
complex operator+(complex c)
{
complex temp;
temp.real=real+c.real;
temp.imag=imag+c.imag;
return temp;
}
void display()
{
cout<<real<<"+i"<<imag;
}
};
int main()
{
complex c1,c2,c3;
c1.getdata();
c2.getdata();
c3=c1+c2; // c3=c1.operator+(c2);
c3.display();
}

Output:
enter the values for real and imaginary3.5 4.2
enter the values for real and imaginary2.3 2.1
5.8+i6.3

Program to overload binary operator using friend function

#include<iostream>
using namespace std;
class complex
{
private:float real,imag;
public:void getdata()
{
cout<<"enter the values for real and imaginary";
cin>>real>>imag;
}
void display()
{
cout<<real<<"+i"<<imag;
}

friend complex operator+(complex a,complex b);


};
complex operator+(complex a,complex b)
{
complex temp;
temp.real=a.real+b.real;
temp.imag=a.imag+b.imag;
return temp;
}
int main()
{
complex c1,c2,c3;
c1.getdata();
c2.getdata();
c3=c1+c2; // c3=c1.operator+(c2);
c3.display();
}
Output:
enter the values for real and imaginary3.2 4.4
enter the values for real and imaginary2.2 3.2
5.4+i7.6

CONSTRAINT ON INCREMENT AND DECREMENT OPERATORS


Data members of one object are initialized with some values and the same values are assigned to
another object with assignment operator

Assignment operator can be overloaded in two ways

 Implicit overloading
 Explicit overloading

Program to overload the assignment operator implicitly

#include<iostream>
using namespace std;
class num
{
private:int x;
public:num(int a)
{
x=a;
}
void show()
{
cout<<x;
}
};
int main()
{
num a1(3),a2(8);
cout<<"before overloading assignment operator"<<endl;
cout<<"a1 object value of x is=";
a1.show();
cout<<"\n a2 object value of x is=";
a2.show();
a2=a1;
cout<<"\n after overloading assignment operator"<<endl;
cout<<"a1 object value of x is=";
a1.show();
cout<<"\n a2 object value of x is=";
a2.show();
}
Output:
before overloading assignment operator
a1 object value of x is=3
a2 object value of x is=8
after overloading assignment operator
a1 object value of x is=3
a2 object value of x is=3

Program to overload the assignment operator explicitly

#include<iostream>
using namespace std;
class num
{
private:int x,y;
public:num(int a,int b)
{
x=a;
y=b;
}
void operator=(num nm)
{
x=nm.x;
y=nm.y;
}
void show()
{
cout<<"\n x="<<x;
cout<<"\n y="<<y;
}
};
int main()
{
num n1(10,20);
num n2(40,50);
n1.show();
n2.show();
n2.operator=(n1);
n1.show();
n2.show();
}
Output:
x=10
y=20
x=40
y=50
x=10
y=20
x=10
y=20

CONSTRAINT ON INCREMENT AND DECREMENT OPERATORS

When an operator (increment/decrement) is used as prefix with object, its value is incremented /
decremented before operation. the postfix use of operator increments/decrements the value of
variable after its use.

++ and -- operators are overloaded for the prefix operation work for postfix operations but
with warning message, but vice versa is not possible. To distinction between prefix and postfix
notations, a new syntax is used to indicate postfix operator overloading function.

Syntax
Operator ++ ( int ) // postfix notation
Operator ++() // prefix notation

Program to overload ++ and -- operator for prefix and postfix

#include<iostream>
using namespace std;
class number
{
private:int x,y;
public:number(int k)
{
x=k;
y=0;
}
//postfix
void operator++(int)
{
y=x++;
}
void operator--()
{
y=--x;

}
void show()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y;
}

};
int main()
{
number n(10);
cout<<"\n before incrementing";
n.show();
cout<<"\n after incrementing";
n++;
n.show();
cout<<"\n after decrementing";
--n;
n.show();
}

Output:
before incrementing x=10
y=0
after incrementing x=11
y=10
after decrementing x=10
y=10

Type Conversion
Process of converting from one data type to another. There are three possibilities of data
conversion:
 Conversion from basic data type to user-defined data type(class type)
 Conversion from class type to basic data type
 Conversion from one class type to another class type

Conversion from Basic to Class Type


The conversion from basic to class type is automatically done by the compiler with the help of in-
built routines . In this type, the left-hand operand of = sign is always class type and the right-hand
operand is always basic type.
Program to convert basic to class type

#include<iostream>
using namespace std;
class data
{
int x;
float y;
public:data(float m)
{
x=2;
y=m;
}
data()
{
x=y=0;
}
void show()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y;
}
};
int main()
{
data d=3.4;
d.show();
}

Output:
x=2
y=3.4

Conversion from Class Type to Basic Data Type


In this type of conversion, the programmer needs to explicitly tell the compiler how to perform
conversion from class to basic type. These Such type of conversion is also known as overloading
of type cast operators. In this type, the left-hand operand is always of basic data type and the right-
hand operand is always of class type. During this conversion, the statement should satisfy the
following conditions:
 The conversion function should not have any argument.
 Do not mention return type.
 It should be a class member function.
Program to convert class type data to basic type data

#include<iostream>
using namespace std;
class data
{
int x;
float y;
public:data()
{
cout<<"\n data default constructor";
x=y=0;
}
operator int()
{
return x;
}
operator float()
{
return y;
}
data(float m)
{
cout<<"\n data parameterized float constructor";
x=2;
y=m;
}
void show()
{
cout<<"\n x="<<x<<endl;
cout<<"y="<<y;
}
};
int main()
{
int i;
float j;
data d;
d.show();
d=2.5;
d.show();
i=d; // operator int() is executed
j=d; // operator float() is executed
d.show();
cout<<"\n value of i="<<i<<endl;
cout<<"value of j="<<j<<endl;
}
Output:
data default constructor
x=0
y=0
data parameterized float constructor
x=2
y=2.5
x=2
y=2.5
value of i=2
value of j=2.5

Conversion from One Class Type to Another Class Type


When an object of one class is assigned to object of another class, it is necessary to give clear-cut
instructions to the compiler about how to make conversion between these two user-defined data
types.
Basic 2 steps are required for conversion

 define a conversion operator function in source class


 one-argument constructor in a destination class.

Consider the following example:


X=A;
Here, X is an object of class XYZ and A is an object of class ABC. The class ABC data type is
converted to class XYZ. The conversion happens from class ABC to XYZ. The ABC is a source
class and XYZ is a destination class.

Program to convert integer to date and vice versa using conversion function in source class.
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class date
{
char d[20];
public:date()
{
strcpy(d,"");
}
date(char *e)
{
strcpy(d,e);
}
void show()
{
cout<<d;
}
};
class dmy
{
int mt,dy,yr;
public:dmy()
{
mt=dy=yr=0;
}
dmy(int m,int d,int y)
{
mt=m;
dy=d;
yr=y;
}
operator date()
{
char temp[60],dt[60];
itoa(dy,dt,10);
strcat(dt,"-");
itoa(mt,temp,10);
strcat(dt,temp);
strcat(dt,"-");
itoa(yr,temp,10);
strcat(dt,temp);
return (date(dt));
}
void show()
{
cout<<dy<<""<<mt<<""<<yr;
}
};
int main()
{
date d1;
dmy d2(1,8,10);
d1=d2;
cout<<"d1=";
d1.show();
cout<<"\n d2=";
d2.show();
}

Output:
d1=8-1-10
d2=8110

Value => value to be converted into string


Str => array in memory
Base => numerical base used to represent the value as string
10 means decimal base
One argument Constructor and operator function
single argument Constructor and an operator function can be used for conversion of objects of
different classes

Conversion Types

s.no Conversion Types Routine in destination Routine in source class


class

1. Class to class constructor Conversion function

2. Class to basic Not applicable Conversion function

3. Basic to class constructor constructor

OVERLOADING STREAM OPERATORS


The predefined objects cin and cout are used to perform various input/output operations in C++.
The extraction operator (>>) is used with cin object to carry out input operations. The insertion
operator (<<) is used with cout object to carry out output operations. The syntax for overloading
(<<) insertion operator is as follows:

friend void operator << ( ostream & put, v1)


{
// code
return put;
}
The keyword friend precedes the declaration. The ostream is an output stream class followed by
reference and keyword operator. The put is an output stream object like cout. The v1 is a user-
defined class object.

Program to overload insertion operator (<<) with friend function.

include<iostream>
using namespace std;
class sample
{
int x;
public:sample(int m)
{
x=m;
}
friend void operator << (ostream &put,sample &k)
{
put<<k.x;
}
};
int main()
{
sample s(20);
cout<<s;
}

Output:
20

You might also like