Sunday, 10 February 2019

Write a program to implement the concept of Copy Constructor in c++

  Program to implement the concept of copy constructor. 


 1. Open a software of c++ like Turbo c++, dev

 2.  Write the code given below.

#include<iostream.h>
#include<conio.h>
class A
{
 int x;
public: A(int a)   //constructor

{
 x=a;
}

A(A &ob)            //copy constructor
{x=ob.x;            //copying value of x
}
public:void show()
{cout<<x;
}
};

void main()
{clrscr();
 A obj1(10);
 A obj2(obj1);  //calling copy constructor of class A
cout<<"\nvalue of 1st obj"<<"  ";
 obj1.show();
cout<<"\nValue of 2nd obj after copying"<<"  ";
 obj2.show();
getch();
}

 

 3. Now compile the program and output screen will be like this:

 
 

No comments:

Post a Comment

👇 🌐 Top 10 Hosting for Developers in 2025: Fast, Reliable & Affordable

 Whether you’re a beginner building your first portfolio site or a professional developer deploying client projects, choosing the right host...