473,382 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Class knowing properties of another class that contains it

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

Aug 8 '06 #1
8 1353
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
Aug 8 '06 #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
Generally you should prefer using std::vector<Houseto manual
allocation. It's exception-safe, has automatic cleanup, and is
standard. See this FAQ:

http://www.parashift.com/c++-faq-lit....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

Aug 8 '06 #3
In article <11*********************@m79g2000cwm.googlegroups. com>,
"Joseph Wakeling" <jo*************@gmail.comwrote:
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.
Aug 8 '06 #4
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?

Aug 8 '06 #5
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
Aug 8 '06 #6
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?

Aug 9 '06 #7
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
Aug 9 '06 #8

OK. Thanks very much for your advice and ideas.

Aug 9 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jianli Shen | last post by:
Hi, I want to implement like this: class A{ int length; class B{ void operate_length_in_A(){
5
by: Xiangliang Meng | last post by:
Hi, all. What are the benefit and the drawback of defining a class embedded inside another class? For example: class List { public:
2
by: Eugene | last post by:
Hi, I got a singleton class. Then in another class (CA), i would declare a variable with this singleton class and get a reference to it. I would have another class (IA1) that would inherit from CA...
2
by: Macca | last post by:
Hi, I have a windows form project. The form class has a number of methods in it that i want to call from another class I have just created. This class is a finite state machine and is created...
4
by: Steve Goldman | last post by:
Even asking this question probably demonstrates that I have a fundamental misunderstanding of how values and references work in C#, but here goes: I'd like to assign a reference to an arbitrary...
3
by: unicorn7777777 | last post by:
Hi, I need a deep copy constructor for a class which contains an array for another class: class Chessboard { public: ChessSquare chessSquare; // copy constructor needed to copy all...
17
by: VanKha | last post by:
After reading some books in c++,I know that it is better to hide the data members ,but sometimes the rule makes everything messy and terrible ! For example class Node{ private: int key; ...
3
by: James Booker | last post by:
Hi all - this should be a very easy question to you guys. Say I have two classes, class1 and class2. In my application, class2 can't operate without a class1, as class1 performs communication...
5
by: Alavi | last post by:
Hi Friends, I'm a new comer to the C# and oop world. I have a little problem about the class properties please kindly help me. I have a class named "User" in namespace_A . (User is not a static...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.