forked from tridibsamanta/CPP_Beginner_to_Expert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPP036_Copy_Constructor.cpp
48 lines (43 loc) · 1.05 KB
/
CPP036_Copy_Constructor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Author: Tridib Samanta
* Created: 08.02.2020
**/
/*
Copy Constructor is a type of constructor which is used to create a copy of an already existing object of a class type.
It is usually of the form X (X&), where X is the class name.
The compiler provides a default Copy Constructor to all the classes.
*/
#include<iostream>
using namespace std;
class Samplecopyconstructor
{
private:
int x, y; //data members
public:
Samplecopyconstructor(int x1, int y1)
{
x = x1;
y = y1;
}
/* Copy constructor */
Samplecopyconstructor (const Samplecopyconstructor &sam)
{
x = sam.x;
y = sam.y;
}
void display()
{
cout<<x<<" "<<y<<endl;
}
};
/* main function */
int main()
{
Samplecopyconstructor obj1(10, 15); // Normal constructor
Samplecopyconstructor obj2 = obj1; // Copy constructor
cout<<"Normal constructor : ";
obj1.display();
cout<<"Copy constructor : ";
obj2.display();
return 0;
}