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();...
Please program in C++ Create two classes, HUSBAND (Family Member 1) and WIFE (Family Member 2)....
Please program in C++ Create two classes, HUSBAND (Family Member 1) and WIFE (Family Member 2). Make WIFE as friend class of HUSBAND. The structure of classes are given below. class WIFE; class HUSBAND { private:                 string Husband_fname;                 string Husband_lname;                 int Husband_income; public:                 HUSBAND(string f1, string l1, int inc):Husband_fname(f1), Husband_lname(l1), Husband_income(inc);                 HUSBAND();                 {                 //            Default initializations of data members                 }                 int getIncome();                 }; class WIFE { private:                 string Wife_fname;...
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...
If a superclass is abstract, then its subclass must implement all of the abstract methods in...
If a superclass is abstract, then its subclass must implement all of the abstract methods in the superclass. Is this statement true or false?A. trueB. false I appreciate if you add some descriptions
Base class: Polygon Derived classes: Rectangle, Triangle Make Square a derived class of Rectangle. There can...
Base class: Polygon Derived classes: Rectangle, Triangle Make Square a derived class of Rectangle. There can be several ways to represent the shapes. For example, a shape can be represented by an array of side lengths counted in the clock-wise order. For example, {2, 4, 2, 4} for a rectangle of width 2 and height 4, and {4, 4, 4, 4} for a square. You need to design your representation. Each polygon should have a function area() that returns its...
Assignment Examine the Main and Address classes. You are going to add two classes derived from...
Assignment Examine the Main and Address classes. You are going to add two classes derived from Address: BusinessAddress and PersonAddress. Create BusinessAddress class Select package home and create a new Java class called BusinessAddress Make the class extend the Address class Add two private String fields businessName and address2 Generate constructor and all getters and setters Add a printLabel() method The printLabel method should print (using System.out.println()) First line – the businessName field Second line – the address2 field if...
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.
In Ruby A dentist appointment schedule validation software Implement a superclass Appointment and sub classes OneTime,...
In 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...
Implement a superclass Person. Make two classes, Student and Instructor, that inherit from Person. A person...
Implement a superclass Person. Make two classes, Student and Instructor, that inherit from Person. A person has a name and a year of birth. A student has a major, and an instructor has a salary. Write the class declarations, the constructors, and the methods toString for all classes. Supply a test program that tests these classes and methods. Add a method Public static Measurable max(Measurable[ ] objects) to the Data class that returns the object with the largest measure. Implement...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT