Untitled

 avatar
unknown
plain_text
5 months ago
1.7 kB
2
Indexable
#include <iostream>
#include <cmath>
using namespace std;

// Abstract Base Class
class CAL_AREA {
protected:
    float x; // Generic dimension 1 (e.g., radius, etc.)
    float y; // Generic dimension 2 (e.g., height, etc.)

public:
    void getdata(float a, float b = 0) {
        x = a;
        y = b;
    }

    virtual void display_volume() = 0; // Pure virtual function
};

// Derived Class: Cone
class cone : public CAL_AREA {
public:
    void display_volume() override {
        float volume = (1.0 / 3.0) * M_PI * x * x * y; // Volume of a cone = (1/3)*pi*r^2*h
        cout << "Volume of Cone: " << volume << endl;
    }
};

// Derived Class: Hemisphere
class hemisphere : public CAL_AREA {
public:
    void display_volume() override {
        float volume = (2.0 / 3.0) * M_PI * x * x * x; // Volume of a hemisphere = (2/3)*pi*r^3
        cout << "Volume of Hemisphere: " << volume << endl;
    }
};

// Derived Class: Cylinder
class cylinder : public CAL_AREA {
public:
    void display_volume() override {
        float volume = M_PI * x * x * y; // Volume of a cylinder = pi*r^2*h
        cout << "Volume of Cylinder: " << volume << endl;
    }
};

int main() {
    float radius, height;

    // Cone
    cone c;
    cout << "Enter radius and height of the cone: ";
    cin >> radius >> height;
    c.getdata(radius, height);
    c.display_volume();

    // Hemisphere
    hemisphere h;
    cout << "\nEnter radius of the hemisphere: ";
    cin >> radius;
    h.getdata(radius);
    h.display_volume();

    // Cylinder
    cylinder cyl;
    cout << "\nEnter radius and height of the cylinder: ";
    cin >> radius >> height;
    cyl.getdata(radius, height);
    cyl.display_volume();

    return 0;
}
Editor is loading...
Leave a Comment