Learning  C++
Home
Tutorials
C++  Programs
Contact  us
Sitemap



// Program to illustrate operator overloading


#include<iostream.h>
class sample
{
     private:
          int value1,value2;
     public:
          sample(int x,int y);
          void operator ++ ();
          void display();
};

sample::sample(int x,int y)
{
     value1=x;
     value2=y;
}


void sample::operator ++ ()
{
     ++value1;
     ++value2;
}


void sample:: display()
{
     cout<<"\n Value1 = "<<value1;
     cout<<"\n Value2 = "<<value2;
}



void main()
{
     sample obj(10,15);
     cout<<"\n Before overloading  ";
     obj.display();
     ++obj;
     cout<<"\n After overloading ";
     obj.display();
}



Test data

Before overloading
Value1 = 10
Value2 = 15

After overloading
Value1 = 11
Value2 = 16