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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.