-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepCopy-2.cpp
More file actions
58 lines (50 loc) · 1.07 KB
/
deepCopy-2.cpp
File metadata and controls
58 lines (50 loc) · 1.07 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
#include <iostream>
using namespace std;
class Box
{
private:
int length;
int *breadth;
int height;
public:
// default constructor called
Box()
{
cout << "Default constructor called " << endl;
// dynamic memory allocation
breadth = new int;
}
//copy constructor called
Box(Box &temp){
this->length = temp.length;
int *b = new int;
*b = *(temp.breadth);
this->breadth = b;
this->height = temp.height;
}
void setDimenssion(int l, int b, int h)
{
this->length = l;
*breadth = b;
this->height = h;
}
void display()
{
cout << "length is :- " << this->length << endl;
cout << "Breadth is :- " << *breadth << endl;
cout << "Height is :- " << this->height << endl;
}
~Box()
{
cout << "Destructor called " << endl;
delete breadth;
}
};
int main()
{
Box box1;
box1.setDimenssion(10, 20, 30);
box1.display();
Box box2 = box1; // copy constructor called
box2.display();
}