Hi,
I've asked this question before but although I got a solution that worked, I didn't understand why it did. Bassically, I have a class that sets up a point, and then another class that uses this point to make a circle.
Some code...
#include <iostream.h>
class point {
private:
int x;
int y;
public:
void init(int x_coord, int y_coord);
void print();
};
class circle {
private:
int radius;
point centre;
public:
void init(int new_radius, point new_centre);
void print();
int get_radius();
point get_centre();
};
int main() {
point point1;
circle newCircle;
point1.init(3,4);
newCircle.init(5, point1);
cout << "The coords are : ";
point1.print();
cout << "\nCIRCLE:\n";
cout << "\tradius: " << newCircle.get_radius cout << "\tcentre: " << newCircle.get_centre() return 0;
}
void circle::init(int new_radius, point new_centre) {
radius = new_radius;
centre = new_centre;
}
void point::init(int x_coord, int y_coord) {
x = x_coord;
y = y_coord;
}
void point:rint() {
cout << x << "," << y << "\n";
}
int circle::get_radius() {
return radius;
}
point circle::get_centre() {
return centre;
}
Now I know that "get_centre" doesn't work because it doesn't know how to return a value that contains two values (x and y). My book seems to suggest that I use something like...
cout << newCircle.centre.x;
cout << newCircle.centre.y;
in the get_centre() function. However, because x and y are private, errors occur (saying that x, y and newCircle are undeclared identifiers and also when calling the function).
I would prefer to do it this way rather than using the print() library function as somebody suggested last time, as I feel that it would help be understand OO programming and inheritance better.
Any help is greatly appreciated.
Bill