Pre & Post Increment Decrement Operator Overloading in C++ Part-2

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

//Unary Operator overloading (++  and -- post )
#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;
    }
    Test(){}
    Test operator++();  // pre increment
    Test operator++(int);//post increment
    void operator--(); // Pre decrement
    void operator--(int); // post decrement
    void display();  // Display value of Object
};
void Test::display()
{
     cout<<"\n"<<a <<"  "<<b<<"  "<<c;
}
Test Test::operator++()
{
    ++a;
    ++b;
    ++c;
    return *this;
}
Test Test::operator++(int)
{
     Test temp=*this;
      a++;
      b++;
      c++;
      return temp;
}
void Test::operator--()
{
    
    --a;
    --b;
    --c;
}
void Test::operator--(int)
{
      a--;
      b--;
      c--;
}


int main()
{
    clrscr();
    Test T;
    Test T1(10,20,30);
    T=T1++;
    T.display();
    cout<<"\n";
    T1.display();
    // T1.display();
    // T1--;   // T1.operator++(int)
    // T1.display();
    return 0;
}

 

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

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

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