Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could you help me to do this task? Create a base class called Circle that will c

ID: 3632814 • Letter: C

Question

Could you help me to do this task?

Create a base class called Circle that will calculate the area of circle object. Create also a derived class called Sphere that will inherit Circle and calculate the surface area of sphere object.
Write constructors, proper member functions to perform the tasks specified in Circle and Sphere, respectively. You may decide what member functions you would like to code; however, in Sphere, you must reuse the code written in Circle. The surface area of a sphere is 4 times of area of a circle.
In the driver code main() to test your class, inside a while loop, you will ask user which area of object to be calculated (c/s), and then you will create an object of that class, and call proper member functions to compute the area and display the result. You must verify if user has entered ‘c’ or ‘s’ in either upper case or lower case. Verification will continue until the user has entered right character. This process will continue until user enters ‘n’ in ether upper case or lower case to stop. You need also to verify if user has entered ‘y’ or ‘n’ in either upper case or lower case to terminate the execution.
Your program must have the following files:
1. Inheritance.h
2. Inheritance.cpp
3. main.cpp
The following is an example of typical screen input/output:
Welcome to Area Calculation!
Please enter which object (c/s): 1[Enter]
Wrong entry, please try again…
Please enter which object (c/s): s[Enter]
Please enter the radius: 4.56

Explanation / Answer

#include <iostream>
using namespace std;

class circle
{
private:
    double radius;
public:
    circle() { radius = 0.0; }
    circle(double r) { radius = r; }
    virtual double area() { return radius*radius*3.14195; }
    virtual const char *type() { return "circle"; }
};

class sphere: public circle
{
public:
    sphere() {}
    sphere(double r) : circle(r) {}
    virtual double area() { return circle::area() * 4; }
    virtual const char *type() { return "sphere"; }
};

static double getradius()
{
    double r;
    cout << "enter radius: ";
    cin >> r;;
    return r;
}

int main()
{
    bool done = false;
    circle *object;
    double radius;
   
    while (!done)
    {
        char type;
       
        cout << "Please enter object type (c/s/q): ";
        cin >> type;
       
        switch (type)
        {
            case 'c':
            case 'C':
                object = new circle(getradius());
                break;
            case 's':
            case 'S':
                object= new sphere(getradius());
                break;
            case 'q':
            case 'Q':
                done = true;
                object = 0;
                break;
            default:
                cout << "Wrong Entry, please try again" << endl;
                object = 0;
        }
       
        if (object)
        {
            cout << "Area of " << object->type() << " is " << object->area() << endl;
            delete object, object = 0;
        }
    }
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote