Class knowing properties of another class that contains it

  • Thread starter Joseph Wakeling
  • Start date
J

Joseph Wakeling

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
 
V

Victor Bazarov

Joseph said:
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
 
M

mlimber

Joseph said:
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
 
D

Daniel T.

"Joseph Wakeling said:
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.
 
J

Joseph Wakeling

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?
 
V

Victor Bazarov

Joseph said:
[..]
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
 
J

Joseph Wakeling

Victor said:
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?
 
V

Victor Bazarov

Joseph said:
[..] 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top