Skip to content

Commit 402b942

Browse files
authored
Merge pull request #834 from pushpakumari5117/issue-832
Added the code for Copy Constructor
2 parents a8305ec + 2db4bb5 commit 402b942

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}

0 commit comments

Comments
 (0)