-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSession14A.cpp
More file actions
57 lines (40 loc) · 1.03 KB
/
Session14A.cpp
File metadata and controls
57 lines (40 loc) · 1.03 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
#include<iostream>
#include<string>
using namespace std;
// Shallow Copy Operation
class Order{
int* oid;
string customerName;
int price;
public:
void setDataForOrder(int oid, string customerName, int price){
// Dynamic Memory Allocation
this->oid = new int(oid);
this->customerName = customerName;
this->price = price;
}
void showDataForOrder(){
cout<<"==="<<*oid<<"===\n";
cout<<"oid contains: "<<oid<<"\n";
cout<<"Customer:\t"<<customerName<<"\n";
cout<<"Price:\t"<<price<<"\n";
}
};
int main(){
// Copying Object
// Copy of Object is happening automatically for us !!
Order o1;
o1.setDataForOrder(101,"John",3000);
o1.showDataForOrder();
Order o2 = o1; // Copy Object
// A Copy Constructor is created automatically by compiler
// Which performs Shallow Copy Operation
Order o3(o1); // Copy Object
o1.showDataForOrder();
o2.showDataForOrder();
o3.showDataForOrder();
cout<<"Address of o1 is: "<<&o1<<"\n";
cout<<"Address of o2 is: "<<&o2<<"\n";
cout<<"Address of o3 is: "<<&o3<<"\n";
return 0;
}