Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone help me modify this program to include a member function that comput

ID: 3831807 • Letter: C

Question

Can someone help me modify this program to include a member function that computes the surface area of the cylinder?

A = 2 * pi * r * H + 2 * pi * r^2

Please have the program display the surface area in the main part of the program. Please leave the volume display as it is and use 3.1416 for pi.


#include <iostream>
using namespace std;

class Cylinder // Declare class Cylinder
{
private:
   double height, radius;
public:
   Cylinder(double, double); // Member Function (Constructor)
   double volume();
};

Cylinder::Cylinder(double ht, double r)
{
   height = ht;
   radius = r;
}

double Cylinder::volume()
{
   return 3.1416 * height * radius * radius;
}

//************************************************************************************************************

// Main part of the program
int main()
{
   Cylinder thiscylinder(7.1, 8.8);
   cout << "The volume of the cylinder is: " << thiscylinder.volume() << endl;

   system("pause");
   return 0;
}

Explanation / Answer

#include <iostream>
using namespace std;
class Cylinder // Declare class Cylinder
{
private:
double height, radius;
public:
Cylinder(double, double); // Member Function (Constructor)
double volume();
double surfaceArea();
};
Cylinder::Cylinder(double ht, double r)
{
height = ht;
radius = r;
}
double Cylinder::volume()
{
return 3.1416 * height * radius * radius;
}
double Cylinder::surfaceArea()
{
return 2 *3.1416 * height * radius + 2 *3.1416 *radius*radius;
}
//************************************************************************************************************
// Main part of the program
int main()
{
Cylinder thiscylinder(7.1, 8.8);
cout << "The volume of the cylinder is: " << thiscylinder.volume() << endl;
cout << "The surfaceArea of the cylinder is: " << thiscylinder.surfaceArea() << endl;
system("pause");
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

The volume of the cylinder is: 1727.33                                                                                                                                                                                                                                 

The surfaceArea of the cylinder is: 879.145

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote