Question

In: Computer Science

Consider the following class definition: class circle                                  &nb

  • Consider the following class definition:

    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;

  1. Write the definitions of the member functions of the classes circle and cylinder. Identify the member functions of the class cylinder that overrides the member functions of the class circle.

Solutions

Expert Solution

#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.


Related Solutions

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...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Write a java class called circle that represents a circle. It should have following three fields:...
Write a java class called circle that represents a circle. It should have following three fields: int x (x-coordinate), int y (y-coordinate), double radius (radius of the circle). Your circle object should have following methods: public int getRadius () public int getX () public int getY () public double area () public double perimeter() public String toString() write a client program called CircleClient that creates objects of the circle class called c1 and c2. Assign values to the fields when...
1: Create a class Circle 2: Create two instances of the Circle class 1: Based on...
1: Create a class Circle 2: Create two instances of the Circle class 1: Based on Program 13-1 (see attachment Pr13-1.cpp) in the textbook, create a class name “Circle” with the following declarations (hint: you can use PI=3.14): //Circle class declaration class Circle { private: double radius; public: void setRadius(double); double getRadius() const; double getArea() const; double getPerimeter() const; }; 2: Based on Program 13-2 (see attachment Pr13-2.cpp) in the textbook, create two instances of the Circle class, pizza1 and...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle {...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle { //defining public variables public: double pi; double radius; public: //default constructor to initialise variables Circle(){ pi = 3.14159; radius = 0; } Circle(double r){ pi = 3.14159; radius = r; } // defining getter and setters void setRadius(double r){ radius = r; } double getRadius(){ return radius; } // method to get Area double getArea(){ return pi*radius*radius; } }; //main method int main() {...
Consider the following portfolio of four stocks: Security                                &nb
Consider the following portfolio of four stocks: Security                                                Weight                 Beta ABC                                        .167                        3.09 DEF                                        .25                          0.63 GHI                                        .233                        1.54 JKL                                          .35                          1.81 The 25 - year government of Canada bond has a yield of 2% The market risk premium is 5% What is the portfolio beta? What is the expected return of each security?
Note- can you rewrite the code in C++. Circle Class c++ code Write a circle class...
Note- can you rewrite the code in C++. Circle Class c++ code 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 acccssor function...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT