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