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();...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
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.
Using Ruby A dentist appointment schedule validation software Implement a superclass Appointment and sub classes OneTime,...
Using Ruby A dentist appointment schedule validation software Implement a superclass Appointment and sub classes OneTime, Day and Month. An appointment has a description (for example, "Root Canal"), and dates information (you can use Date object or int Year, Int Month, Int Day). Fill an array of Appointment objects with a mixture of appointments. Write a method OccursOn inside each of the sub classes that checks whether the appointment occurs on that date (OneTime), day (Day) or month (Month). Ask...
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...
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...
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.
(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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT