-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHierarchical_Inheritance.cpp
66 lines (61 loc) · 2.64 KB
/
Hierarchical_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
#include <iostream>
using namespace std;
/*———————————————————————————————————————————————————————————————————————————*/
class Shape{ // base class
public:
int x, y;
void get_data(int n, int m) {
x = n;
y = m;
}
};
/*———————————————————————————————————————————————————————————————————————————*/
class Rectangle : public Shape { // derived class 1
public:
int area_rect() {
int area = x * y;
return area;
}
};
/*———————————————————————————————————————————————————————————————————————————*/
class Triangle : public Shape { // derived class 2
public:
int triangle_area() {
float area = (0.5) * x * y;
return area;
}
};
/*———————————————————————————————————————————————————————————————————————————*/
class Square : public Shape { // derived class 3
public:
int square_area() {
float area = x * x;
return area;
}
};
/*———————————————————————————————————————————————————————————————————————————*/
int main() {
Rectangle r;
Triangle t;
Square s;
int length, breadth, base, height, side;
//area of a Rectangle
cout << "Enter the length and breadth of a Rectangle : ";
cin >> length >> breadth;
r.get_data(length, breadth);
int rect_area = r.area_rect();
cout << "Area of the rectangle = " << rect_area << " sq. units\n\n";
//area of a triangle
cout << "Enter the base and height of the Triangle : ";
cin>>base>>height;
t.get_data(base,height);
float tri_area = t.triangle_area();
cout << "Area of the triangle = " << tri_area << " sq. units\n\n";
//area of a Square
cout << "Enter the length of one side of the square : ";
cin >> side;
s.get_data(side, side);
int sq_area = s.square_area();
cout << "Area of the square = " << sq_area << " sq. units\n\n";
return 0;
}