In: Computer Science
there is a superclass SHAPE, and 2 derived classes CIRCLE and CYLINDER.
The member methods of class Shape:
Shape(radius:double, height:double): Overloaded constructor. This constructor initializes the member attributes of the class radius and height to the value of its parameters and also initializes the constant variable PI to 3.142. Besides, it also sets the value of radius and height to default value 0 if no parameter is provided.
setRadius(r int):This function sets the value of member attribute radius to the value of the parameter r.
setHeight(h int):This function sets the value of member attribute height to the value of the parameter h.
getRadius():This function returns the value of member attribute radius.
getHeight():This function returns the value of member attribute height.
The member methods of class Circle:
Circle(radius:double): Overloaded constructor. This constructor initializes the superclass’s member attributes radius by calling the constructor of superclass Shape.
getArea():This function returns the area value of a circle. Given the equation as: (A = π r²)
The member methods of class Cylinder:
Cylinder (radius:double, height:double): Overloaded constructor. This constructor initializes the superclass’s member attributes radius and height by calling the constructor of superclass Shape.
getArea():This function returns the area value of a cylinder. Given the equation as: 2(pi)radius(height) + 2(pi)radius squared.
Given the code below:
#include <iostream>
using namespace std;
int main(){
Circle c(5.2);
cout << "The radius of circle is " << c.getRadius() << "cm" << endl;
cout << "The total area of circle is " << c.getArea() << "cm2" << endl;
Cylinder cylin(5.2, 7.4);
cout << "The radius and height of cylinder is " << cylin.getRadius() << "cm and " <<
cylin.getHeight() << "cm" << endl;
cout << "The total area of cylinder is " << cylin.getArea() << "cm2" << endl;
return 0;
}
Q1.1) Define the class Shape here:
Q1.2) Define the class Circle here:
Q1.3) Define the class Cylinder here:
If something is not clear please ask me in the comments. The code is given below.
CODE:
#include <iostream>
#include <cmath>
using namespace std;
const double pi = 22.0/7; // defining and intializing globally
class Shape{
private:
double radius;
double height;
public:
Shape(){ // default
constructor
radius=0;
//initializing values to zero
height=0;
}
Shape(double r , double h){
radius=r;
height=h;
}
void setRadius(double r){
radius =
r;
}
void setHeight(double h){
height=h;
}
double getRadius(){
return
radius;
}
double getHeight(){
return
height;
}
};
class Circle : public Shape{
public:
Circle(double r):Shape(r,0){ //
calling the super class constructor
}
double getArea(){
return
pi*pow(getRadius(),2);
}
};
class Cylinder : public Shape{
public:
Cylinder(double r, double
h):Shape(r,h){
}
double getArea(){
return
2*pi*pow(getRadius(),2)*getHeight();
}
};
int main(){
Circle c(5.2);
cout << "The radius of circle is " << c.getRadius() << "cm" << endl;
cout << "The total area of circle is " << c.getArea() << "cm2" << endl;
Cylinder cylin(5.2, 7.4);
cout << "The radius and height of cylinder is " << cylin.getRadius() << "cm and " <<
cylin.getHeight() << "cm" << endl;
cout << "The total area of cylinder is " << cylin.getArea() << "cm2" << endl;
return 0;
}
OUTPUT & CODE SCREENSHOT
Please ask if something is not clear
Please Thumbs Up
Thank You