forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
31aug6.cpp
36 lines (35 loc) · 819 Bytes
/
31aug6.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
// Code to understand the working of the copy constructor.
#include <iostream>
using namespace std;
class person
{
private:
string name;
int age;
public:
person(string person_name, int person_age)
{
cout << "Constructor for both name and age is called" << endl;
name = person_name;
age = person_age;
}
person(const person &obj)
{
cout << "Copy constructor is called" << endl;
name = obj.name;
age = obj.age;
}
void display()
{
cout << "Name of current object : " << name << endl;
cout << "Age of current object : " << age << endl;
cout << endl;
}
};
int main()
{
person obj1("First person", 25);
obj1.display();
person obj2(obj1);
obj2.display();
};