Pre and Post Increment Decrement with Assignment Operator Overloading in C++

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;
      }
      Test(){}
    Test operator++();// pre
    Test operator++(int); //post
    Test operator--();// pre
    Test operator--(int); //post
    void display();
};
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;
}

Test Test::operator--()
{
      --a;
      --b;
      --c;
      return *this;
}

Test Test::operator--(int)
{
   Test temp=*this;
   a--;
   b--;
   c--;
  return temp;
}

int main()
{
    clrscr();
    Test T1;
    Test T2(10,20,30);
   T1=T2--;   // T2.operator--(int)
    T1.display();

    T2.display();
    // T1=++T2;
    // T1.display();
    // T2.display();
}

 

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

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