-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemofinal.cpp
More file actions
107 lines (91 loc) · 2.45 KB
/
demofinal.cpp
File metadata and controls
107 lines (91 loc) · 2.45 KB
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
#include <iostream>
using namespace std;
// Abstract base class
class CAL_VOLUME
{
protected:
float radius; // Common data member for radius of the shape
public:
// Virtual function to input data
void getdata(float r)
{
radius = r;
}
// Pure virtual function to compute and display volume (to be implemented in derived classes)
virtual void display_volume() = 0;
};
// Derived class for Cone
class Cone : public CAL_VOLUME
{
private:
float height; // Specific data member for height of the cone
public:
// Function to input data for the cone
void getdata(float r, float h)
{
CAL_VOLUME::getdata(r); // Call base class getdata
height = h;
}
// Function to calculate and display the volume of the cone
void display_volume()
{
const float PI = 3.14159;
float volume = (PI * radius * radius * height) / 3;
cout << "Volume of the Cone: " << volume << endl;
}
};
// Derived class for Hemisphere
class Hemisphere : public CAL_VOLUME
{
public:
// Function to calculate and display the volume of the hemisphere
void display_volume()
{
const float PI = 3.14159;
float volume = (2 * PI * radius * radius * radius) / 3;
cout << "Volume of the Hemisphere: " << volume << endl;
}
};
// Derived class for Cylinder
class Cylinder : public CAL_VOLUME
{
private:
float height; // Specific data member for height of the cylinder
public:
// Function to input data for the cylinder
void getdata(float r, float h)
{
CAL_VOLUME::getdata(r); // Call base class getdata
height = h;
}
// Function to calculate and display the volume of the cylinder
void display_volume()
{
const float PI = 3.14159;
float volume = PI * radius * radius * height;
cout << "Volume of the Cylinder: " << volume << endl;
}
};
int main()
{
Cone cone;
Hemisphere hemisphere;
Cylinder cylinder;
float radius, height;
// Input for Cone
cout << "Enter the radius and height of the cone: ";
cin >> radius >> height;
cone.getdata(radius, height);
cone.display_volume();
// Input for Hemisphere
cout << "Enter the radius of the hemisphere: ";
cin >> radius;
hemisphere.getdata(radius);
hemisphere.display_volume();
// Input for Cylinder
cout << "Enter the radius and height of the cylinder: ";
cin >> radius >> height;
cylinder.getdata(radius, height);
cylinder.display_volume();
return 0;
}