Class as member of another class question.

S

SearedIce

Consider the following simplified hypothetical code:

#include <iostream.h>

class rabbit
{
public:
rabbit() {x = 3; y = 3; /*code here to set field[x][y] to 1*/}
void runtocage();
int x;
int y;
}

void rabbit::runtocage()
{
//code here to set field[x][y] to 0 and field[0][0] to 1
}

class garden
{
public:
void monsterattack();
rabbit fluffy;
bool field[5][5];
}

void garden::monsterattack()
{
fluffy.runtocage();
cout << "Ahh!!";
}

//End of code segment

This not at all what I'm really working on...I'm working on a linked
list for something, but what I show here is what is almost necessary
for what I'm doing...just thought the rabbit/garden thing would be cute
and clearly show what I'm wondering about...

Now for the question:
In the two places I have commented in the rabbit code, how can I do
what I wrote?

Could I add a member variable to rabbit that is a pointer to a garden
(garden * homesweethome; for instance) and give it the address of the
garden in the rabbit constructor when the rabbit is created in the
garden? (in garden code: "rabbit fluffy(this);" instead of "rabbit
fluffy;" and then change the rabbit constructor to: "rabbit(garden
home) {x = 3; y = 3; homesweethome = home; homesweethome->field[x][y] =
1;}" then do some similar code as that last command in the runtocage()
function to change the "field"...)

Won't that cause a compile error, however, because whichever class I
put before the other, the compiler will tell me it doesn't know what
one of them is when it is referred to in the other.

I'm probably missing something simple...there are some basic things
I've missed in the book's I've read and the classes I've taken...
Thanks for your time,
John
 
V

Victor Bazarov

SearedIce said:
Consider the following simplified hypothetical code:

#include <iostream.h>


class rabbit
{
public:
rabbit() {x = 3; y = 3; /*code here to set field[x][y] to 1*/}

What's field[x][y]? And prefer initalisation over assignment. It's in
the FAQ.
void runtocage();
int x;
int y;
} ;


void rabbit::runtocage()
{
//code here to set field[x][y] to 0 and field[0][0] to 1

What's field[x][y]?
}

class garden
{
public:
void monsterattack();
rabbit fluffy;
bool field[5][5];

Ah, *that* field?
} ;


void garden::monsterattack()
{
fluffy.runtocage();
cout << "Ahh!!";
}

//End of code segment

This not at all what I'm really working on...I'm working on a linked
list for something,

Really? Why? There is 'std::list', just use it...
but what I show here is what is almost necessary
for what I'm doing...just thought the rabbit/garden thing would be cute
and clearly show what I'm wondering about...

Cute? Are you trying to appeal to our inner children?
Now for the question:
In the two places I have commented in the rabbit code, how can I do
what I wrote?

Pass the 'field' as an argument.
Could I add a member variable to rabbit that is a pointer to a garden
(garden * homesweethome; for instance) and give it the address of the
garden in the rabbit constructor when the rabbit is created in the
garden?
Sure.

(in garden code: "rabbit fluffy(this);" instead of "rabbit
fluffy;" and then change the rabbit constructor to: "rabbit(garden
home) {x = 3; y = 3; homesweethome = home; homesweethome->field[x][y] =
1;}" then do some similar code as that last command in the runtocage()
function to change the "field"...)

Something like that...
Won't that cause a compile error, however, because whichever class I
put before the other, the compiler will tell me it doesn't know what
one of them is when it is referred to in the other.

Read about "circular references" and "forward declarations".
I'm probably missing something simple...there are some basic things
I've missed in the book's I've read and the classes I've taken...

Read the book again, take the classes again.

V
 
S

Sethalicious

On design: The class rabbit shouldn't have any knowledge of the
garden. What if you made another class called LooneyTunes where you
wanted to use a rabbit? The runtocage method should be a part of
another class or method which dictates how objects in the garden act,
and the garden class should provide methods to move objects within
itself.

Compiler errors will occur in the above scenario because you have a
circular reference. Rabbit is defined in terms of Garden, and Garden
is defined in terms of Rabbit. The compiler will recursively keep
searching for the definition of the class until it chokes.

The solution is to make sure each class has proper accessor methods
(get/set) for the private variables. Then, create another class that
then uses both rabbit and garden; GardenManager for example. Or,
design rabbit so it has no need to know anything about garden, and make
garden control all the logic for "runtocage" method.

There are multiple solutions to what you want to do, but I think your
example is obscuring what you're trying to do with a linked list.

I'm not clear on how you need this type of logic for a linked list,
though. The list needs no special knowledge of the type of objects its
storing. Please do some research on the Standard Template Library's
vector class (and other container classes) if you need some inspiration
for your studies. Otherwise, reread the books! ;-) We've all been
there!
 
U

Ulrich Achleitner

Could I add a member variable to rabbit that is a pointer to a garden
(garden * homesweethome; for instance) and give it the address of the
garden in the rabbit constructor when the rabbit is created in the
garden? (in garden code: "rabbit fluffy(this);" instead of "rabbit
fluffy;" and then change the rabbit constructor to: "rabbit(garden
home) {x = 3; y = 3; homesweethome = home; homesweethome->field[x][y] =
1;}" then do some similar code as that last command in the runtocage()
function to change the "field"...)

yes, and i would make it a reference instead of a pointer, because then
you do not need to worry about the NULL-case.
however, using a reference makes impossible the use of constructor which
does not initialize the reference.

Won't that cause a compile error, however, because whichever class I
put before the other, the compiler will tell me it doesn't know what
one of them is when it is referred to in the other.

I'm probably missing something simple...

yes :)
use a forward declaration.
i.e. in rabbit.h write:

class garden;
class rabbit
{
public:
rabbit(garden& g);
/*...*/
garden& mGarden;
};

and analogous in the header file for garden.
 
S

SearedIce

Alright...thanks for your effort.
Can anyone recommend a good book on object-oriented design? I guess
I've passed the knowledge level of the C++ stuff I have and need
something with a more careful author.
About why I'm making my own list: I'm the type of person that needs to
know how/why something works...also I can have more control. Not to
mention, I can show off to friends...well, at least the ones that
program...
Also, I'm not programming as a profession...just a hobby...so, I have
the time to do stuff like make stuff on my own and not just use the
built-in stuff...it's more fun this way.
~John
 
V

Victor Bazarov

SearedIce said:
Alright...thanks for your effort.
Can anyone recommend a good book on object-oriented design?

I used (and liked at the time) "Advanced C++" by J.Coplien. It is a bit
old (1991?), but helped me a great deal. Check it out.

Also, there is a newsgroup comp.object, where you can definitely find more
concentrated knowledge about OOD. Books included.
I guess
I've passed the knowledge level of the C++ stuff I have and need
something with a more careful author.
About why I'm making my own list: I'm the type of person that needs to
know how/why something works...also I can have more control. Not to
mention, I can show off to friends...well, at least the ones that
program...
Also, I'm not programming as a profession...just a hobby...so, I have
the time to do stuff like make stuff on my own and not just use the
built-in stuff...it's more fun this way.

Write a compiler. If it's any good, your friends will be green with envy.

V
 
S

Sethalicious

Can anyone recommend a good book on object-oriented design?
Design Patterns
by Eric Gramma, et al

A really good book you shouldn't be without! A must for your bookshelf.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top