Monday, July 22, 2024

Program 4 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 4 Design and implement a class to represent a Solid object. a. Apart from data members to represent dimensions, use a data member to specify the type of solid. b. Use functions to calculate volume and surface area for different solids.

 

BCA

Object Oriented Programming using C++ Practical

Madras University 

Program 4

Design and implement a class to represent a Solid object. 

a. Apart from data members to represent dimensions, use a data member to specify the type of solid.
b. Use functions to calculate volume and surface area for different solids. 

SOURCE CODE:

#include <iostream>
#include <string>
using namespace std;

class Solid {
protected:
    string type;

public:
    Solid(const string& type) : type(type) {}

    virtual ~Solid() {}

    string getType() const {
        return type;
    }

    virtual double volume() const = 0;
    virtual double surfaceArea() const = 0;
};
class Cube : public Solid {
private:
    double side;

public:
    Cube(double side) : Solid("Cube"), side(side) {}

    double volume() const override {
        return side * side * side;
    }

    double surfaceArea() const override {
        return 6 * side * side;
    }
};
class Sphere : public Solid {
private:
    double radius;

public:
    Sphere(double radius) : Solid("Sphere"), radius(radius) {}

    double volume() const override {
        return (4.0 / 3.0) * 3.14159265358979323846 * radius * radius * radius;
    }

    double surfaceArea() const override {
        return 4 * 3.14159265358979323846 * radius * radius;
    }
};
class Cylinder : public Solid {
private:
    double radius;
    double height;

public:
    Cylinder(double radius, double height) : Solid("Cylinder"), radius(radius), height(height) {}

    double volume() const override {
        return 3.14159265358979323846 * radius * radius * height;
    }

    double surfaceArea() const override {
        return 2 * 3.14159265358979323846 * radius * (radius + height);
    }
};
int main() {
    Cube cube(3);
    Sphere sphere(2);
    Cylinder cylinder(2, 5);

    Solid* solids[] = { &cube, &sphere, &cylinder };

    for (Solid* solid : solids) {
        cout << "Type: " << solid->getType() << endl;
        cout << "Volume: " << solid->volume() << endl;
        cout << "Surface Area: " << solid->surfaceArea() << endl;
        cout << std::endl;
    }

    return 0;
}

To design and implement a class to represent a Solid object in C++, we can use inheritance and polymorphism to handle different types of solids. Here, we'll create a base class Solid and derive specific classes for different types of solids, such as Cube, Sphere, and Cylinder. Each derived class will implement its own methods to calculate volume and surface area.

Here's an example of how this can be done:

Step-by-Step Implementation

  1. Base Class Solid:

    • Contains a data member to specify the type of solid.
    • Declares virtual functions to calculate volume and surface area.
  2. Derived Classes:

    • Implement specific solids like Cube, Sphere, and Cylinder.
    • Implement the methods to calculate volume and surface area.

Explanation

  1. Base Class Solid:

    • The Solid class has a protected data member type and a constructor to initialize it.
    • The class declares pure virtual functions volume() and surfaceArea(), making Solid an abstract class.
  2. Derived Classes (Cube, Sphere, Cylinder):

    • Each derived class initializes the base class with its type.
    • They override the volume() and surfaceArea() methods to provide specific implementations for each solid.
  3. Main Function:

    • Demonstrates how to create instances of different solids and call their methods to calculate volume and surface area.


OUTPUT:

Type: Cube

Volume: 27

Surface Area: 54


Type: Sphere

Volume: 33.5103

Surface Area: 50.2655


Type: Cylinder

Volume: 62.8319

Surface Area: 87.9646


No comments:

Post a Comment

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...