-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_in_Class.cpp
126 lines (85 loc) · 1.88 KB
/
function_in_Class.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include<iostream>
using namespace std;
class rectangle
{
private:
int length,breadth;
public:
rectangle();
rectangle(int l,int b);
rectangle(rectangle &r);
void setlength(int l);
void setbreadth(int b);
int getlength(){ return length;}
int getbreadth(){ return breadth;}
int area();
int perimeter();
bool isSquare();
~rectangle();
};
int main()
{
//rectangle r;//default constructor is created by machine and get data member value is garbage but if we declare any user constructor then that constructor run
//cout<<r.area();
rectangle r1; //when constructor not defined by user then by default constructor object of data member value is garbage but now it is non-parameterized costructor are not take any parameter and set value in constructor length and breadth is 1
cout<<"Area of rectangle (non -parameterized constructor) "<<r1.area()<<endl;//1 value
rectangle r2(7,8);
cout<<"Area of rectangle (parameterized constructor) "<<r2.area()<<endl;//56
rectangle r3(r2);
cout<<"Area of rectangle (copy constructor ) "<<r3.area()<<endl;//56
if(r2.isSquare())
cout<<"yes it is square"<<endl;
else
cout<<"No it is not square"<<endl;
return 0;
}
//constructor
rectangle::rectangle()
{
length=1;
breadth=1;
}
rectangle::rectangle(int l,int b)
{
setlength(l);;
setbreadth(b);
}
rectangle::rectangle(rectangle &r)
{
length=r.length;
breadth=r.breadth;
}
//mutators
void rectangle::setlength(int l)
{
if(l>0)
length=l;
else
length=0;
}
void rectangle::setbreadth(int b)
{
if(b>0)
breadth=b;
else
breadth=0;
}
//facilitators
int rectangle:: area()
{
return length*breadth;
}
int rectangle::perimeter()
{
return 2*(length+breadth);
}
//inspector
bool rectangle::isSquare()
{
return length==breadth;
}
//destructor
rectangle::~rectangle()
{
cout<<"Rectangle destroy"<<endl;
}