C++
Derive the cylinder class from the base circle class. Assume the circle class has a protected member variable representing the radius called radius and declared as a double with a default value of 1.0. It also has a public function called calcVal that evaluates the area of a circle as PI * radius * radius where PI is a constant 3.14. In your derived class include an additional protected member representing the length of the cylinder. Call this variable length. Have the default values for the cylinder class be 1 for both the radius and the length. For this derived cylinder class include a public function calcVal that evaluates the volume of the cylinder. (Hint: The volume of the cylinder is length * circle :: calcVal) Output: Provide a detailed description of the process that took place in developing the solution to the above-mentioned question.
CODE:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
// Creating Circle class
class Circle
{
// Declaring variable
private:
double radius;
public:
static const double PI=3.1415926;
// Zero argumented constructor
Circle()
{
}
// Parameterized constructor
Circle(double radius)
{
this->radius = radius;
}
// getters and setters
double getRadius()
{
return radius;
}
void setRadius(double radius)
{
this->radius = radius;
}
// This method will calculate the Area of the Circle
double calArea()
{
return PI * radius * radius;
}
// This function will calculate the Circumference of the Circle
double calCircumference()
{
return 2 * PI * radius;
}
};
// Creating Cylinder class deriving from Circle class
class Cylinder : public Circle
{
private:
// Declaring variable
double height;
public:
// Zero argumented constructor
Cylinder()
{
}
// Parameterized constructor
Cylinder(double radius, double height)
: Circle(radius)
{
// super(radius);
this->height = height;
}
// Setters and getters
double getHeight()
{
return height;
}
void setHeight(double height)
{
this->height = height;
}
// This function will calculate the Volume of the Cylinder
double calVolume()
{
return Circle::calArea()* height;
}
// This function will calculate the Area of the Cylinder
double calArea()
{
return calCircumference()*(height+getRadius()) ;
}
};
int main()
{
// declaring varibles
double radius1, height1, radius2, height2;
cout << "Test Circle class constructor and area() function:" << endl;
Circle cir1(10);
cout<<"Circle cir1: radius="<<cir1.getRadius()<<endl;
cout<<"area of cir1 = "<<cir1.calArea()<<endl;
cout<<endl;
cout << "Test set and get methods for class Circle:" << endl;
Circle cir2(12);
cout<<"Circle cir2: radius="<<cir2.getRadius()<<endl;
cout<<"get data members for cir2: "<<cir2.getRadius()<<endl;
cout<<endl;
cout << "Test Cylinder class constructor, area(), and volume() functions:" << endl;
// Creating the CylinderType class object
Cylinder cyl1(20, 30);
cout << "Cylinder cyl1: radius="<<cyl1.getRadius()<<" height="<<cyl1.getHeight()<<endl;
cout<<"area of cyl1 = "<<cyl1.calArea()<<endl;
cout<<"volume of cyl1 = "<<cyl1.calVolume()<<endl;
// Creating the CylinderType class object
Cylinder cyl2(40,50);
cout<<endl;
cout << "Test set and get methods for class Cylinder:" << endl;
cout<<"Cylinder cyl2: radius="<<cyl2.getRadius()<<" height="<<cyl2.getHeight()<<endl;
cout<<"get data members for cyl2: "<<cyl2.getRadius()<<", "<<cyl2.getHeight()<<endl;
return 0;
}
____________________
Output:
Get Answers For Free
Most questions answered within 1 hours.