0% found this document useful (0 votes)
37 views

Operator Overloading

The document discusses operator overloading in C++. It shows how to overload operators like ++, +, * for a Counter and Complex class. The key points are: 1) The ++ and + operators are overloaded for the Counter and Complex classes to add or increment their data members. 2) Overloaded operators can be class members or non-members. Non-member operators are declared as friends. 3) Overloaded operators allow treating user-defined types like built-in types when using operators on them. This allows natural and intuitive usage of classes.

Uploaded by

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

Operator Overloading

The document discusses operator overloading in C++. It shows how to overload operators like ++, +, * for a Counter and Complex class. The key points are: 1) The ++ and + operators are overloaded for the Counter and Complex classes to add or increment their data members. 2) Overloaded operators can be class members or non-members. Non-member operators are declared as friends. 3) Overloaded operators allow treating user-defined types like built-in types when using operators on them. This allows natural and intuitive usage of classes.

Uploaded by

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

nary Operator Overloading

void main()
{clrscr();
Counter c1,c2;
cout<<"\nc1="<<c1.get_co
unt();
cout<<"\nc2="<<c2.get_co
unt();
c1++;
c2++;
++c2;
cout<<"\nc1="<<c1.get_co
unt();
cout<<"\nc2="<<c2.get_co
unt();
getch();
} Output:- C1=0

#include<iostream.h>
#include<conio.h>
class Counter
{ private:
unsigned int count;
public:
Counter() {count=0;}
int get_count(){return
count;}
void operator++(int)
{count++;}
void operator++()
C2=0
C1=1
{count++;}
C2=2
};12/24/15
By: Dy. Head. Lect. Sudeep Shakya,
KEC, Kalimati

#include<iostream.h>
//adds default and C2 complex objects.
#include<conio.h>
complex
class complex
complex::AddComplex(complex c2)
{private:
{
float real;//real part of complex
number.
complex temp;//object temp of
float imag;//imag. part of
complex class
complex number.
temp.real=real+c2.real; //object
public:
temp of complex class
complex()//no argument
temp.imag=imag+c2.imag; //add
constructor.
imaginary parts
{real=imag=0.0;}
return temp;
void getdata()
}
{
cout<<"Real Part?";
void main()
cin>>real;
{
clrscr();
cout<<"Imag Part?";
complex c1,c2,c3; //c1,c2,c3 are object
cin>>imag;
of
//complex class
}
cout<<"Enter Complex Number
complex AddComplex(complex
C1..."<<endl;
C2);//Add complex
c1.getdata();
void outdata(char *msg)
cout<<"Enter Complex Number
{
cout<<endl<<msg;
C2..."<<endl;
cout<<"("<<real;
By: Dy. Head. c2.getdata();
Lect. Sudeep Shakya,
cout<<","<<imag<<")";
12/24/15
2
KEC, Kalimati
c3=c1.AddComplex(c2);
}

Output
Enter Complex Number C1...
Real Part?2.5
Imag Part?2.0
Enter Complex Number C2...
Real Part?3.0
Imag Part?1.5
c3=c1.Addcomplex(c2):(5.5,3.5)
12/24/15

By: Dy. Head. Lect. Sudeep Shakya,


KEC, Kalimati

After using operator overloading


#include<iostream.h>
//adds default and C2 complex
#include<conio.h>
objects.
complex complex::operator+
class complex
(complex c2)
{private:
float real;//real part of complex number. {
float imag;//imag. part of complex number. complex temp; //object temp of
complex class
public:
temp.real=real+c2.real;
complex()//no argument constructor.
//object temp of complex class
{real=imag=0.0;}
temp.imag=imag+c2.imag;//add
complex(float x,float y)
imaginary parts
{
real=x;
return temp;
imag=y;
}
}
void main()
complex operator+(complex);//Add complex
{
clrscr();
void outdata(char *msg)
complex
{
cout<<endl<<msg;
c1(2.5,3.5),c2(1.6,2.7),c3; //c1,c2,c3
cout<<"("<<real;
are object of complex class
cout<<","<<imag<<")";
c1.outdata("C1=");
}
c2.outdata("C2=");
};
12/24/15

c3=c1+c2;
c3.outdata("c3=c1+c2:
");
By: Dy. Head. Lect. Sudeep
Shakya,
KEC, Kalimati getch();

Output:
C1=(2.5,3.5)
C2=(1.6,2.7)
c3=c1+c2:
(4.1,6.2)

12/24/15

By: Dy. Head. Lect. Sudeep Shakya,


KEC, Kalimati

#include<iostream.h>
#include<conio.h>
class example
{
int i;
float j;
public:
example()
{i=0; j=0;}
example(int x)
{ i=x;
j=0.0;
}
example(int x, float y)
{ i=x;
j=y;
}
void showdata()
{cout<<i<<" "<<j<<endl;
}
example operator*(example);
};
12/24/15

example
example::operator*(example e)
{
example temp;
temp.i=i*e.i;
temp.j=j*e.j;
return(temp);
}
void main()
{clrscr();
example
e1(10,3.14),e2(2,1.5),e3,e4,e5;
e3=e1*e2;
e1.showdata();
e4=e1*2;
e2.showdata();
e3.showdata();
e4.showdata();
//e5=2*e1;//int*example cause an
error.
getch();
}

By: Dy. Head. Lect. Sudeep Shakya,


KEC, Kalimati

Output:
10 3.14
2 1.5
20 4.71
20 0

12/24/15

By: Dy. Head. Lect. Sudeep Shakya,


KEC, Kalimati

#include<iostream.h>
#include<conio.h>
class example
{ int i;
float j;
public:
example()
{i=0; j=0;}
example(int x)
{
i=x;
j=0.0;
}
example(int x, float y)
{
i=x;
j=y;
}
void showdata()
{
cout<<i<<" "<<j<<endl;
}
friend example
operator*(example,example);
};
12/24/15

example operator*(example
e1,example e2)
{
example temp;
temp.i=e1.i*e1.i;
temp.j=e2.j*e2.j;
return(temp);
}
void main()
{clrscr();
example
e1(10,3.14),e2(1,1.5),e3,e4,e5;
e3=e1*2;
e4=2*e1;
e5=e1*e2*2;
e1.showdata();
e2.showdata();
e3.showdata();
e4.showdata();
e5.showdata();
getch();
}

By: Dy. Head. Lect. Sudeep Shakya,


KEC, Kalimati

Output:
10 3.14
1 1.5
100 0
4 9.859601
10000 0

12/24/15

By: Dy. Head. Lect. Sudeep Shakya,


KEC, Kalimati

You might also like