In: Computer Science
class circle
class cylinder: public circle
{
{
public:
public:
void print() const;
void print() const;
void setRadius(double);
void setHeight(double);
void setCenter(double, double);
double getHeight();
void getCenter(double&, double&);
double volume();
double
getRadius();
double area();
double area();
circle();
cylinder();
circle(double, double, double);
cylinder(double, double,
double, double);
private:
private:
double xCoordinate;
double height;
double yCoordinate;
}
double radius;
}
Suppose that you have the declaration:
cylinder newCylinder;
#include <iostream>
using namespace std;
class circle
{
public:
void print() const;
void setRadius(double);
void setCenter(double, double);
void getCenter(double&, double&);
double getRadius();
double area();
circle();
circle(double, double, double);
private:
double xCoordinate;
double yCoordinate;
double radius;
};
void circle::print() const
{
cout<<"\n Circle radius = "<<radius
<< " at
("<<xCoordinate<<","<<yCoordinate<<")"
;
}
void circle::setRadius(double radius )
{
this->radius = radius;
}
void circle::setCenter(double x, double y)
{
xCoordinate = x;
yCoordinate = y;
}
void circle::getCenter(double &x, double &y)
{
x = xCoordinate;
y = yCoordinate;
}
double circle::getRadius()
{
return radius;
}
double circle::area()
{
return 3.14*radius*radius;
}
circle::circle()
{
}
circle::circle(double radius, double x, double y)
{
this->radius = radius;
xCoordinate = x;
yCoordinate = y;
}
class cylinder: public circle
{
public:
void print() const;
void setHeight(double);
double getHeight();
double volume();
double area();
cylinder();
cylinder(double, double,double, double);
private:
double height;
};
void cylinder::print() const // override function
{
circle::print();
cout<<"\n Height = "<< height;
}
void cylinder::setHeight(double height)
{
this->height =
height;
}
double cylinder::getHeight()
{
return height;
}
double cylinder::volume()
{
return area() * height;
}
double cylinder::area() // override
function
{
return 2*3.14 *
getRadius()*(getRadius() + height);
}
cylinder::cylinder()
{
}
cylinder::cylinder(double radius, double x,
double y, double height) : circle(radius,x,y)
{
this->height = height;
}
int main() {
cylinder newCylinder(5.6,4.5,6.7,8.6);
newCylinder.print();
cout<<"\nArea =
"<<newCylinder.area();
cout<<"\nVolume =
"<<newCylinder.volume();
return 0;
}
Output:
Circle radius = 5.6 at (4.5,6.7)
Height = 8.6
Area = 499.386
Volume = 4294.72
Do ask if any doubt. Please upvote.