GregA
- 210
- 0
An exercise I am trying to complete is to create a base class pointType which has functions to get two coordinates, store and print them; then to create another class derived from pointType that will find the radius, centre, area of a circle, and print these; then to create another class cylinderType derived from circleType that will find the volume and surface area of a cylinder as well as printing out all the parameters that make up the cylinder etc...
My program so far is the following:
The problem I'm having so far is that in circleType I want to store the info about the circle's centre in an array with just 2 elements but at compile time I get the following:
cylinderType.cpp: In function ‘int main()’:
cylinderType.cpp:34: error: ‘double circleType::centre [2]’ is private
cylinderType.cpp:42: error: within this context
Furthermore if I comment out the keyword private (which I don't want to do) then though the constructor with parameters seems to work, the function setCentre isn't.
I have played around with test programs that have arrays as private members and had no problems but with this, it doesn't like it and I don't know what the problem is (though I bet its something stupid on my part)
Can someone point out what I'm doing wrong?
My program so far is the following:
Code:
#include <iostream>
#include <cmath>
using namespace std;
const double PI = acos(-1);
class pointType //gets information about 2 points x and y
{
public:
void setCoords(double X, double Y);
void printCoords() const;
double getX() const;
double getY() const;
pointType();
pointType(double X, double Y);
private:
double x;
double y;
};
class circleType: public pointType //gets information about a circle with radius rad, centered at point (x,y)
//derived from pointType
{
public:
void setCircle(double X, double Y, double rad);
void setCentre();
double calcArea() const;
void setArea();
void printCircleDetails() const;
circleType();
circleType(double X, double Y, double rad);
private:
double centre[2];
double radius;
double area;
};
int main()
{
circleType myCircle1(5,6,7);
cout << myCircle1.centre[0] << endl;
myCircle1.printCircleDetails();
cout << "\n\n";
circleType myCircle2;
myCircle2.setCircle(5,6,7);
myCircle2.printCircleDetails();
return 0;
}
void pointType::setCoords(double X, double Y)
{
x = X;
y = Y;
}
void pointType::printCoords() const
{
cout << "coordinates of point are: (" << x << "," << y << ")" << endl;
}
double pointType::getX() const
{
return x;
}
double pointType::getY() const
{
return y;
}
pointType::pointType()
{
x = 0;
y = 0;
}
pointType::pointType(double X, double Y)
{
x = X;
y = Y;
}
void circleType::setCircle(double X, double Y, double rad)
{
pointType::setCoords(X,Y);
if(rad < 0.0)
radius = -1*rad;
else
radius = rad;
}
void circleType::setCentre()
{
centre[0] = pointType::getX();
centre[1] = pointType::getY();
}
double circleType::calcArea() const
{
return PI*pow(radius,2);
}
void circleType::setArea()
{
area = calcArea();
}
void circleType::printCircleDetails()const
{
cout << "centre = (" << centre[0] << "," << centre[1] << ")"
<< ", radius = " << radius << ", area = " << calcArea();
}
circleType::circleType():pointType()
{
radius = 0;
centre[0] = 0;
centre[1] = 0;
}
circleType::circleType(double X, double Y, double rad):pointType(X,Y)
{
radius = rad;
centre[0] = pointType::getX();
centre[1] = pointType::getY();
}
The problem I'm having so far is that in circleType I want to store the info about the circle's centre in an array with just 2 elements but at compile time I get the following:
cylinderType.cpp: In function ‘int main()’:
cylinderType.cpp:34: error: ‘double circleType::centre [2]’ is private
cylinderType.cpp:42: error: within this context
Furthermore if I comment out the keyword private (which I don't want to do) then though the constructor with parameters seems to work, the function setCentre isn't.
I have played around with test programs that have arrays as private members and had no problems but with this, it doesn't like it and I don't know what the problem is (though I bet its something stupid on my part)
Can someone point out what I'm doing wrong?