-
Notifications
You must be signed in to change notification settings - Fork 0
/
rectangle_inheritance.cpp
117 lines (111 loc) · 2.17 KB
/
rectangle_inheritance.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
#include<iostream>
using namespace std;
class Rectangle{
float length, width, thelength, thewidth, a, p;
public:
rectangle()
{
cout<<"default constructor"<<endl;
setlength(1.0);
setwidth(1.0);
}
rectangle(float thelength, float thewidth)
{
cout<<"parameterised constructor for Rectangle class"<<endl;
setlength(thelength);
setwidth(thewidth);
}
void setlength(float thelength);
void setwidth(float thewidth);
int getlength();
int getwidth();
void area();
void perimeter();
void display();
};
void Rectangle::setlength(float thelength)
{
length = ( thelength > 0.0 && thelength < 20.0 ? thelength : 1.0 );
}
void Rectangle::setwidth(float thewidth)
{
width = ( thewidth > 0 && thewidth < 20.0 ? thewidth : 1.0 );
}
int Rectangle::getlength()
{
return length;
}
int Rectangle::getwidth()
{
return width;
}
void Rectangle::area()
{
a=length*width;
}
void Rectangle::perimeter()
{
p=(2*(length+width));
}
void Rectangle::display()
{
cout<<"\nThe length of the rectangle is: "<<length;
cout<<"\nThe width of the rectangle is: "<<width<<endl;
cout<<"\nArea of rectangle: "<<a;
cout<<"\nPerimeter of rectangle: "<<p;
}
class cuboid:public Rectangle
{
float height;
public:
void vol();
cuboid()
{
cout<<"default constructor for cuboid class"<<endl;
height=1;
}
cuboid(float theheight, float thelength, float thewidth):Rectangle(thelength, thewidth)
{
cout<<"parameterised constructor"<<endl;
setheight(theheight);
}
void setheight(float theheight);
int getheight();
};
void cuboid::setheight(float theheight)
{
height = ( theheight > 0 && theheight < 20.0 ? theheight : 1.0 );
}
int cuboid::getheight()
{
return height;
}
void cuboid:vol()
{
float p;
cout<"volume is:";
p=getlength()*getwidth()*height;
cout<<p;
}
int main()
{
float thelength, thewidth;
Rectangle r;
cout<<"\nEnter length and width of rectangle:"<<endl;
cin>>thelength>>thewidth;
r.setlength(thelength);
r.setwidth(thewidth);
r.getlength();
r.getwidth();
r.area();
r.perimeter();
r.display();
float height;
cuboid c;
cout<<"\nEnter height of cuboid:"<<endl;
cin>>theheight;
c.setheight(theheight);
c.getheight();
c.vol();
return 0;
}