Question

In: Computer Science

there is a superclass SHAPE, and 2 derived classes CIRCLE and CYLINDER. The member methods of...

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:

Solutions

Expert Solution

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


Related Solutions

Here I'm using "person" as an abstract superclass or parent class, and "Student" as a derived/child...
Here I'm using "person" as an abstract superclass or parent class, and "Student" as a derived/child class. // File name: Person.h // Person is the base, or parent for chapter11 #pragma once #include <iostream> #include <string> using namespace std; class Person { private:    string fName;    string lName;    int areaCode;    int phone; public:    Person();    Person(string, string);    void setFirst(string);    void setLast(string);    void setPhoneNumber(int, int);    string getFirstlast();    string getLastFirst();    string getPhoneNumber();...
2. Explain the differences between a Wide Flange Member and a Standard American-I shape: Remember the...
2. Explain the differences between a Wide Flange Member and a Standard American-I shape: Remember the term ‘beefy’ as a hint. 3. Why might built up members be more economical than long span members? 4. List 3 factors that should be considered for erecting steel? 5. If beams are not being used for the roof structure, name and describe at least one other popular type of steel/ metal construction.
A cylindrical wire is bent into the shape of the quarter circle C given by ?...
A cylindrical wire is bent into the shape of the quarter circle C given by ? = ? ???(?) , ? = ? ???(?) , ? = ?, for 0 ≤ ? ≤ ?/2. The density function is ?(?, ?, ?) = ?? ? . Determine the mass and center of mass of the wire.
In general, large bacteria have the shape of a cylinder of a radius r and a...
In general, large bacteria have the shape of a cylinder of a radius r and a very long length h, where h >> r. This is due to the fact that a large cylindrical bacterium can feed itself more efficiently than a large spherical bacterium. To check whether this is true or not, assume there are two bacteria, one cylindrical (with h >> r) and one spherical with radius R where they have the same volume, and thus the same...
Describe the methods for converting inherited types, or 8) superclass/subclasses instances. Include the four options for...
Describe the methods for converting inherited types, or 8) superclass/subclasses instances. Include the four options for conversion and describe the tradeoffs of using each one in terms of total/partial participation and support for overlapping/disjoint subtypes. Then, describe how to represent 9) union types.
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash()...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash() method to indicate that pants are dry clean only. Include additional method hang() Add to your driver/tester file to make and print new pants objects and test it. Shirt class Include additional property of type string called sleeves. Write necessary constructors For sleeves only allow it to be set to {"short", "long", "none"} For size, only allow {"S","M","L"} Override the wash() method to indicate...
(True and False) Circle the correct answers. (True or False)   A continuous metric is one derived...
(True and False) Circle the correct answers. (True or False)   A continuous metric is one derived by counting something. (True or False) Categorical data is nominal data. (True or False) The tracking signal is the MAD divided by the running sum or errors. ((True or False) Ordinal data are ranked according to some relationship to one another. (True or False) Observations that radically different than the rest are called outlaws. (True or False) The second quartile and median are the...
A thermos bottle in the shape of a cylinder has an inner radius of 4.0 cm,...
A thermos bottle in the shape of a cylinder has an inner radius of 4.0 cm, an outer radius of 4.5 cm, and a lengt of 30cm. The insulating walls have a thermal conductivity equal to 2.0 x 10^-5 cal/(sec-cm celsius). One liter of hot coffee at 90 degrees Celsius is poured into the bottle. If the outside wall remains at 20 degrees Celsius, how long does it take for the coffee to cool to 50 degrees Celsius?
A Gaussian surface in the shape of a right circular cylinder with end caps has a...
A Gaussian surface in the shape of a right circular cylinder with end caps has a radius of 16.4 cm and a length of 68.1 cm. Through one end there is an inward magnetic flux of 36.6 μWb. At the other end there is a uniform magnetic field of 1.40 mT, normal to the surface and directed outward. What are the (a) magnitude and (b) direction (inward or outward) of the net magnetic flux through the curved surface?
A Gaussian surface in the shape of a right circular cylinder with end caps has a...
A Gaussian surface in the shape of a right circular cylinder with end caps has a radius of 10.3 cm and a length of 64.6 cm. Through one end there is an inward magnetic flux of 19.0 μWb. At the other end there is a uniform magnetic field of 2.18 mT, normal to the surface and directed outward. What are the (a) magnitude and (b) direction (inward or outward) of the net magnetic flux through the curved surface?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT