# Homework Help: Cylinder Area (C++)

Tags:
1. May 30, 2017

### Techno_Knight

1. The problem statement, all variables and given/known data

Find the are of the cylinder, by creating a program in C++.

2. The attempt at a solution

So far I have:

Code (C):

#include <iostream>
using namespace std;

const double PI = 3.14159;

class point
{
protected:
int x;
int y;

public:
point()
{
}
point (int xvalue, int yvalue)
{
x = xvalue;
y = yvalue;
}
void setX(int value)
{
x = value;
}
void setY(int value)
{
y = value;
}
int getX(void)
{
return x;
}
int getY(void)
{
return y;
}
void print(void)
{
cout << "(" << x << "," << y << ")" << endl;
}
};

class circle : public point
{
private:

public:
circle()
{
}
circle(int xvalue, int yvalue, double radiusvalue)
{
x = xvalue;
y = yvalue;
}
{
}
{
}
double getArea(void)
{
}
void print(void)
{
cout << "Center: (" << x << "," << y << ") ";
}
};

class cylinder : circle
{
private:
double height;

public:
cylinder()
{
}
cylinder(int xvalue, int yvalue, int rvalue, double hvalue)
{
x = xvalue;
y = yvalue;
height = hvalue;
}
void setHeight(double value)
{
height = value;
}
double getHeight(void)
{
return height;
}
double getArea(void)
{
}
void print(void)
{
cout << "Center: (" << x << "," << y << ") ";
cout << "Radius: " << radius << " Height: " << height << endl;
}
};

int main()
{
cylinder x(37, 43, 2.5, 3);

x.print();
cout << "Cylinder area: " << x.getArea() << endl;

x.setX(25);
x.setY(35);
x.setHeight(2);
x.print();

return 0;
}

But I can't compile it yet. I get:

---------- Capture Output ----------
> "C:\Emerald\gcpp.bat" AC.cpp AC
AC.cpp: In constructor 'cylinder::cylinder(int, int, int, double)':
AC.cpp:46:10: error: 'double circle::radius' is private
^
AC.cpp:90:4: error: within this context
^
AC.cpp: In member function 'double cylinder::getArea()':
AC.cpp:46:10: error: 'double circle::radius' is private
^
AC.cpp:103:38: error: within this context
^
AC.cpp: In member function 'void cylinder::print()':
AC.cpp:46:10: error: 'double circle::radius' is private
^
AC.cpp:108:26: error: within this context
cout << "Radius: " << radius << " Height: " << height << endl;
^
AC.cpp: In function 'int main()':
AC.cpp:21:8: error: 'void point::setX(int)' is inaccessible
void setX(int value)
^
AC.cpp:119:11: error: within this context
x.setX(25);
^
AC.cpp:119:11: error: 'point' is not an accessible base of 'cylinder'
AC.cpp:25:8: error: 'void point::setY(int)' is inaccessible
void setY(int value)
^
AC.cpp:120:11: error: within this context
x.setY(35);
^
AC.cpp:120:11: error: 'point' is not an accessible base of 'cylinder'
AC.cpp:58:8: error: 'void circle::setRadius(double)' is inaccessible
^
AC.cpp:121:17: error: within this context
^
AC.cpp:121:17: error: 'circle' is not an accessible base of 'cylinder'

> Terminated with exit code 1.

I'm guessing the problem is that the cylinder part of the program can't access the circle class, and use the radius. Problem is, I know little next to nothing about classes and whatnot, so I'm at a loss here. I set up that program with what was given by a previous exercise, plus some other programs I studied, but I'm stuck now.

Any help is appreciated!

2. May 30, 2017

### collinsmark

I haven't tried to compile the code, but I think the issue has to do with the difference between "private" and "protected" in your variable (property) declarations.

"public" means that the variable (property) can be accessed anywhere.

"protected" means that the variable (property) can be accessed by member functions (i.e. methods) within the class, as well as other classes that inherit from that class. (Friends are also able to access protected variables/properties, but that's not important in this case.)

"private" means that the variable (property) can only be accessed by member functions (i.e. methods) within the class. Classes that inherit from the parent class do not have access to the parent class' private variables/properties.

Make sure that a child class is not trying to access a parent's class' private properties/variables.

3. May 30, 2017

### Techno_Knight

Thanks for the reply! I looked around a bit based on what you said, and came up with this:

Code (C):

#include <iostream>
using namespace std;

const double PI = 3.14159;

class point
{
protected:
int x;
int y;

public:
point()
{
}
point (int xvalue, int yvalue)
{
x = xvalue;
y = yvalue;
}
void setX(int value)
{
x = value;
}
void setY(int value)
{
y = value;
}
int getX(void)
{
return x;
}
int getY(void)
{
return y;
}
void print(void)
{
cout << "(" << x << "," << y << ")" << endl;
}
};

class circle : public point
{
protected:

public:
circle()
{
}
circle(int xvalue, int yvalue, double radiusvalue)
{
x = xvalue;
y = yvalue;
}
{
}
{
}
double getArea(void)
{
}
void print(void)
{
cout << "Center: (" << x << "," << y << ") ";
}
};

class cylinder : public circle
{
private:
double height;

public:
cylinder()
{
}
cylinder(int xvalue, int yvalue, int rvalue, double hvalue)
{
x = xvalue;
y = yvalue;
height = hvalue;
}
void setHeight(double value)
{
height = value;
}
double getHeight(void)
{
return height;
}
double getArea(void)
{
}
void print(void)
{
cout << "Center: (" << x << "," << y << ") ";
cout << "Radius: " << radius << " Height: " << height << endl;
}
};

int main()
{
cylinder x(37, 43, 2.5, 3);

x.print();
cout << "Cylinder area: " << x.getArea() << endl;

x.setX(25);
x.setY(35);
x.setHeight(2);
x.print();
cout << "Cylinder area: " << x.getArea() << endl;

return 0;
}

When I execute it, I get:

Center: (37,43) Radius: 2 Height: 3
Cylinder area: 62.8318
Center: (25,35) Radius: 1.5 Height: 2
Cylinder area: 32.9867
Press any key to continue . . .

Which is correct if you do the computations by hand. So I guess that's it? Are there any logical errors or something along those lines?

4. May 30, 2017

### collinsmark

Logically speaking, it looks good to me. (as far as I can tell by glancing through it.)

5. May 30, 2017

### collinsmark

[Edit: Post deleted. Nevermind. You already did what I was about to suggest correctly.]

6. May 30, 2017

### Techno_Knight

Nice! Thanks a lot for the help!