problems accessing a function within a class.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  #include <iostream>
#include <iomanip>
using namespace 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
{
	friend class 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
{
	const double 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";
}
closed account (48T7M4Gy)
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.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <iomanip>

using namespace 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
{
    const double 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.

Topic archived. No new replies allowed.