#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;
}

Recommended Answers

All 7 Replies

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.

oo so i still need to create a getRadius() on class cylinder ??

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

cylinder.setHeight(h, r).

so in main i still need to ask user to input radius ??

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;
}

Mark the article solved if it solved your problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.