Monday, April 5, 2021

unary operator overloading (OOP) (33)

 /* unary operator overloading

                                 *****************************/


#include<iostream>

using namespace std;

class unary

{

private:

int a,b,c;

public:

void inputdata(int x,int y, int z)

{

a=x;

b=y;

c=z;

}

void operator -() //here we define the non static data member to overload the unary operator

{

a=-a;

b=-b;

c=-c;

}

void display()

{

cout<<"the value of a:"<<a<<endl;

cout<<"the value of b:"<<b<<endl;

cout<<"the value of c:"<<c<<endl;

}

};

int main()

{

unary obj;

obj.inputdata(-4,7,-6);

cout<<"before using the operator overloading:"<<endl;

obj.display();

-obj; //caling operator

cout<<"after using the operator overloading:"<<endl;

obj.display();

}

No comments:

Post a Comment