Example : A C++ program to add two object’s variables normally (without operator overloading) and store & display them using third object’s variables .

#include <iostream>
using namespace std;
class A
{
    public:
      int x;
      int y;
    	
      A()
       {}
    	
      A(int j, int k)
      {
          x=j;
          y=k;
      }
      void display()
      {
      	cout<<x<<" "<<y;
      }
};

int main()
{
    //clrscr();    
    A a(10,20);	   //Calling of Parameterised Constructor
    A b(30,40);
    A c;           //Calling of Default Constructor
	
    // A a(10,20),b(30,40),c;	
	
    a.display();
    cout<<"\n";
    b.display();
	    
    cout<<"\n";
    c.x=a.x+b.x;
    c.y=a.y+b.y;
    
    c.display();	   
    return 0;
}
Output :
10 20
30 40
40 60

Example : A C++ program to show addition of objects as member function with return type operator function using binary operator overloading.

#include <iostream>  
using namespace std;  
class Overload  
{    
    int x;
    int y;
	  
    public:  
    Overload()
     {}	
    
    Overload(int j, int k)  
    {  
       x=j;
       y=k;  
    }
	  
    Overload operator + (Overload D)	
    {		
      Overload T;
      T.x=x+D.x;
      T.y=y+D.y;
      return T;	  		
    } 
    void display()
    {
      cout<<x<<" "<<y;	
    }   
};  
  
int main()  
{  
    Overload A(10,20),B(50,60),C;
    
    //Overload A(10,20);  
    //Overload B(50,60);  
    //Overload C;
    
    A.display();
    cout<<"\n";
    B.display();
    cout<<"\n";
    
    C=A+B; 
    C.display(); 
    return 0;  
}  
Output :
10 20
50 60
60 80

NB: This program performs addition operation using function operator.Here, the operator has return type and uses local object T to hold the addition result.Whenever the statement C=A+B  is executed then the compiler searches for definition of operator + .The object A calls/invokes the operator function and object B is passed as argument and the copy of object B is stored in the formal argument D.Here, the member variable of A is directly available in operator function as the function is invoked by the same object. 

Example : A C++ program to show addition of objects as member function without return type using binary operator overloading.

#include <iostream>  
using namespace std;  
class Overload  
{    
    int num1;  
    public:  
    Overload(int num2)  
    {  
       num1=num2;  
    }
	  
    void operator + (Overload x)	
    {		
      int total = num1+x.num1;  
      cout<<"The result of the addition of two objects is = "<<total;		
    }    
};  
  
int main()  
{  
    Overload Obj1(10);  
    Overload Obj2(20);  
    Obj1+Obj2;  
    return 0;  
}  
Output :
The result of the addition of two objects is = 30

------------  OR  ------------
#include <iostream>  
using namespace std;
  
class Overload  
{     
    int num1;  
    public:        
    Overload(int num2)  
    {  
       num1=num2;  
    }	  
    void operator + (Overload);      
};  
void Overload :: operator+(Overload x)  
{     
    int total = num1+x.num1;  
    cout<<"The result of the addition of two objects is = "<<total;  
}  
int main()  
{  
    Overload Obj1(10);  
    Overload Obj2(20);  
    Obj1+Obj2;  
    return 0;  
} 
Output :
 The result of the addition of two objects is = 30

------------  OR  ------------
#include <iostream>  
using namespace std;  
class Complex
{
   int real, imag;
    public:
     Complex()
      {
      }
     Complex(int a,int b)
      {
        real = a;
        imag = b;
      }
     void operator + (Complex c);
};

	void Complex::operator+(Complex c)
	 {
	   Complex temp;
	   temp.real = real + c.real;
	   temp.imag = imag + c.imag;
	   cout<<"real number sum is = "<<temp.real<<endl;
	   cout<<"Imaginary number sum is = "<<temp.imag<<endl;
	 }
	 
int main()
 {
  //clrscr();
  Complex c1(40,30);
  Complex c2(50,70);
  c1+c2;
  return 0;
}
Output :
real number sum is = 90
Imaginary number sum is = 100

Example : A C++ program to show real and imaginary number as friend function using unary operator overloading.

#include <iostream>  
using namespace std;  
class Complex  
{    
    float real;
    float imag;
	  
    public:  
    Complex()
     {
       real=imag=0;	  	
     }
	    
    Complex(float r, float i)  
     {  
       real=r;
       imag=i;  
     }
	  
    friend Complex operator - (Complex c)	
     {		
      c.real=-c.real;
      c.imag=-c.imag;
      return c;	
     } 
    void display()
     {
       cout<<real<<" "<<imag;	
     }   
};  
  
int main()  
{  
    Complex c1 (10.25,20.50),c2;  
    
    cout<<"The result before Negation = ";    
    c1.display();
    cout<<"\n";
    
    c2=-c1;
    cout<<"The result after Negation = ";
    c2.display();
    cout<<"\n";   
   
    return 0;  
}
Output :
The result before Negation = 10.25 20.5
The result after Negation = -10.25 -20.5  

Example : A C++ program to show negation of object’s variable using negative operator overloading.

#include <iostream>  
using namespace std;  
class Overload  
{    
    int x;
    int y;
	  
    public:	    
    Overload(int j,int k)  
    {  
       x=j;
       y=k;  
    }
	  
    void operator - ()	
    {		
      x=-x;
      y=-y;	
    } 
    void display()
    {
      cout<<x<<" "<<y;	
    }   
};  
  
int main()  
{  
    Overload A(10,20);  
    
    cout<<"The result before Negation = ";    
    A.display();
    cout<<"\n";
    
    -A;
    cout<<"The result after Negation = ";
    A.display();
    cout<<"\n";   
   
    return 0;  
}
Output :
The result before Negation = 10 20
The result after Negation = -10 -20  

NB : Here, the operator - is overloaded and the statement -calls the function operator -() and makes 
     all the member variables negative.

Example : A C++ program to show unary pre-increment operator overloading.

#include <iostream>  
using namespace std;  
class Overload  
{    
    int x;
    int y;
	  
    public:  
    Overload()
	{	  	
	}
	    
    Overload(int j,int k)  
    {  
       x=j;
       y=k;  
    }
	  
    void operator ++ ()	
    {		
      ++x;
      ++y;		
    } 
    void display()
    {
      cout<<x<<" "<<y;	
    }   
};  
  
int main()  
{  
    Overload A(10,20);  
    
    cout<<"The result before Increment = ";    
    A.display();
    cout<<"\n";
    
    ++A;
    cout<<"The result after Increment = ";
    A.display();
    cout<<"\n";   
   
    return 0;  
} 
Output :
The result before Increment = 10 20
The result after Increment = 11 21

Example : A C++ program to show unary post-increment operator overloading.

#include <iostream>
using namespace std;

class Overload
{
   private:
      int count;      
   public:   	
       // Overload(): count(3){}
       Overload()
       {
       	count=3;
       }
       
       void operator ++() 
       { 
         //count = count+1;
         count++; 
       }
       
       void display() 
       { 
	 cout<<"Total Count Value is = "<< count;	   
       }	   
};

int main()
{
    Overload Obj;   
    ++ Obj;     // Calling of function void operator ++()
    
    Obj.display();    
    return 0;
}
Output :
Total Count Value is = 4

Example : A C++ program to show assignment operator overloading.

#include <iostream>  
using namespace std;
class Complex
{
	private:
	 double real,imag;
	
	public:
	Complex()
	{
	  real = 0;
	  imag = 0;
	}
	
	Complex(double r, double i) 
	{
	  real = r;
	  imag = i;
	}
	
	double getReal() const 
	{
	  return real;
	}
	
	double getImag() const 
	{
	  return imag;
	}
	
	Complex & operator=(const Complex &);
};
	Complex & Complex::operator=(const Complex& c) 
	{
	  real = c.real;
	  imag = c.imag;
	  return *this;
	}

int main()
{
	Complex c1(5,10);
	Complex c2(50,100);
	
	cout << " The output of c1= " << c1.getReal() << "+" << c1.getImag() << "i" << endl;	
	cout << "The output of c2 before assigning = " << c2.getReal() << "+" << c2.getImag() << "i" 
             << endl;
	
	c2 = c1;
	
	cout << "After assign c1 to c2 :" << endl;
	cout << "The output ofc2 after assigning = " << c2.getReal() << "+" << c2.getImag() << "i" << 
             endl;
	
	return 0;
}
Output :
The output of c1= 5+10i
The output of c2 before assigning = 50+100i

After assign c1 to c2 :
The output ofc2 after assigning = 5+10i

Example : A C++ program to show Operator overloading from String object to basic String.

#include <iostream.>
#include <string.h>
using namespace std;
class Overload
{
	char *Str1;
	int len;
	
	public:
	Overload()
	 {
	 }
	 
	Overload(char *x)
	{
	  len=strlen(x);
	  Str1=new char[len+1];
	  strcpy(Str1,x);
	}
	
	operator char*()
	{
	  return(Str1);
	}
	
	void display()
	{
	  cout<<Str1;
	}
};

int main()
{
	//clrscr();
	Overload Obj1="Codershelpline";
	
	cout<<"String of Class type is : ";
	Obj1.display();
	cout<<endl;
	
	char *Str2=Obj1;
	cout<<"String of Basic type is : "<<Str2;
	return 0;
}
Output :
String of Class type is : Codershelpline
String of Basic type is : Codershelpline

Loading

Categories: C++

0 Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.