-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor.cpp
More file actions
43 lines (40 loc) · 763 Bytes
/
constructor.cpp
File metadata and controls
43 lines (40 loc) · 763 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
41
42
43
#include <iostream>
using namespace std;
class Rectangle
{
public:
int l;
int b;
Rectangle()
{ // default constructor - no args passed
l = 0;
b = 0;
}
Rectangle(int x, int y)
{ // parameterized constructor - args passed
l = x;
b = y;
}
Rectangle(Rectangle &r)
{ // copy constructor - initialise an obj by another existing obj
l = r.l;
b = r.b;
}
~Rectangle()
{ // Destructor
cout << "Destructor is called";
}
};
int main()
{
Rectangle *r0 = new Rectangle();
cout << r0->l << " " << r0->b << endl;
delete r0;
Rectangle r1;
cout << r1.l << " " << r1.b << endl;
Rectangle r2(3, 4);
cout << r2.l << " " << r2.b << endl;
Rectangle r3 = r2;
cout << r3.l << " " << r3.b << endl;
return 0;
}