-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshallowCopy.cpp
More file actions
62 lines (49 loc) · 1.13 KB
/
shallowCopy.cpp
File metadata and controls
62 lines (49 loc) · 1.13 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Shallow copy
#include<iostream>
using namespace std;
class Person {
int *roll;
int age;
public:
// default constructor
Person() {
cout << "Constructor called." << endl;
}
// parameterized constructor
Person (int rollVal, int age) {
this->age = age;
roll = new int;
*roll = rollVal;
}
// shallow copy - points to same address
Person(Person& p) {
this->age = p.age;
this->roll = p.roll;
}
// member functions
int getAge() {
return this->age;
}
int getRoll() {
return *(this->roll);
}
void setRoll(int r) {
*roll = r;
}
void display() {
cout << "Age: " << this->age << endl;
cout << "Roll number: " << *roll << endl;
}
};
int main() {
Person anshika(3,20);
cout << "Initially anshika's details: " << endl;
anshika.display();
Person anshi(anshika);
anshi.setRoll(4);
cout << "Anshi's details on: " << endl;
anshi.display();
cout << "After updating Anshi's roll no., Anshika's details: " << endl;
anshika.display();
return 0;
}