-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program56.cpp
75 lines (66 loc) · 1.87 KB
/
Program56.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
/*PROGRAM USING HIERARCHICHAL INHERITANCE*/
#include<iostream>
using namespace std;
class Shape // shape class -> base class
{
public:
int x, y;
void get_data(int n, int m)
{
x = n;
y = m;
}
};
class Rectangle : public Shape // inherit shape class (fixed typo)
{
public:
int rect_area()
{
int area = x * y;
return area;
}
};
class Triangle : public Shape // inherit shape class
{
public:
float triangle_area() // corrected function name from tri_area to triangle_area
{
float area = 0.5 * x * y;
return area;
}
};
class Square : public Shape // inherit shape class
{
public: // Added public access specifier for function visibility
int sq_area() // corrected function name from sq_area to sq_area
{
int area = 4 * x;
return area;
}
};
int main()
{
Rectangle r; // Fixed capitalization from Rectangle to r
Triangle t;
Square s;
int length, breadth, base, height, side;
// area of a rectangle
std::cout << "Enter the length and breadth of a rectangle:";
cin >> length >> breadth;
r.get_data(length, breadth);
int rect_area = r.rect_area();
std::cout << "Area of the rectangle = " << rect_area << std::endl;
// area of a triangle
std::cout << "Enter the base and height of a triangle:";
cin >> base >> height;
t.get_data(base, height); // Changed r.get_data to t.get_data
float tri_area = t.triangle_area(); // Changed tri_area to triangle_area
std::cout << "Area of the triangle = " << tri_area << std::endl;
// area of a square
std::cout << "Enter the side length of a square:"; // Fixed typo in the output message
cin >> side;
s.get_data(side, side);
int sq_area = s.sq_area(); // Changed _area to sq_area
std::cout << "Area of a square = " << sq_area << std::endl;
return 0;
}