C++ debugging help

I have an assignment where I need to find the area of a cylinder. I am only able to get the output of the circumference and the area, but they are both 0. Currently I am not getting an output for the height, surface area and volume.

Here is my code


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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <iomanip>
using namespace std;
const double PI = 3.14159;
class Circle
{
private:
        double radius;
        double x;
        double y;
        double circumference;
        double cArea;
public:
Circle()
{
radius = 0;
x = 0;
y = 0;
circumference = 0;
cArea = 0;
}
        int setRadius()
{
cout << "Enter radius: ";
cin >> radius;
         if (radius < 1)
{
cout << "Please eneter a positive number: ";
cin >> radius;
}
         return radius;
}
        void setX()
{
cout << "Enter X cordinate: ";
cin >> x;
}
        void setY()
{
cout << "Enter y Coirdinate: ";
cin >> y;
}
        void setCircumference()
{
circumference = PI*(radius*radius);
cout << fixed << showpoint << setprecision(2);
cout << "Circumference is: " <<circumference << endl;
}
        void setCArea()
{
cArea = PI*(radius*radius);
cout << fixed << showpoint << setprecision(2);
cout << " Area is: " <<cArea << endl;
}
};
class Cylinder:public Circle
{
private:
        double height;
        double volume;
        double area;
        int rad;
public:
        Cylinder()
        {
                height = 0.0;
                volume = 0.0;
                area = 0.0;
                rad = 0.0;
        }


        void setheight()
        {
                cout << "What is the height: " << endl;
          height = volume/(PI*(radius*radius));
cout << fixed << showpoint << setprecision(2);
cout << "Circumference is: " <<circumference << endl;

                cin >> height;
        }

        void setrad()
        {
                cout << "Please enter the radius" << endl;
                cin >> rad;
        }

        void setvolume()
        {
                volume = height * PI*(rad*rad);
                cout << fixed << showpoint << setprecision(2);
                cout << "Volume is : " << volume << endl;
        }

        void setarea()
        {
                area = 2*PI*(rad*rad) + 2*PI*(rad*height);
                cout << fixed << showpoint << setprecision(2);
                cout << "Surface area is : " << area << endl;
        }
};

int main()
{
        Circle shape1;
        Cylinder shape2;

        shape1.setX();
        shape1.setY();
        shape1.setCircumference();
        shape1.setCArea();
        cout << endl;
    std::cin.get();
    return 0;
}

remove all the i/o of your class, ask for parameters and return values instead.

¿what does `set' mean to you? because in some parts you use it to give a value to a variable and in others you use it to retreive said value.
also, not everything should be stored in a member variable.

because of inheritance, Cylinder already has a `radius' member
you don't actually need to access it, just use the interface provided by Circle (the parent class) to retrieve the circumference and area


> I am only able to get the output of the circumference and the area, but they are both 0
yes, because your radius is 0
Hello Alex4321,
A couple of notes:

The line cout << fixed << showpoint << setprecision(2); only needs done once. I usually put this near the beginning of"main". It is not needed before every "cout" that outputs a double.

A class is "private by default, so starting a class with "private:" is a bit redundant.

When you write class Cylinder : public Circle this only gives you access to the "public" section of the class and to use this with height = volume / (PI * (radius * radius));. The public member function of "Cylinder" has access to its private member variables, but not the private member of "Circle".

"Cylinder" would need to call a public member function of "Circle" to get, or ste, the private member variables of "Circle". So the line of code height = volume / (PI * (radius * radius)); should look something more like this height = volume / (PI * (getRadius() * getRadius()));.

Your IDE.compiler should have flagged errors similar to the shell program here:

 In member function 'void Cylinder::setheight()':
8:16: error: 'double Circle::radius' is private
76:32: error: within this context

8:16: error: 'double Circle::radius' is private
76:39: error: within this context

11:16: error: 'double Circle::circumference' is private
78:32: error: within this context


Each second line is trying to say that you can not access a private member variable of another class directly.

Something I noticed when I started to check the program. In "Circle" you have the function "int setRadius()". The function is OK for the most part, but since you are working with "double"s you might want to say if (radius < 1.0). Next the function returns an "int", but the variable is a "double". Trying to put a "double" into an "int" you will drop the decimal part of the number and only return the whole number. Since this is a set function it should not be returning anything in the first place.

Now that I have the program compiling I can go through it to see what is happening.

Andy
> When you write class Cylinder : public Circle this only gives you access to
> the "public" section of the class and to use this with `height = volume / (PI * (radius * radius));'.
> The public member function of "Cylinder" has access to its private member
> variables, but not the private member of "Circle".

> "Cylinder" would need to call a public member function of "Circle" to get, or ste,
> the private member variables of "Circle".
> So the line of code height = volume / (PI * (radius * radius));
> should look something more like this height = volume / (PI * (getRadius() * getRadius()));.

class Cylinder: public Circle
Cylinder inherits all the members from Circle. It has access to the public members (like anyone else) and also to the protected members
it cannot access the private members, but those do form part of its state
the private members are responsibility of the base class, so the base class should manage them.

¿so how to compute the volume then? use the interface provided by the base class, no need to expose anything more
1
2
3
4
5
6
7
double Cylinder::volume() const{
	//all these are equivalent
	//double v = height * Circle::area();
	//double v = height * this->area();
	double v = height * area();
	return v;
}



one could argue that a cylinder is not a circle, it has two circles as base and top
I don't know what X and Y are all about so I ignored them.

A lot of simplification is possible FWIW

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <iomanip>

using namespace std;

class Circle
{
private:
    double radius = 0;
    const double PI = 3.14159; // JUST FOR DEMO - CYLINDER DOESN'T NEED PI
    
public:
    Circle(double aRadius)
    {
        radius = aRadius;
    }
    
    double getRadius()
    {
        return radius;
    }
    
    double getCircumference()
    {
        return 2 * PI * radius;
    }
    
    double getArea()
    {
        return PI * radius * radius;
    }
};

class Cylinder // NO INHERITANCE NEEDED
{
private:
    double height = 0;
    double radius = 0;
public:
    Cylinder(double aRadius, double aHeight)
    {
        height = aHeight;
        radius = aRadius;
    }
    
    double getRadius()
    {
        return radius;
    }
    
    double getHeight()
    {
        return height;
    }
    
    double getArea()
    {
        Circle end(radius);
        
        double area = 0;
        area = 2 * end.getArea();
        area += end.getCircumference() * height; // <-- EDIT
        
        return area;
    }
    
    double getVolume()
    {
        Circle end(radius);
        
        double volume = 0;
        volume = end.getArea() * height;
        
        return volume;
    }
};

int main()
{
    // INPUT - cin's GO HERE IF REQUIRED
    double rad = 2.5;
    double ht = 3.7;
    
   // OUTPUT - GO HERE UNLESS CLASSES HAVE << operator (OR SIMILAR VIA <string>'s)
    Circle shape1(rad);
    cout
    << "Radius: " <<  shape1.getRadius()
    << " Circumference: " << shape1.getCircumference()
    << " Area: " << shape1.getArea() << '\n';
    
    Cylinder shape2(rad, ht);
    cout
    << "Radius: " <<  shape2.getRadius()
    << " Height: " << shape2.getHeight()
    << " Area: " << shape2.getArea()
    << " Volume: " << shape2.getVolume() << '\n';
    
    
    return 0;
}



Radius: 2.5 Circumference: 15.708 Area: 19.6349
Radius: 2.5 Height: 3.7 Area: 97.3893 Volume: 72.6493
Program ended with exit code: 0
Last edited on
Just for interest sake after a bit of compaction etc ...

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
#include <iostream>
#include <iomanip>

class Circle
{
private:
    double m_radius{0};
    const double PI{3.14159}; // JUST FOR DEMO - CYLINDER DOESN'T NEED PI
    
public:
    Circle(double aRadius): m_radius(aRadius){}
    ~Circle(){}
    
    double getRadius() { return m_radius; }
    double getCircumference() { return 2 * PI * m_radius; }
    double getArea() { return PI * m_radius * m_radius; }
};

class Cylinder // NO INHERITANCE NEEDED - BUT MAKE SURE CIRCLE IS #include'd
{
private:
    double m_height{0};
    double m_radius{0};
public:
    Cylinder(double aRadius, double aHeight): m_height(aHeight), m_radius(aRadius){}
    ~Cylinder(){}
    
    double getRadius() { return m_radius; }
    double getHeight(){ return m_height; }
    double getSurfaceArea()
    {
        return 2 * Circle(m_radius).getArea() + Circle(m_radius).getCircumference() * m_height;
        
    }
    double getVolume() { return Circle(m_radius).getArea() * m_height; }
};

int main()
{
    // INPUT - cin's GO HERE IF REQUIRED
    double rad{2.5};
    double ht{3.7};
    
   // OUTPUT - GO HERE UNLESS CLASSES HAVE << operator (OR SIMILAR VIA <string>'s)
    Circle shape1(rad);
    std::cout
    << "Radius: " <<  shape1.getRadius() << ' '
    << "Circumference: " << shape1.getCircumference() << ' '
    << "Area: " << shape1.getArea() << '\n';
    
    Cylinder shape2(rad, ht);
    std::cout
    << "Radius: " <<  shape2.getRadius() << ' '
    << "Height: " << shape2.getHeight() << ' '
    << "Surface area: " << shape2.getSurfaceArea() << ' '
    << "Volume: " << shape2.getVolume() << '\n';
    
    return 0;
}


Radius: 2.5 Circumference: 15.708 Area: 19.6349
Radius: 2.5 Height: 3.7 Surface area: 97.3893 Volume: 72.6493
Program ended with exit code: 0
Topic archived. No new replies allowed.