Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / question about parent and children classes

Author
Message
haliop
User Banned
Posted: 1st Apr 2010 17:13 Edited at: 1st Apr 2010 17:19
from what i understand
there are inharit functions in C++
i dont fully understand it thats why im asking for a bit of help.

for an example i will use the common "List" idea or Resource Manager
(never built one just rad about them).

so im building a simple GUI , where there is a Drop Down Menu Bar
the menu bar has like 3 buttons (File,Options,Help)
File opens -> New,Save,Load,Exit
Options opens -> Render Options,Texture Options,Light Options
Help opens -> Tutorials,Web Link,Credits

this is just an example.

so , for now what i do is
this is the Button.H eader file


ofcourse no need to point at the Button.Cpp file as most of you can understand or imagine what is written there as you have probably mastered it already

and i my code : Main.CPP
Button Setup.


updating Sprites, as Button dont really hide the sprites just contains a Boolean if the button is hidden or not


main loop



so i guess some of you may have seen the problem.

if i want to check on all the MenuBar Top Menus
i will have to check with File[i] and then with Options[i] and then with Help[i]

so its a bit of a mass of code not really needed , altough it will work good , it will just contain alot of code that i know there is a better way to do that with inharit but i dont know how

i appreciate any help i can get.
TY
nadav.
haliop
User Banned
Posted: 1st Apr 2010 22:08
anyone?
haliop
User Banned
Posted: 2nd Apr 2010 15:30
ohh come on honestly?
entomophobiac
21
Years of Service
User Offline
Joined: 1st Nov 2002
Location: United States
Posted: 2nd Apr 2010 23:47
I'm not sure what you're after, in all honesty, but try reading up on Polymorphism and the "virtual" keyword.

Something like this:
http://www.glenmccl.com/virt_cmp.htm

That might be what you're after. And a good way to get good answers here is to write a simplified question and not post too much code.

Just a suggestion.
Cuddle Bunniezzz 12
15
Years of Service
User Offline
Joined: 14th Jan 2009
Location: Buffalo, NY
Posted: 3rd Apr 2010 00:02
I'm a little confused on what your question is. Is it "How do child classes inherit stuff from their parent classes?".

Well, child classes inherit ALL of the functions that are declared in "public" and "protected" sections of a parent class. One thing to mention is that child classes CANNOT access "private" class data-members, this includes variables and functions. But what you can do is two things:

A. Changes those "private" members to "protected" members. Protected works similar to that of a "private" member, but the child class can access the members in "protected" section.

B. Add accessor and mutator functions to the parent class in the "public" section. The accessor functions are able to get the data-members like so:


And the mutator functions, well mutate (change) the variables, ex:


In my opinion, it's better to have stuff in the "protected", section.

One more important thing. To use a parent class's constructor, you need to do something like this:



It may seem a bit confusing, but try it out and you'll get it.

Any more questions?

http://www.darkgdk.us/ <- You can now submit pages, upload images, yet were lacking content. We need your help!
haliop
User Banned
Posted: 3rd Apr 2010 08:17
ok , i think i got it, i'll try sometings and if i'll stuck ill ask again
ty.
haliop
User Banned
Posted: 4th Apr 2010 21:08
HERE IS an example of an Enemy Ai (simple )
where i use Class and inharince (i think thats the name)
its just a beggining , but tell me if im wrong with it

Cuddle Bunniezzz 12
15
Years of Service
User Offline
Joined: 14th Jan 2009
Location: Buffalo, NY
Posted: 4th Apr 2010 21:22
Nah, it looks fine.

http://www.darkgdk.us/ <- You can now submit pages, upload images, yet were lacking content. We need your help!
haliop
User Banned
Posted: 5th Apr 2010 11:18
awesome
can you tell me more about
Virtual?
and the ~ thing you put behind it and everything?
Lich120
18
Years of Service
User Offline
Joined: 19th Mar 2006
Location:
Posted: 5th Apr 2010 14:25
The the ~ signifies a classes destructor function. Which is the function that is called when you call delete on a pointer of that type. It's not always necessary to implement, but if you are allocating more memory from within your class, then it's very important you clean up that memory in the destructor.
Cuddle Bunniezzz 12
15
Years of Service
User Offline
Joined: 14th Jan 2009
Location: Buffalo, NY
Posted: 5th Apr 2010 14:26
A virtual function is a function that you can over load in a child class, but it needs to be declared "virtual" in the parent class.

http://www.darkgdk.us/ <- You can now submit pages, upload images, yet were lacking content. We need your help!
haliop
User Banned
Posted: 5th Apr 2010 15:09
hmm
i dont understand..
i have alot of c++ learning to do
well , i got a book somewhere , i need to find it
Cuddle Bunniezzz 12
15
Years of Service
User Offline
Joined: 14th Jan 2009
Location: Buffalo, NY
Posted: 5th Apr 2010 22:28
Well, two good ones I recommend:

http://www.amazon.com/Beginning-C-Through-Game-Programming/dp/1598633600/ref=sr_1_1?ie=UTF8&s=books&qid=1270495470&sr=8-1
^^^
Great First C++ book, covers the core C++ needed to know how to program games in C++, gives a nice introduction to classes. Covers the STL a bit.


http://www.amazon.com/Beginners-Guide-Second-Herbert-Schildt/dp/0072232153/ref=sr_1_1?ie=UTF8&s=books&qid=1270495547&sr=1-1
^^^
A book that covers almost everything C++ except for the STL. Over half of the book are the last 5 chapters (out of 12). And they are just about Classes, Inheritance, Polymorphism, Templates, Exceptions, and the IO-Stream. If you've read the first book, you only need to read the last 4 chapters of this book. This book is very in depth of using classes.


Both are great books.

http://www.darkgdk.us/ <- You can now submit pages, upload images, yet were lacking content. We need your help!
Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 5th Apr 2010 22:38
I dont recommend 'Learn C++ in 24 hours', its just not very good.
It has examples that are a pages long just to illustrate one small point, useless as a reference.

haliop
User Banned
Posted: 7th Apr 2010 23:00
yeah ive actually heard about
/Beginning-C-Through-Game-Programming
and heard it was good!

i will check into that.

Login to post a reply

Server time is: 2024-10-02 03:33:40
Your offset time is: 2024-10-02 03:33:40