0% found this document useful (0 votes)
21 views7 pages

Operator Overloading Polymorphism, Virtual Function

The document discusses operator overloading in C++. Operator overloading allows operators to be used with user-defined types by defining additional functionality for operators. It provides examples of overloading the unary minus (-) and pre-increment (++) operators for an integer class. It also mentions that binary operators require an additional argument of the class type since they operate on two objects.
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)
21 views7 pages

Operator Overloading Polymorphism, Virtual Function

The document discusses operator overloading in C++. Operator overloading allows operators to be used with user-defined types by defining additional functionality for operators. It provides examples of overloading the unary minus (-) and pre-increment (++) operators for an integer class. It also mentions that binary operators require an additional argument of the class type since they operate on two objects.
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/ 7

Operator overloading

Suppose I,j,k are variable of int type. The statement


k=i+j is valid.

Suppose a,b,c are object of integer class ,and if we


want to collect the sum of the member data of the
object a and b in the object c. we cannot write
c=a+b?

Operator overloading is one of the feature in c++ . It


assign additional functionality to the exiting
operators.
Syntax of operator overloading
type operator operator_to_be_overloaded(formal argument)
{
Local variable
Statements
Return statement
}
#include<iostream>
using namespace std;
class integer
{
private:
int x;
public:
integer (int y=0)
{
x=y;
}
integer operator -()
{
integer t;
t.x=-x;
return t;

}
void display()
{
cout<<"x="<<x<<"\n";
}
};
int main()
{
integer a,b(5),c(-7);
cout<<"b=";
b.display();
a=-b;
cout<<"-a=";
a.display();
cout<<"c=";
c.display();
a=-c;
cout<<"a=";
a.display();
return 0;
}
#include<iostream>
using namespace std;
class integer
{
private:
int x;
public:
integer (int y=0)
{
x=y;
}
integer operator ++()
{
++x;
}
integer operator ++(int)
{
x++;
}
void display()
{
cout<<x<<"\n";
}
};
int main()
{
integer a(5);
cout<<"a=";
a.display();
++a;
cout<<"after ++a;,a=";
a.display();
a++;
cout<<"after a++;,a=";
a.display();
return 0;
}
Overload binary operator
* / < <=
type operator operator_to_be_overloaded(formal argument)
{
Local variable
Statements
Return statement
}
Note that special member function used for overloading a binary
operator require one more argument of the class type. This is
because operator can operate on two object .
#include<iostream>
int main()
using namespace std;
{
class measure
measure m1(2,3),m2(4,10),m3;
{
cout<<"m1:";
private:
m1.display();
int feet;
cout<<"m2:";
float inches;
m2.display();
public:
m3=m1+m2;
measure (int f=0,float i=0)
cout<<"sum=";
{
m3.display();
feet=f;
return 0;
inches=i;
}
}
measure operator +(measure &m)
{
measure t;
t.feet=feet+m.feet;
t.inches=inches+m.inches;
if(t.inches>=12)
{
t.feet++;
t.inches-=12;
}
return (t);
}

void display()
{
cout<<feet<<"-"<<inches<<"\n";
}
};

You might also like