C++ Program For Unary Operator Overloading

Free C++ course with real-time projects Start Now!!

Program 1

//Unary Operator overloading (- operator)
#include<iostream>
#define clrscr() system("cls")
using namespace std;

class Test
{
    int a,b,c;
    public:
    Test(int a,int b,int c)
    {
        this->a=a;
        this->b=b;
        this->c=c;
    }
    void operator-();
    void display();
 };
 void Test::operator-()
 {
     a=-a;
     b=-b;
     c=-c;
 }
 void Test::display()
 {
    {
        cout<<a <<"  "<<b<<"  "<<c;
    }
 }
 int main()
 {
     clrscr();
    Test T1(50,70,90);
    Test T2(100,20,34);
    
    T1.display();
    T2.display();
      cout<<endl;
      -T1; 
      -T2;
    T1.display();
    T2.display();

 }

Your opinion matters
Please write your valuable feedback about DataFlair on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *