I am trying to figure out how I can get the area function to run within the TestCylinder class. I get an error 'area' identifier not found on lines 37 and 38. If someone could point me in the right direction I would appreciate it.
#include <iostream>
#include <iomanip>
usingnamespace std;
class Cylinder //start of class
{
public:
Cylinder()//Default constructor sets radius to 3
{
radius = 3;
}
//Setter functions
void set_radius();
void set_height();
//Getter functions
int getRadius();
int getHeight();
private:
int radius;
int height;
};//end of class
class TestCylinder
{
friendclass Cylinder;
private:
Cylinder ten;
Cylinder default;
public:
TestCylinder() : ten(), default()
{
ten.set_radius();//sets radius
ten.set_height();//sets height
cout << fixed << setprecision(2) << "\n The test cylinder with a radius of " << default.getRadius() << " and a height of " << ten.getHeight() << " has a surface area of: " << area(default.getRadius(), ten.getHeight());
cout << fixed << setprecision(2) << "\n The test cylinder with a radius of " << ten.getRadius() << " and a height of " << ten.getHeight() << " has a surface area of: " << area(ten.getRadius(), ten.getHeight()) << "\n";
}
};
void Cylinder::set_radius() //sets radius from user
{
cout << "\n Please enter the radius: ";
cin >> radius;
}
void Cylinder::set_height() //sets height from user
{
cout << "\n Please enter the height: ";
cin >> height;
}
int Cylinder::getRadius()//Gets radius
{
return radius;
}
int Cylinder::getHeight()//gets height
{
return height;
}
double area(int r, int h) //calculates total surface area
{
constdouble PI = 3.14;//sets PI as a constant
double area = (2 * PI * r* r + 2 * PI*r*h);
return area;
}
int main() //main function
{
TestCylinder one;
Cylinder user, default;//creates two objects
user.set_radius();//sets user radius
user.set_height();//sets user height
cout << fixed << setprecision(2) << "\n The cylinder with a radius of " << default.getRadius() << " and a height of " << user.getHeight() << " has a surface area of: " << area(default.getRadius(), user.getHeight());
cout << fixed << setprecision(2) << "\n The cylinder with a radius of " << user.getRadius() << " and a height of " << user.getHeight() << " has a surface area of: " << area(user.getRadius(), user.getHeight()) << "\n";
}
For clarity, avoid lines of code greater than 80 characters wide.
Why have you written a TestCylinder class? By the look of it you can delete that part of your program.
'default' is a keyword in C++, you can't use it as a variable name.
area would be better called getArea(), but it must be written in the class implementation as double Cylinder::area() as you have done with the other methods.
#include <iostream>
#include <iomanip>
usingnamespace std;
class Cylinder //start of class
{
public:
Cylinder()//Default constructor sets radius to 3
{
radius = 3;
}
//Setter functions
void set_radius();
void set_height();
//Getter functions
int getRadius();
int getHeight();
double getArea();
private:
int radius;
int height;
};//end of class
void Cylinder::set_radius() //sets radius from user
{
cout << "Please enter the radius: ";
cin >> radius;
}
void Cylinder::set_height() //sets height from user
{
cout << "Please enter the height: ";
cin >> height;
}
int Cylinder::getRadius()//Gets radius
{
return radius;
}
int Cylinder::getHeight()//gets height
{
return height;
}
double Cylinder::getArea() //calculates total surface area
{
constdouble PI = 3.14;//sets PI as a constant
double area = (2 * PI * radius * radius + 2 * PI * radius * height );
return area;
}
int main() //main function
{
Cylinder pot;
pot.set_radius();//sets user radius
pot.set_height();//sets user height
cout
<< fixed << setprecision(2)
<< "The cylinder with a radius of " << pot.getRadius()
<< " and a height of " << pot.getHeight()
<< " has a surface area of: " << pot.getArea() << '\n';
return 0;
}
Please enter the radius: 6
Please enter the height: 7
The cylinder with a radius of 6 and a height of 7 has a surface area of: 489.84
Program ended with exit code: 0
Kemort,
Thank you for the feedback and tips. I now have it working, the reason for the TestCylinder is because my instructor wanted a second class that created objects using the first class. Again thank you as my compiler did not indicate default was not usable.