This program utilizes the concepts learned in Chapters 910 i

This program utilizes the concepts learned in Chapters 9-10, i.e., Objects, this, Friend, Constructors/Destructors.

Create a class named Cylinder with a field for the radius. Be sure to include the following:

a default constructor that sets the radius to 3

a setter function to set the value of the radius

a setter function to set the value of the height

a getter function to return the value of the radius

a getter function to return the value of the height

a method to compute and return the cylinder’s surface area

Be aware that the surface area of a cylinder is 2 * pi * radius * radius + 2 * pi * radius * height. Feel free to use the value of 3.14 for pi.

After creating this class, write a second class, TestCylinder, that creates an object with a radius of 10. It should also create a second Cylinder object that uses the radius set by the default constructor. Once the objects have been created, write code to display the surface area of both cylinders in an attractive fashion.

Submit both the Cylinder file and the TestCylinder file for grading.

Sample Session:

Please enter the radius: 5
Please enter the height: 8
The cylinder with a radius of 3 and height of 8 has a surface area of:  207.24
The cylinder with a radius of 5 and height of 8 has a surface area of:  408.20

please provide output

Solution

#include <iostream>

using namespace std;

class Cylinder {
double radius;
double height;
public:
friend void printArea( Cylinder cylinder );
void setRadius( double radius );
void setHeight( double heigh );
double getRadius();
double getHeight();
  
//default constructor
Cylinder();
};

//a default constructor that sets the radius to 3
Cylinder::Cylinder(void) {
this->radius = 3;
}

void Cylinder::setRadius( double radius ) {
this->radius = radius;
}

double Cylinder::getRadius( void ) {
return this->radius;
}

void Cylinder::setHeight( double height ) {
this->height = height;
}

double Cylinder::getHeight( void ) {
return this->height;
}


void printArea( Cylinder cylinder ) {
/* Because printArea() is a friend of Cylinder, it can
directly access any member of this class */

double pi = 3.14;
double area = ( 2 * pi * cylinder.radius * cylinder.radius ) + (2 * pi * cylinder.radius * cylinder.height );
cout << \"The cylinder with a radius of \"<< cylinder.radius <<\" and height of \"<< cylinder.height <<\" has a surface area of: \" << area <<endl;
}


class TestCylinder{
public:
static void test();
};


void TestCylinder::test( void ) {

double r;
double h;
cout << \"Please enter the radius: \" << endl;
  
cin >> r;
  
cout << \"Please enter the height: \" << endl;
  
cin >> h;
  
Cylinder c1;
Cylinder c2;
  
//Cylinder c1 with default radius
c1.setHeight(h);
  
//Cylinder c2 with given height and radius
c2.setRadius(r);
c2.setHeight(h);
  
printArea(c1);
printArea(c2);
  
}

int main() {
TestCylinder::test();
}

This program utilizes the concepts learned in Chapters 9-10, i.e., Objects, this, Friend, Constructors/Destructors. Create a class named Cylinder with a field f
This program utilizes the concepts learned in Chapters 9-10, i.e., Objects, this, Friend, Constructors/Destructors. Create a class named Cylinder with a field f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site