#include<iostream>
#include<iomanip>

using namespace std;

class circleType
{
public:
    void setRadius(double cRadius);
    double getRadius() ;
    double calcArea() ;
    double calcCir() ;
    void print() ;
    circleType ();
    circleType(double cRradius);
private:
        double radius;
};
void circleType::setRadius(double cRadius)
{
    radius = cRadius;
}

void circleType::print() 
{
    cout << "The radius of the circle is: " << getRadius() << endl;
    cout << "The area of the circle is: " << calcArea() << endl;
    cout << "The circumference of the circle is: " << calcCir() << endl <<endl;
}

double circleType::getRadius() 
{
    return radius ;
}

double circleType::calcArea() 
{
    double pi = 3.14159;
    return (pi * (radius * radius));
}

double circleType::calcCir() 
{
    double pi = 3.14159;
    return (2 * pi * radius);
}

circleType::circleType()     //Default rutor
{
    radius;
}

circleType::circleType(double cRadius)
{
    radius = cRadius;
}


class cylinderType: public circleType
{
public:
    void setHeight(double cHeight);
    void print() ;
    double getHeight() ;
    double calcVolume();
    double calcSurface() ;
    cylinderType();
    cylinderType(double cHeight);
private:
    double height;
};

void cylinderType::setHeight(double cHeight)
{
    height = cHeight;
}

void cylinderType::print() 
{
    cout << "The height of the cylinder is: " << getHeight() << endl;
    cout << "The radius of the cylinder base is: " << getRadius() << endl;
    cout << "The volume of the cylinder is: " << calcVolume() << endl;
    cout << "The surface area of the cylinder is: " << calcSurface() << endl;
}

double cylinderType::getHeight()  
{
    return height;
}

double cylinderType::calcVolume() 
{
    double pi = 3.14159;
    return (height * pi) * (getRadius() * getRadius());
}

double cylinderType::calcSurface() 
{
    double pi = 3.14159;
    return (2 * calcArea()) + (calcCir() * height);
}

cylinderType::cylinderType()
{
    height ;
}

cylinderType::cylinderType(double cHeight)
{
    height = cHeight;
}

int main()
{
    circleType circle;

    double r ;

    cout << "Enter the radius of the circle: ";
    cin >> r;

    circle.setRadius(r);
    circle.print();

    cylinderType cylinder;

    double h;

    cout << "Enter the height of the cylinder: ";
    cin >> h;


    cylinder.setHeight(h);
    cylinder.print();

    system("pause");
    return 0;
}

In cylinder class, getRadius(), calcArea() are of circle class, and you are using them in cylinder class, you might want to write separate functions for both of these classes. If you see, you will realize that when getRadius() is called in Cylinder class, it has value 0.

And never use system("pause") or any system calls in your program.

You can code setHeight() such that it takes the height and radius, from the circle part.

cylinder.setHeight(h, r).

you need to modify your cylinder class to take in the radius and than pass that to the circle class that makes up the cylinder class.

Cylinder::Cylinder(int h, int r) : Circle(r)
{
    height = h;
}

Edited 4 Years Ago by NathanOliver

This article has been dead for over six months. Start a new discussion instead.