child class accessing parent class

Hi all,

I've got a class called human:

1
2
3
4
5
6
7
8
9
10
11
class human
{

public:

	int height;

	human(){}
	~human(){}
	
};


and a child class called peter:

1
2
3
4
5
6
7
8
9
10
11
12
13
class peter : public human
{

public:

	int age;

	string name;

	peter(){}
	~peter(){}
	
};


I want to modify the parent class' height variable's value and later display it in human.cpp. I can access it in peter.cpp but I cannot modify it in a way that the human.cpp would display the modified value.

Is there a way I can modify a parent class' variables' values with a child class?

Thanks in advance,
Yours3lf
but I cannot modify it in a way that the human.cpp would display the modified value.


This doesn't make any sense. It doesn't matter where you modify it. Modifying it modifies it.

Are you sure you're printing it after it's been modified? Or are you only printing it before you modify it?
ok, believe your eyes here's the project:

http://www.easy-share.com/1912067702/proba2.rar
*waits the required 45 seconds to download the file*

you couldn't just paste the code?


EDIT:

in the future, do not put the .sdf file in the .rar. It makes it huge and is not required.

EDIT2:

Okay I'm looking at the code and I don't see what you're talking about. What variable are you changing and where? And how is it not behaving the way you expect?
Last edited on
sorry for making you wait, and ok i wont put it in
bump to make sure you see my latest edit
so I begin with hm.loop(),
which calls give_birth("peter"),
which calls pt.life("textxt") and sets rectx = 1, and valami = 1.

pt.life writes them out, modifies them and writes them out again.
Then I hop back to hm.loop() and from there I write out rectx and valami;

The output should be:


rectx(in peter.cpp) = 1; //i get 0
valami(in peter.cpp) = 1; //i get 0
rectx(in peter.cpp, modified) = 2; //this is what i get
valami(in peter.cpp, modified) = 2; //this is what i get
rectx(in human.cpp) = 2; //i get 1
valami(in human.cpp) = 2; // i get 1


Okay. Your problem is you're misunderstanding the concept of objects.

All these variables you have exist for each object. That is, you can have multiple humans and each human will have its own properties.

In your code, you have a total of two humans:

1) 'hm' (a "human" defined in main.cpp)
2) 'pt' (a "peter" defined in human.cpp)

Now when you call hm.loop();, that function is running for hm (not for pt). So changes you make to rectx, etc all change hm.rectx.

When loop() calls pt.life(), that function is running for pt (not for hm). So changes that function makes to rectx, etc all change pt.rectx (not hm.rectx!).


Basically you have two different people with their own characteristics, and you're expecting changes to one of them to affect the other. Which, of course, they don't.
so no matter peter called peter it will be a human extended with peter?

edit:

and how can i edit hm from pt?
Last edited on
I'm not sure you're understanding (or maybe I'm not understanding your question?)

The thing of it is.... hm and pt are two seperate humans. Two different people, like you and me. If my hair is brown, it doesn't mean your hair is brown. We each have our own hair color.

Just like hm and pt in your code. Just because pt has a rectx of 2 doesn't mean hm does. They each have their own properties.

I suspect that maybe you didn't mean to create this hm person, and you wanted all of this to be done with pt?
I'm sure I'm the one who doesnt understand :)

so I should do:
1
2
3
4
5
6
7
{main.cpp}
peter pt;

pt.give_birth("peter");
pt.life("textxt");
cout << "rectx(in human.cpp) = " << rectx << "\nvalami(in human.cpp) = " << valami;
pt.loop();


but then I can't call draw_peter() from loop()....
That might do the trick. But I don't really understand what you're trying to accomplish, so I can't say for sure.

"give_birth" seems like its name might be a little misleading. I would expect that function to create a new person and return a pointer to it or something. But you seem to be using it as some kind of initialization?

EDIT:

Also note that you'd have to get rid of the 'pt' in human.cpp, otherwise you'll still have two different people.
Last edited on
if under initialization you mean SDL_Init: yes i do, I'm try to do this because I have issues with it in a bigger project, a little game, where I would basically modify the base class' variables, but now I see I should go the other way making a child class which has the base class' stuff and can modify it.

edit:

ok now i can modify it but it gives me an unexpected error when trying to read surface, which is part of human
SDL_BlitSurface(images, 0, surface, &rect);

the project link
http://www.2shared.com/file/v1R2ICUi/proba2.html
{to download click at the bottom to the little text: Save file to your PC: click here}
Last edited on
Well I think you just need a tutorial on OO design or something. The way you have things structured is really weird, and that's what's giving you problems.

It's like your 'human' class is designed to be some kind of human manager, and not a single human, yet its variables are meant to be properties of a single human.

But anyway that's a lengthy subject that I don't care to get into right now =x
ok, I'm going to read an oo tutorial. I'm really a beginner at c++, sorry.
Topic archived. No new replies allowed.