This C++ program utilizes Objects, this, Friend, Constructors/Destructors. Creat
ID: 3688886 • Letter: T
Question
This C++ program utilizes 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
Explanation / Answer
#include<iostream.h>
#include<conio.h>
class cylinder
{
public:
int r,h;
cylinder()
{
r=3;
}
cylinder(int ra)
{
r=ra;
}
void setrad(int ra)
{
r=ra;
}
void sethei(int he)
{
h=he;
}
int getrad()
{
return r;
}
int gethei()
{
return h;
}
double area()
{
double a=2*3.14*r*r+2*3.14*r*h;
return a;
}
};
class second:public cylinder
{
public:
second():cylinder()
{
}
second(int aa):cylinder(int ra)
{
}
};
void main()
{
double a1,a2;
cylinder o(5);
cylinder o1();
o.sethei(8);
a1=o.area();
o1.sethei(8);
a2=o1.area();
cout<<"The cylinder with a radius of 3 and height of 8 has a surface area of:"<<a1;
cout<<"The cylinder with a radius of 5 and height of 8 has a surface area of:"<<a2;
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.