pgrm_017.cpp
Problem Statement
pgrm_017.cpp
Copy Constructor
Source Code
cpp
#include<iostream>
using namespace std;
class A {
private:
int x;
double y;
public:
A(int a, double b) {
x = a; y = b;
}
A(A & ob) {
// Copy constructor
x = ob.x;
y = ob.y;
}
void display() {
cout << "x = " << x << " y = " << y << endl;
}
};
int main() {
A obj(20, 23.5);
cout << "obj: ";
obj.display();
A obj2(obj);
cout << "obj2: ";
obj2.display();
return 0;
}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
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