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?
*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?
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.
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?
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.
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);
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