- #1
Const@ntine
- 285
- 18
Homework Statement
Find the are of the cylinder, by creating a program in C++.
2. The attempt at a solution
So far I have:
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:
double radius;
public:
circle()
{
}
circle(int xvalue, int yvalue, double radiusvalue)
{
x = xvalue;
y = yvalue;
radius = radiusvalue;
}
void setRadius(double value)
{
radius = value;
}
double getRadius (void)
{
return radius;
}
double getArea(void)
{
return PI*radius*radius;
}
void print(void)
{
cout << "Center: (" << x << "," << y << ") ";
cout << "Radius : " << radius << endl;
}
};
class cylinder : circle
{
private:
double height;
public:
cylinder()
{
}
cylinder(int xvalue, int yvalue, int rvalue, double hvalue)
{
x = xvalue;
y = yvalue;
radius = rvalue;
height = hvalue;
}
void setHeight(double value)
{
height = value;
}
double getHeight(void)
{
return height;
}
double getArea(void)
{
return 2*circle::getArea() + 2*PI*radius*height;
}
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.setRadius(1.5);
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
double radius;
^
AC.cpp:90:4: error: within this context
radius = rvalue;
^
AC.cpp: In member function 'double cylinder::getArea()':
AC.cpp:46:10: error: 'double circle::radius' is private
double radius;
^
AC.cpp:103:38: error: within this context
return 2*circle::getArea() + 2*PI*radius*height;
^
AC.cpp: In member function 'void cylinder::print()':
AC.cpp:46:10: error: 'double circle::radius' is private
double radius;
^
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
void setRadius(double value)
^
AC.cpp:121:17: error: within this context
x.setRadius(1.5);
^
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!