Operator Overloading Example in C++

 #include<iostream>

using namespace std;

class space

{

int x,y,z;

public:

void getData(int m,int n,int o)

{

x=m;

y=n;

z=o;

}

void showData()

{

cout<<"value of x =\t"<<x<<endl;

cout<<"value of y =\t"<<y<<endl;

cout<<"value of z =\t"<<z<<endl;

}

void operator -();

};

void space :: operator -()

{

x=-x;

y=-y;

z=-z;

}

int main()

{

space s;

cout<<"Before Operator overloading :-"<<endl;

s.getData(100,-29,39);

s.showData();

s.operator -();

cout<<"\nAfter Operator overloading :-"<<endl;

s.showData();

}

Output:

Before Operator overloading :-

value of x =    100

value of y =    -29

value of z =    39

After Operator overloading :-

value of x =    -100

value of y =    29

value of z =    -39



Comments