Lesson 13: Programming With Class(es)

Blog post by darkwyrm on Sun, 2010-04-04 00:48

Today we'll be diving headlong into the murky depths of C++ programming: classes and inheritance -- struct's with fancy tricks aplenty. It's also our last lesson before writing our first windowed Haiku program, so get ready and study well. Enjoy! Learning to Program With Haiku, Lesson 13.

Comments

Re: Lesson 13: Programming With Class(es)

Thank you again.
I like your lessons.

Re: Lesson 13: Programming With Class(es)

good lesson.
thanks.

Re: Lesson 13: Programming With Class(es)

I am not sure if it was your intention to simplify the description of the virtual keyword to keep the lesson short, but it is possible to override a function of the parent class without using the virtual keyword. All public and protected functions can be overridden, private functions, however, can not be overridden.

The virtual keyword is used for "late binding". This means that if you pass a child object that inherits from a parent class to a certain function that accepts the parent class object as input, and that function calls a function from the parent class that has been declared virtual, then the child implementation of that virtual function will be called. Without the virtual keyword the parent class function will be called, even if the child has overridden that function.

Virtual functions can also be made pure, and that can be used to define interfaces that will be implemented in child classes.

It sounds complicated, and is better explained with a good example. To get an in-depth understanding about this and other topics, I would recommend to download Thinking in C++ by Bruce Eckel. Reading this book helped me a lot.