File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ // C++ program to demonstrate Copy Constructor
2
+
3
+ // Copy Constructor - It is a member function that initializes an object using another object of same class.
4
+
5
+ #include < bits/stdc++.h>
6
+ using namespace std ;
7
+
8
+ // class with a Copy Constructor
9
+ class Person {
10
+ private:
11
+ string name;
12
+ int age;
13
+ public:
14
+ // Default Constructor
15
+ Person (){
16
+ name=" abc" ;
17
+ age=20 ;
18
+ }
19
+ // Copy Constructor
20
+ Person (const Person &p1){
21
+ name=p1.name ;
22
+ age=p1.age ;
23
+ }
24
+ void display (){
25
+ cout<<" Name : " <<name<<endl;
26
+ cout<<" Age : " <<age<<endl;
27
+ }
28
+ };
29
+
30
+ int main (){
31
+ // Creating an object of class Person (initializing it using Default Constructor)
32
+ Person p1;
33
+ p1.display ();
34
+ cout<<endl;
35
+
36
+ // Creating an object of class Person (initializing it using Copy Constructor)
37
+ Person p2=p1;
38
+ p2.display ();
39
+
40
+ return 0 ;
41
+ }
You can’t perform that action at this time.
0 commit comments