#include <iostream>
#include <cmath>
#include <stdexcept>

const double PI = 3.14159;

class Shape {
public:
    virtual double calculateVolume() const = 0; // Virtual function to calculate volume
};

class Cube : public Shape {
private:
    double side;

public:
    Cube(double s) : side(s) {
        if (side <= 0) {
            throw std::invalid_argument("Invalid side length for cube!");
        }
    }

    double calculateVolume() const override {
        return side * side * side; // Volume of a cube formula
    }
};

class Cuboid : public Shape {
private:
    double length;
    double width;
    double height;

public:
    Cuboid(double l, double w, double h) : length(l), width(w), height(h) {
        if (length <= 0 || width <= 0 || height <= 0) {
            throw std::invalid_argument("Invalid dimensions for cuboid!");
        }
    }

    double calculateVolume() const override {
        return length * width * height; // Volume of a cuboid formula
    }
};

class Cylinder : public Shape {
private:
    double radius;
    double height;

public:
    Cylinder(double r, double h) : radius(r), height(h) {
        if (radius <= 0 || height <= 0) {
            throw std::invalid_argument("Invalid dimensions for cylinder!");
        }
    }

    double calculateVolume() const override {
        return PI * pow(radius, 2) * height; // Volume of a cylinder formula
    }
};

int main() {
    try {
        Cube cube(5.0);
        Cuboid cuboid(4.0, 6.0, 3.0);
        Cylinder cylinder(2.5, 7.0);

        std::cout << "Volume of Cube: " << cube.calculateVolume() << std::endl;
        std::cout << "Volume of Cuboid: " << cuboid.calculateVolume() << std::endl;
        std::cout << "Volume of Cylinder: " << cylinder.calculateVolume() << std::endl;
    } catch (std::invalid_argument &ex) {
        std::cout << "Exception caught: " << ex.what() << std::endl;
    }

    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: