Thread: Using classes as data members

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    25

    Using classes as data members

    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

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    First you may want to add a method GetX and GetY to the point class so you have some way to access the data of the point, other that printing it out with the print function.

    Now what is wrong with your get_centre function?
    Since the return type is a point, you should be able to return the point. What problems are you having?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamicly adding data members to structs
    By ITAmember in forum C Programming
    Replies: 11
    Last Post: 06-01-2009, 03:26 PM
  2. pointers as data members
    By Drogin in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2009, 06:55 AM
  3. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  5. templated members in non-template classes
    By drrngrvy in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2007, 01:38 PM