0% found this document useful (0 votes)
1 views29 pages

Operator Overloading PDF

Operator overloading in C++ allows normal operators to be given additional meanings for user-defined data types, enabling operations like addition and comparison using the same syntax as basic types. The document explains how to implement operator overloading through operator functions, provides examples for various operators, and discusses type conversion between basic and user-defined types. It also includes exercises for practicing operator overloading and type conversion concepts.

Uploaded by

Prashun Dhungana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views29 pages

Operator Overloading PDF

Operator overloading in C++ allows normal operators to be given additional meanings for user-defined data types, enabling operations like addition and comparison using the same syntax as basic types. The document explains how to implement operator overloading through operator functions, provides examples for various operators, and discusses type conversion between basic and user-defined types. It also includes exercises for practicing operator overloading and type conversion concepts.

Uploaded by

Prashun Dhungana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Compiled By:

Sushant Paudel
Operator Overloading
Introduction
Operator overloading is one of the most exciting features of C++. The term operator
overloading refers to giving the normal C++ operators additional meaning so that
we can use them with user-defined data types. For example, C++ allows us to add
two variables of user-defined types with the same syntax that is applied to the basic
type. We can overload all the C++ operators except the following:

Class member access operators (., .*)


Scope resolution operator (::)
Size operator (sizeof)
Conditional operator (?:)
Operator overloading is done with the help of a special function, called operator function. The
general form of an operator function is:
return-type operator op(arguments)
{
Function body
}
Where return-type is the type returned by the operation, operator is the keyword, and op is the
operator symbol. For example, to add two objects of type distance each having data members feet
of type int and inches of type float, we can overload + operator as follows:
distance operator +(distance d2)
{
//function body
}

And we can call this operator function with the same syntax that is applied to its basic types as
follows:
d3 = d1 + d2;
#include<iostream> complex add(complex c)
#include<conio.h> {
using namespace std; complex temp;
class complex temp.a=a+c.a;
{ temp.b=b+c.b;
private: return(temp);
int a,b; }
public: };
void setdata(int x, int y) int main()
{ {
a=x; complex c1,c2,c3;
b=y; c1.setdata(3,4);
} c2.setdata(5,6);
void showdata() c3=c1.add(c2);
{ //c3=c1+c2; (result ?)
cout<<"a="<<a<<"b="<<b<<endl; c3.showdata();
} }
#include<iostream>
#include<conio.h> complex operator+(complex c)
using namespace std; {
class complex complex temp;
{ temp.a=a+c.a;
private: temp.b=b+c.b;
int a,b; return(temp);
public: }
void setdata(int x, int y) };
{ int main()
a=x; {
b=y; complex c1,c2,c3;
} c1.setdata(3,4);
void showdata() c2.setdata(5,6);
{ c3=c1+c2;
c3.showdata();
cout<<"a="<<a<<"b="<<b<<endl; }
}
#include<iostream> cout<<"equal"<<endl;
#include<conio.h> else
using namespace std; cout<<"not equal"<<endl;
class compare }
{
int a; };
public : int main()
void getdata() {
{ compare t1,t2;
int x; t1.getdata();
cout<<"enter value of a"<<endl; t2.getdata();
cin>>x; t1.comparedata(t2);
a=x; getch();
} return 0;
void comparedata(compare t) }
{
if(a==t.a)
#include<iostream>//operator overloading of == operator cout<<"equal"<<endl;
#include<conio.h> else
using namespace std; cout<<"not equal"<<endl;
class compare }
{
int a; };
public : int main()
void getdata() {
{ compare t1,t2;
int x; t1.getdata();
cout<<"enter value of a"<<endl; t2.getdata();
cin>>x; t1==t2;
a=x; getch();
} return 0;
void operator==(compare t) }
{
if(a==t.a)
#include<iostream>//operator overloading to concatenate string Stringg operator+(Stringg t)
#include<string.h> {
#include<conio.h> Stringg temp;
using namespace std; strcpy( temp.s,s);
class Stringg strcat(temp.s, t.s);
{ return temp;
char s[20]; }
public: };
void getdata() int main() {
{ Stringg s1,s2,s3;
cout<<"Enter a string"<<endl; s1.getdata();
cin>>s; s2.getdata();
} //s3=s1+s2;
void display() s3=s1.operator+(s2);
{ s3.display();
cout<<"s="<<s<<endl; getch();
} }
#include<iostream>//operator overloading of < operator else if(hr == t.hr && min< t.min)
#include<conio.h> return 1;
using namespace std; else
class Time return 0;
{ }
int hr,min; };
public: int main()
void getdata() {
{ Time t1,t2,t3;
cout<<"Enter hour and minute"<<endl; t1.getdata();
cin>>hr>>min; t2.getdata();
} if(t1<t2)
void display() {
{ cout<<"t1 is smaller"<<endl;
cout<<hr<<"hr"<<min<<"min"<<endl; }
} else
int operator<(Time t) {
{ cout<<"t2 is smaller"<<endl;
Time temp; }
if(hr<t.hr) getch();
return 1; }
#include<iostream>//operator overloading of < operator Time temp;
#include<conio.h> if(hr<t.hr)
using namespace std; cout<<"t1 is less than t2"<<endl;
class Time else if(hr == t.hr && min< t.min)
{ cout<<"t1 is less than t2"<<endl;
int hr,min; else
public: cout<<"t2 is less than t1"<<endl;
void getdata() }
{ };
cout<<"Enter hour and minute"<<endl; int main()
cin>>hr>>min; {
} Time t1,t2,t3;
void display(){ t1.getdata();
cout<<hr<<"hr"<<min<<"min"<<endl; t2.getdata();
} t1<t2;
void operator<(Time t) getch();
{ }
#include<iostream> friend complex operator +(complex x1,complex x2);
#include<conio.h> };
using namespace std; complex operator+(complex x1, complex x2)
class complex {
{ complex temp;
private: temp.a=x1.a+x2.a;
int a,b; temp.b=x1.b+x2.b;
public: return(temp);
void setdata(int x, int y) }
{ int main()
a=x; {
b=y; complex c1,c2,c3;
} c1.setdata(3,4);
void showdata() c2.setdata(5,6);
{ c3=c1+c2;
c3.showdata();
cout<<"a="<<a<<"b="<<b<<endl; }
cout<<"In the form of complex number"<<endl;

cout<<a<<"+"<<b<<"i"<<endl;
}
Overloading Unary Operators Let us consider the increment (++) operator. This
operator increases the value of an operand by 1 when applied to a basic data item.
This operator should increase the value of each data member when applied to an
object.
#include<iostream> ++length;
#include<conio.h> ++breadth;
using namespace std; }
class rectangle void display()
{ {
private: cout<<"Length = "<<length<<endl
int length; <<"Breadth = "<<breadth;
int breadth; }
public: };
rectangle(int l, int b) int main()
{ {
length = l; rectangle r1(5, 6);
breadth = b; ++r1; //r1.operator ++();
} r1.display();
void operator ++() getch();
{ }
• When we want to return an object from member function of class without creating
an object, for this: we just call the constructor of class and return it to calling
function and there is an object to hold the reference returned by constructor.
• This concept is known as nameless temporary objects, using this we are going
to implement a C++ program for pre-increment operator overloading.
using namespace std; counter operator ++(int)
class counter{ {
public: count++;
counter() { return counter(count);//Temorary nameless object.
count=0; }
//cout << "\nStart value " << count; private:
}; int count;
counter(int a) { };
count=a; int main(void){
}; getche();
void getcount() { counter b;
cout << "\nCounter Value :>> " << count; counter c;
} ++b;
counter operator ++ (){ b.getcount();
++count; c= ++b;
return counter(count);//Temorary nameless object. //b.getcount();
} c.getcount();
getche();
using namespace std; //printing the value
#include <iostream> void printValue()
class Sample {
{ cout<<"Value of count : "<<count<<endl;
//private data section }
private: };
int count;
public: //main program
//default constructor int main()
Sample() {
{ count = 0;} int i = 0;
//parameterized constructor Sample S1(100), S2;
Sample(int c) //for(i=0; i< 5; i++)
{ count = c;} //{
//Operator overloading function definition S2 = ++S1;
Sample operator++() cout<<"S1 :"<<endl;
{ S1.printValue();
++count; cout<<"S2 :"<<endl;
//returning count of Sample S2.printValue();
//There is no new object here, //}
//Sample(count): is a constructor by passing value of count return 0;
//and returning the value (incremented value) }
return Sample(count);
}
using namespace std; }
class counter counter operator ++(int)
{ {
public: count++;
counter() return counter(count);//Temorary nameless object.
{ }
count=0; private:
cout << "\nStart value " << count; int count;
}; };
counter(int a)
{ int main(void)
count=a; {
}; getche();
void getcount() counter c,b;//initialization
{ b = ++c;
cout << "\nCounter Value :>> " << count; b.getcount();
} c.getcount();
counter operator ++ (){ getche();
++count; }
return counter(count);//Temorary nameless object.
Exercises 1. Write a program to overload unary minus (-) operator to invert sign of
data members of a distance object.
2. Write a program to compare two distance objects using < and > operators.
3. Write a program to add two complex numbers using + operator.
4. Write a program to concatenate two strings using + operator.
5. Write a program to compare two strings using = = operator.
Type Conversion The type conversions are automatic as long as the data types
involved are built-in types. If the data types are user defined, the compiler does not
support automatic type conversion and therefore, we must design the conversion
routines by ourselves. Three types of situations might arise in the data conversion in
this case.
1. Conversion from basic type to class type
2. Conversion from class type to basic type
3. Conversion from one class type to another class type
Conversion from basic type to class type inch=12*(x-feet);
In this case, it is necessary to use the constructor. }
The constructor in this case takes single argument void display()
whose type is to be converted. For example,
{
#include<iostream> cout<<feet<<endl<<inch<<endl;
#include<conio.h> }
using namespace std; };
class A int main()
{ {
int feet; float f=7.2;
int inch; A l=f;
public: L.display();
A(float x) getch();
{ }
feet=int(x);
#include<iostream> operator float()
#include<conio.h> {
using namespace std; float a= feet + inch/12.0;
Conversion from class type to basic type return a; }
In this case, it is necessary to overload casting void display()
operator. To do this, we use conversion function. {
cout<<feet<<endl<<inch<<endl;
class A
}
{
};
int feet;
int main()
int inch;
{
public:
A d(8, 6);
A(int i,int j)
float x = (float)d;
{
cout<<"x = "<<x;
feet=i;
getch();
inch=j;
}
}
Conversion from one class type to another class type
Conversion from one class type to another class type This type of conversion can
be carried out either by a constructor or an operator function. It depends upon
where we want the routine to be located – in the source class or in the destination
class.
a. Function in the source class: In this case, it is necessary that the operator
function be placed in the source class. For example,
using namespace std;
class product };
{ private: class item
int m,n; {
public: private:
product() int a,b;
{ public:
void getdata(int x,int y)
} {
product(int x,int y) a=x;
{ b=y;
m=x; }
n=y;
} item()
void showdata() {
{
cout<<m<<endl<<n<<endl; }
}
operator product()
{
//product p;
int f,i;
f=a*2;
i=b*2;
return product(f,i);
}
};
int main()
{
item i1;
product p1;
i1.getdata(3,4);
p1=i1;
p1.showdata();
}
Function in the destination class:
In this case, it is necessary that the constructor be placed in the
destination class. This constructor is a single argument
constructor and serves as an instruction for converting the
argument’s type to the class type of which it is a member.
class product{ public:
private: void showdata()
int m,n; {
public: cout<<a<<endl<<b<<endl;
void setdata(int x,int y) }
{ item(){ }
m=x; item(product p){
n=y; a=p.getm();
} b=p.getn();
int getm(){ }
return m; };
} int main()
int getn(){ { item i1;
return n; product p1;
}}; p1.setdata(3,4);
class item{ i1=p1;
private: i1.showdata();
int a,b; }

You might also like