Question

C++ Derive the cylinder class from the base circle class. Assume the circle class has a...

C++

Derive the cylinder class from the base circle class. Assume the circle class has a protected member variable representing the radius called radius and declared as a double with a default value of 1.0. It also has a public function called calcVal that evaluates the area of a circle as PI * radius * radius where PI is a constant 3.14. In your derived class include an additional protected member representing the length of the cylinder. Call this variable length. Have the default values for the cylinder class be 1 for both the radius and the length. For this derived cylinder class include a public function calcVal that evaluates the volume of the cylinder. (Hint: The volume of the cylinder is length * circle :: calcVal) Output: Provide a detailed description of the process that took place in developing the solution to the above-mentioned question.

Homework Answers

Answer #1

CODE:

#include <iostream>

#include <cmath>

#include <iomanip>

using namespace std;

// Creating Circle class

class Circle

{

// Declaring variable

private:

double radius;

public:

static const double PI=3.1415926;

// Zero argumented constructor

Circle()

{

}

// Parameterized constructor

Circle(double radius)

{

this->radius = radius;

}

// getters and setters

double getRadius()

{

return radius;

}

void setRadius(double radius)

{

this->radius = radius;

}

// This method will calculate the Area of the Circle

double calArea()

{

return PI * radius * radius;

}

// This function will calculate the Circumference of the Circle

double calCircumference()

{

return 2 * PI * radius;

}

};

// Creating Cylinder class deriving from Circle class

class Cylinder : public Circle

{

private:

// Declaring variable

double height;

public:

// Zero argumented constructor

Cylinder()

{

}

// Parameterized constructor

Cylinder(double radius, double height)

: Circle(radius)

{

// super(radius);

this->height = height;

}

// Setters and getters

double getHeight()

{

return height;

}

void setHeight(double height)

{

this->height = height;

}

// This function will calculate the Volume of the Cylinder

double calVolume()

{

return Circle::calArea()* height;

}

// This function will calculate the Area of the Cylinder

double calArea()

{

return calCircumference()*(height+getRadius()) ;

}

};

int main()

{

// declaring varibles

double radius1, height1, radius2, height2;

cout << "Test Circle class constructor and area() function:" << endl;

  

Circle cir1(10);

cout<<"Circle cir1: radius="<<cir1.getRadius()<<endl;

cout<<"area of cir1 = "<<cir1.calArea()<<endl;

  

cout<<endl;

cout << "Test set and get methods for class Circle:" << endl;

Circle cir2(12);

cout<<"Circle cir2: radius="<<cir2.getRadius()<<endl;

cout<<"get data members for cir2: "<<cir2.getRadius()<<endl;

  

cout<<endl;

cout << "Test Cylinder class constructor, area(), and volume() functions:" << endl;

// Creating the CylinderType class object

Cylinder cyl1(20, 30);

cout << "Cylinder cyl1: radius="<<cyl1.getRadius()<<" height="<<cyl1.getHeight()<<endl;

cout<<"area of cyl1 = "<<cyl1.calArea()<<endl;

cout<<"volume of cyl1 = "<<cyl1.calVolume()<<endl;

  

  

  

// Creating the CylinderType class object

Cylinder cyl2(40,50);

cout<<endl;

cout << "Test set and get methods for class Cylinder:" << endl;

cout<<"Cylinder cyl2: radius="<<cyl2.getRadius()<<" height="<<cyl2.getHeight()<<endl;

cout<<"get data members for cyl2: "<<cyl2.getRadius()<<", "<<cyl2.getHeight()<<endl;

  

return 0;

}

____________________

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
java Create a program that defines a class called circle. Circle should have a member variable...
java Create a program that defines a class called circle. Circle should have a member variable called radius that is used to store the radius of the circle. Circle should also have a member method called calcArea that calculates the area of the circle using the formula area = pi*r^2. Area should NOT be stored in a member variable of circle to avoid stale data. Use the value 3.14 for PI. For now, make radius public and access it directly...
In this assignment, you will be building upon the work that you did in Lab #5A...
In this assignment, you will be building upon the work that you did in Lab #5A by expanding the original classes that you implemented to represent circles and simple polygons. Assuming that you have completed Lab #5A (and do not move on to this assignment unless you have!), copy your Circle, Rectangle, and Triangle classes from that assignment into a new NetBeans project, then make the following changes: Create a new Point class containing X and Y coordinates as its...
IN C++ - most of this is done it's just missing the bolded part... Write a...
IN C++ - most of this is done it's just missing the bolded part... Write a program that creates a class hierarchy for simple geometry. Start with a Point class to hold x and y values of a point. Overload the << operator to print point values, and the + and – operators to add and subtract point coordinates (Hint: keep x and y separate in the calculation). Create a pure abstract base class Shape, which will form the basis...
In this assignment, you’ll make an inventory system for a store’s items, including produce and books....
In this assignment, you’ll make an inventory system for a store’s items, including produce and books. The starter program is an inventory system for only produce. 1. Include the price of an item by adding to the Item class the protected data member int priceInDollars that stores the price in dollars of an individual item. Write a public function SetPrice with a single int parameter prcInDllrs and returns nothing. SetPrice assigns the value of prcInDllrs to priceInDollars. Modify the AddItemToInventory...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). Details and Requirements Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...