fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class A
  5. {
  6. public:
  7. int x,y;
  8. char* str;
  9.  
  10. //Default Constructor
  11. A(){}
  12.  
  13. //Constructor
  14. A(int a, int b, char* s){
  15. cout<<"initialising\n";
  16. x = a;
  17. y = b;
  18. str = new char[10];
  19. str = s;
  20. }
  21.  
  22. //Destructor
  23. ~A(){}
  24.  
  25. //Overloaded assignment operator
  26. const A& operator=(const A& obj)
  27. {
  28. cout<<"Invoking Assignment Operator\n";
  29. x = obj.x;
  30. y = obj.y;
  31. str = new char[10];
  32. str = obj.str;
  33.  
  34. //return *this;
  35. }
  36. };
  37.  
  38. ostream& operator<<(ostream& os, const A& obj)
  39. {
  40. os <<"X="<< obj.x<<" Y="<<obj.y<<" Str="<<obj.str<<"\n";
  41. return os;
  42. }
  43.  
  44. int main()
  45. {
  46. A c(3,4,"Object C");
  47. cout<<c;
  48.  
  49. A d, e, f;
  50. d = e = f = c; //Assignment operator invoked 3 times
  51. cout<<e;
  52. }
Runtime error #stdin #stdout 0.02s 2856KB
stdin
Standard input is empty
stdout
Standard output is empty