overloads assignment operator (=) : Assign « Overload « C++






overloads assignment operator (=)

  
#include <iostream>
using namespace std;
class alpha  {
   private:
      int data;
   public:
      alpha(){ }
      alpha(int d){ data = d; }
      void display(){ cout << data; }
      alpha operator = (alpha& a){
         data = a.data;
         cout << "\nAssignment operator invoked";
         return alpha(data);
      }
};
int main(){
   alpha a1(37);
   alpha a2;

   a2 = a1;
   cout << "\na2=";
   a2.display();

   alpha a3 = a2;
   cout << "\na3=";
   a3.display();
   cout << endl;
   return 0;
}
  
    
  








Related examples in the same category

1.Demo: Overload the +, -, and = relative to MyClass.Demo: Overload the +, -, and = relative to MyClass.
2.Another Demo: Overload the == and && relative to MyClass class.Another Demo: Overload the == and && relative to MyClass class.
3.overloaded '+=' assignment operator
4.Operator Overloading: Overload the +, =,