Class knowing properties of another class that contains it

Discussion in 'C++' started by Joseph Wakeling, Aug 8, 2006.

  1. Hello all---novice C++ user here, though experienced with C.

    Let's suppose I have two classes where one contains another, e.g.,

    /********************/
    class House {
    int housenumber;
    int number_of_people;
    // etc.
    }

    class Street {
    int number_of_houses;
    House *h; // array of houses in the street
    int PostalCode;
    // etc.
    }
    /********************/

    What I want to know is, is there a way for a House to inherit certain
    data from the Street which contains it? For example, I would like each
    House to know the PostalCode of its Street, but not other data.

    One way, of course, might be to place a Street * pointer in each House,
    allowing each House to refer to the Street that contains it. But let's
    say I don't want to do that, I don't want the House to contain any more
    elements, I just want the House class and the associated functions to
    have access to this particular piece of data. Is there a way?

    I presume that my way of setting up these classes may not be the best
    to achieve what I want, so any alternative formulations of this
    House/Street setup would be welcome.

    Note that the House/Street here is just a casual example and one could
    think of plenty of others, e.g. webpage/website, neuron/neural network,
    computer/network, engine/car ...

    Many thanks,

    -- Joe
    Joseph Wakeling, Aug 8, 2006
    #1
    1. Advertising

  2. Joseph Wakeling wrote:
    > Hello all---novice C++ user here, though experienced with C.
    >
    > Let's suppose I have two classes where one contains another, e.g.,
    >
    > /********************/
    > class House {
    > int housenumber;
    > int number_of_people;
    > // etc.
    > }
    >
    > class Street {
    > int number_of_houses;
    > House *h; // array of houses in the street
    > int PostalCode;
    > // etc.
    > }
    > /********************/
    >
    > What I want to know is, is there a way for a House to inherit certain
    > data from the Street which contains it? For example, I would like
    > each House to know the PostalCode of its Street, but not other data.
    >
    > One way, of course, might be to place a Street * pointer in each
    > House, allowing each House to refer to the Street that contains it.


    That's a good way, yes.

    > But let's say I don't want to do that, I don't want the House to
    > contain any more elements, I just want the House class and the
    > associated functions to have access to this particular piece of data.
    > Is there a way?


    You could have a member in Street to enumerate its own Houses, and then
    a static member in Street to enumerate all Streets (which would require
    each Street to register itself). A House could call Street enumeration
    function, and for each Street, find itself in the Street's collection of
    Houses, and then ask the Street where it is located for the PostalCode.
    But that's utterly inefficient, and to keep a pointer to Street or a copy
    of PostalCode in a House is *much better*.

    > I presume that my way of setting up these classes may not be the best
    > to achieve what I want, so any alternative formulations of this
    > House/Street setup would be welcome.


    No, thank you. You do your own homework. Besides, how the hell should
    we know what it is you want?

    > Note that the House/Street here is just a casual example and one could
    > think of plenty of others, e.g. webpage/website, neuron/neural
    > network, computer/network, engine/car ...


    State your requirements, give your solution, we can discuss its pro et
    contra.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 8, 2006
    #2
    1. Advertising

  3. Joseph Wakeling

    mlimber Guest

    Joseph Wakeling wrote:
    > Hello all---novice C++ user here, though experienced with C.
    >
    > Let's suppose I have two classes where one contains another, e.g.,
    >
    > /********************/
    > class House {
    > int housenumber;
    > int number_of_people;
    > // etc.
    > }
    >
    > class Street {
    > int number_of_houses;
    > House *h; // array of houses in the street


    Generally you should prefer using std::vector<House> to manual
    allocation. It's exception-safe, has automatic cleanup, and is
    standard. See this FAQ:

    http://www.parashift.com/c -faq-lite/containers.html#faq-34.1

    > int PostalCode;
    > // etc.
    > }
    > /********************/
    >
    > What I want to know is, is there a way for a House to inherit certain
    > data from the Street which contains it? For example, I would like each
    > House to know the PostalCode of its Street, but not other data.
    >
    > One way, of course, might be to place a Street * pointer in each House,
    > allowing each House to refer to the Street that contains it. But let's
    > say I don't want to do that, I don't want the House to contain any more
    > elements, I just want the House class and the associated functions to
    > have access to this particular piece of data. Is there a way?
    >
    > I presume that my way of setting up these classes may not be the best
    > to achieve what I want, so any alternative formulations of this
    > House/Street setup would be welcome.


    I'd say you should either pass the postal code in through the
    constructor of House or create a "setter" function in House to allow
    the user (in this case, Street) to supply the postal code to it. The
    benefit of using the constructor is that the House object is (well, can
    be) guaranteed to be valid since it can never have an invalid postal
    code.

    Cheers! --M
    mlimber, Aug 8, 2006
    #3
  4. Joseph Wakeling

    Daniel T. Guest

    In article <>,
    "Joseph Wakeling" <> wrote:

    > Hello all---novice C++ user here, though experienced with C.
    >
    > Let's suppose I have two classes where one contains another, e.g.,
    >
    > /********************/
    > class House {
    > int housenumber;
    > int number_of_people;
    > // etc.
    > }
    >
    > class Street {
    > int number_of_houses;
    > House *h; // array of houses in the street
    > int PostalCode;
    > // etc.
    > }
    > /********************/
    >
    > What I want to know is, is there a way for a House to inherit certain
    > data from the Street which contains it? For example, I would like each
    > House to know the PostalCode of its Street, but not other data.
    >
    > One way, of course, might be to place a Street * pointer in each House,
    > allowing each House to refer to the Street that contains it. But let's
    > say I don't want to do that, I don't want the House to contain any more
    > elements, I just want the House class and the associated functions to
    > have access to this particular piece of data. Is there a way?
    >
    > I presume that my way of setting up these classes may not be the best
    > to achieve what I want, so any alternative formulations of this
    > House/Street setup would be welcome.
    >
    > Note that the House/Street here is just a casual example and one could
    > think of plenty of others, e.g. webpage/website, neuron/neural network,
    > computer/network, engine/car ...


    This isn't so much a C++ issue as an Object-Oriented Design issue. You
    should ask on comp.object for a lively discussion of it.
    Daniel T., Aug 8, 2006
    #4
  5. Thanks to everyone for your suggestions and useful comments.

    > State your requirements, give your solution, we can discuss its pro et
    > contra.


    As I say, I'm a novice, and really I'm just trying to learn about
    possibilities of the language. There are a couple of possible
    applications in some neural network-related code I'm writing, but I'm
    not convinced what I'm asking about here is the best solution for that.
    I just want to know whether it's a *possible* solution.

    What I'm trying to find out is whether it's possible to have two
    objects---one is a container, the other is contained in the
    container---and for the contained object to know about properties of
    the container *without* having special pointers or whatever. I suspect
    that this is best done with "nested classes", but the book I have isn't
    too forthcoming on them, which is why I'm asking here.

    So, with the houses and so on, for example, I might use,

    /**************************/
    class Street {
    public:
    int number_of_houses;
    int PostalCode;

    private:
    class House {
    private:
    int number_of_people;
    public:
    void census(void)
    {
    cout << number_of_people << " live here, and the postal
    code is " << Street::postalCode;
    }
    }
    }
    /**************************/

    Any comments on this kind of solution?
    Joseph Wakeling, Aug 8, 2006
    #5
  6. Joseph Wakeling wrote:
    > [..]
    > As I say, I'm a novice, and really I'm just trying to learn about
    > possibilities of the language. There are a couple of possible
    > applications in some neural network-related code I'm writing, but I'm
    > not convinced what I'm asking about here is the best solution for
    > that. I just want to know whether it's a *possible* solution.
    >
    > What I'm trying to find out is whether it's possible to have two
    > objects---one is a container, the other is contained in the
    > container---and for the contained object to know about properties of
    > the container *without* having special pointers or whatever.


    No. Think about it. You're asking to introduce a dependency of some
    kind without storing *any* additional information anywhere. It's just
    impossible. Some information has to exist to reach the dependency you
    want to introduce. If you are starting at the "Street" level, you can
    find all "Houses". But if you're at the "House" level, there is no
    way to go back to the "Street" level without additional data somewhere.
    You don't have to store it in the "House", but it has to exist and be
    available to each "House".

    > I
    > suspect that this is best done with "nested classes", but the book I
    > have isn't too forthcoming on them, which is why I'm asking here.


    No. "Nested classes" are a way to express a relationship between types
    not between objects.

    > So, with the houses and so on, for example, I might use,
    >
    > /**************************/
    > class Street {
    > public:
    > int number_of_houses;
    > int PostalCode;
    >
    > private:
    > class House {
    > private:
    > int number_of_people;
    > public:
    > void census(void)
    > {
    > cout << number_of_people << " live here, and the postal
    > code is " << Street::postalCode;
    > }
    > }

    ;

    No, it won't work. The fact that the class 'House' is declare inside the
    class 'Street' does NOT mean that there is a 'House' object inside each
    'Street' object. Those things are orthogonal.

    > }
    > /**************************/
    >
    > Any comments on this kind of solution?


    It's not.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 8, 2006
    #6
  7. Victor Bazarov wrote:
    > No. Think about it. You're asking to introduce a dependency of some
    > kind without storing *any* additional information anywhere. It's just
    > impossible. Some information has to exist to reach the dependency you
    > want to introduce.


    No, I appreciate that a dependency has to exist. What I'm wanting to
    find out is if I can automate that dependency while defining the
    classes, instead of having to put it in manually---in a similar way to
    how the C++ object-oriented syntax automates various other things that
    I would have to do manually in C.

    e.g. "An object of the House class can only ever be created inside an
    object of the Street class and should know such-and-such
    characteristics of the object of Street class in which it's created."

    .... but I take it the C++ object-oriented syntax doesn't allow me to do
    things like that?
    Joseph Wakeling, Aug 9, 2006
    #7
  8. Joseph Wakeling wrote:
    > [..] What I'm wanting to
    > find out is if I can automate that dependency while defining the
    > classes, instead of having to put it in manually---in a similar way to
    > how the C++ object-oriented syntax automates various other things that
    > I would have to do manually in C.
    >
    > e.g. "An object of the House class can only ever be created inside an
    > object of the Street class and should know such-and-such
    > characteristics of the object of Street class in which it's created."
    >
    > ... but I take it the C++ object-oriented syntax doesn't allow me to
    > do things like that?


    No, it does not. You have to introduce any of those things "manually".
    For example, class Street can be declared a friend of House, and House's
    constructor can be private which will only allow Street to create House
    objects...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 9, 2006
    #8
  9. OK. Thanks very much for your advice and ideas.
    Joseph Wakeling, Aug 9, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Dyer
    Replies:
    5
    Views:
    638
  2. Kannan
    Replies:
    0
    Views:
    540
    Kannan
    Mar 11, 2005
  3. Franck PEREZ
    Replies:
    15
    Views:
    545
    Mike Meyer
    Nov 19, 2005
  4. Joseph Turian
    Replies:
    5
    Views:
    578
  5. gilak
    Replies:
    2
    Views:
    329
    gilak
    Aug 20, 2007
Loading...

Share This Page