Write A Program For Copy Constructor In C++


Write A Program For Copy Constructor In C++

#include<iostream>
#include<conio.h>

using namespace std;

class Example {
   // Member Variable Declaration
   int a, b;
public:

   //Normal Constructor with Argument

   Example(int x, int y) {
      // Assign Values In Constructor
      a = x;
      b = y;
      cout << "\nIm Constructor";
   }

   //Copy Constructor with Obj Argument

   Example(const Example& obj) {
      // Assign Values In Constructor
      a = obj.a;
      b = obj.b;
      cout << "\nIm Copy Constructor";
   }

   void Display() {
      cout << "\nValues :" << a << "\t" << b;
   }
};

int main() {
   //Normal Constructor Invoked
   Example Object(10, 20);

   //Copy Constructor Invoked - Method 1
   Example Object2(Object);

   //Copy Constructor Invoked - Method 2
   Example Object3 = Object;

   Object.Display();
   Object2.Display();
   Object3.Display();
   // Wait For Output Screen
   getch();
   return 0;
}

OUTPUT:-
Im Constructor                                                                                                                  
Im Copy Constructor                                                                                                             
Im Copy Constructor                                                                                                             
Values :10      20                                                                                                              
Values :10      20                                                                                                              
Values :10      20 


Comments