-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path01Copy_constructor.cpp
More file actions
40 lines (38 loc) · 909 Bytes
/
Copy path01Copy_constructor.cpp
File metadata and controls
40 lines (38 loc) · 909 Bytes
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
/*
What is copy constructor C++?
Image result for copy constructor in c++
Copy constructor is called when a new object is created from an existing object, as a copy of the existing object. Assignment operator is called when an already initialized object is assigned a new value from another existing object.
*/#include <iostream>
using namespace std;
class student
{
int id;
public:
student()
{
}
student(int a)
{
id = a;
}
student(student &s)
{
id = s.id;
}
int display()
{
return id;
}
};
int main()
{
system("cls");
student s1(200);
student s2(s1);
student s3(s2);
student s4(s1);
cout << "Id of student s1 = " << s1.display()<<endl;
cout << "Id of student s2 =" << s1.display()<<endl;
cout << "Id of student s3 =" << s1.display()<<endl;
cout << "Id of student s4 =" << s1.display()<<endl;
}