🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ Workshop - Polymorphism (Ch. 14)

Started by
13 comments, last by Dbproguy 16 years, 1 month ago

Welcome to the GDNet C++ Workshop – Ch. 14

For a complete introduction to this workshop, please look here. Workshop Overview This workshop is designed to aid people in their journey to learn beginning C++. This workshop is targeted at highly motivated individuals who are interested in learning C++ or who have attempted to learn C++ in the past, but found that without sufficient support and mentoring they were unable to connect all the pieces of this highly complex but powerful programming language. This is a 'guided' self-teaching C++ workshop. Each student is responsible for taking the time to read the material and learn the information. The community and tutors that arise out of this workshop are here for making the learning process run more smoothly, but are not obligated to baby-sit a person's progress. Because everyone will be working from the same textbook (Teach Yourself C++ in 21 days 5th Ed.), students may find it easier to get answers to the specific questions they might have. There is no minimum age requirement, and there is no previous programming experience required. Additionally, this workshop does not attempt to defend C++ as a language, nor does it attempt to demonstrate that C++ is either more or less useful then other programming languages for any particular purpose. People who intend to start a discussion about the differences between C++ and ANY other languages (except as are relevant to a particular discussion), are encouraged to do so elsewhere. This workshop is for educational, not philosophical discussions. Quizzes & Exercises Each week will have quizzes and exercises posted in the weekly threads. Please try and answer them by yourself. As well, please DO NOT post the answers to Quizzes and Exercises within this thread. Once it becomes acceptable to post the answers to quizzes and exercises, an additional thread will be created each week specifically for the purpose of posting quiz answers. If you try with reasonable effort but are unable to answer the questions or complete the exercises, feel free to post a clarification question here on the thread. Tutors, myself, or others will do the best we can to point you in the right direction for finding the answer.

Chapter 14 – Polymorphism

Introduction Welcome back all! This week we will be exploring the topic of Polymorphism. The definition of polymorphism is simply something taking on more than one form. In reality, this occurs quite frequently in C++ - ie. function overloading, operator overloading, templating, and even inheritance. But when we speak of Polymorphism (capital p), we're referring specifically to base pointers being able to act as any of its children, depending upon what it actually points to, and what interfaces are exposed. This can often be a difficult topic for people so I look forward to seeing your questions. Also, there's just one week left to complete Project 1. This project is designed to test everything you've learned so far. Expressions, branching, functions, pointers, classes, loops, and object oriented design. I'm looking forward to seeing what each person following the tutorial can come up with. If you find you're having technical questions about how to do something, you might want to go back and re-read a chapter or 2. If you're having questions about design, and where to get started, well...welcome to the wonderful world of problem solving. Please feel free to post questions in the Project 1 thread on how to design the program. We will be beginning Project 2 next week. Project 2 will be partially based upon project 1. If you wish to continue on the projects, you'll want to get project 1 finished this week. Please remember to use OPINION and WARNING tags whenever applicable. As well, feel free to post your own insights, and review questions or exercises beginning Wednesday or Thursday. Outline of the Reading - Chapter 14
  1. Problems with Single Inheritance
  2. Multiple Inheritance
  3. Abstract Data Types

Good Luck!

[Edited by - jwalsh on May 30, 2007 1:05:13 PM]
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Advertisement
Sorry for posting a general question here but older threads dont generaly get much attention.

My question is about pointer and references. When is it agood idea to pass by reference instead of passing by value.Always or only when you want to pass an object.Is it realy nessesary to pass by reference when you only need to pass an int?

Thanks in advance for your aswers.
Quote:
My question is about pointer and references. When is it agood idea to pass by reference instead of passing by value.Always or only when you want to pass an object.Is it realy nessesary to pass by reference when you only need to pass an int?


Make a distinction between const reference and 'plain' reference. Only pass by 'plain' reference when you intend for the function to modify the value. Choosing between const reference and by value is not as simple. Prefer to pass by const reference when the object is large, is uncopyable, or needs to maintain polymorphic behavior. Pass primitives (like int) by value. Prefer to pass objects by value when copying them is not expensive, or it's logical to copy them.


[Edited by - Ham Ho on September 14, 2006 11:22:56 PM]
Please take this conversation to either the thread on pointers or references. Contraty to the OP's opinion, the older threads do get attention, because they get moved to the top of the list.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Thanks for the aswer Deyja.


jwalsh : Understood i will post further post on the right tread.
Sorry again
Hi everyone I have just registered on game dev, and im glad, as I find this C++ workshop very useful. I think the extra questions and exercises are very useful and I would like to thank the people that have put them together. I would also like to thank all the tutors for spending their time and sharing their wisdom with us.

I am abit behind but I have this week off work so I hope to catch up with you guys. I have posted something on the forum for chapter four I hope it sheds abit more light on the short circuit evaluation question.

Sorry if i am rambling on but agian I would like to thank everyone who is working on this forum. I am also looking forward with working with everyone through this book not as a tutor but as a student.
Greetings All!

It's once again QUIZ TIME!!! That's right, listed below are a set of quiz questions to help you test your knowledge and understanding of the material contained in chapters 14.

In addition to the questions and exercises below, make sure that as you're reading the book you enter the examples into your compiler, build the program, and run the executable. I know this is a time consuming process, but the repeat use of keywords, syntax, and semantics will help ingrain the information into your long-term memory. My advice is to create a simple "driver" project with a function main. As you read, enter the examples into function main, test it, and then erase it for use again in the next example.

PLEASE DO NOT POST THE ANSWERS TO THESE QUESTIONS OR EXERCISES. If you are unable to answer these questions, please ask for assistance, but DO NOT POST THE ANSWERS. Any question which is not marked with [Extra Credit] can be answered by reading your textbook. Questions which are marked [Extra Credit] either have been answered in the thread previously, or can be answered by doing a bit of research.

I will create an answer thread for these questions immediately, so that people will have a chance to get the answers more quickly.

Chapter 14 Quiz

1. According to the text, what happens if you “percolate” too much functionality from derived classes into the base class.
2. What cast operator can be safely used to down-cast a base class to a derived class? What happens if the class you’re trying to cast doesn’t match the target class.
3. If you’re using multiple inheritance with classes that have no default constructor, but does have constructors with parameters what must you do?
4. What must you do if you multipley inherit from two classes which have a function with the same name and signature?
5. What must you do if you multipley inherit two classes which share a common base class?
6. What does virtual inheritance do? What are the benefits?
7. Do you need to use the term “virtual” in derived classes if the base class already has the function marked as virtual? Should you?
8. How do you create an abstract data type (abstract class)?
9. Can you instantiate an object of an abstract data type?
10. What must you do in a class derived from an abstract data type before you can instantiate an object of the derived type?
11. Is it possible to provide an implementation for a pure virtual function? Why would you do it?
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Well, another chapter done, must say, this was the toughest one yet. Going to work on Project 1 now before continuing

Chapter 14.

1) According to the text, what happens if you "percolate" too much functionality from derived classes into the base class?
A) Then the base class becomes like a global namespace for all the functions that might be used by any of the derived classes. This can seriously undermine the class typeing of C++, and create a large and cumbersome base class.

2) What cast operator can be safely used to down-cast a base class to a derived class? What happens if the class you're trying to cast doesn't match the target class?
A) The dynamic_cast<> operator can be safely used for this, incase of a non matching target class, the pointer becomes a null pointer.

3) If you're using multiple inheritance with classes that have no default constructor, but does have constructors with parameters what must you do?
A) Write your own default constructor.

4) What must you do if you multiply inherit from two classes which have a function with the same name and signature?
A) You must make an explicit call to the class its specific function you want to invoke.

5) What must you do if you multiply inherit two classes which share a common base class?
A) You must make the base class virtual.

6) What does virtual inheritance do? What are the benefits?
A) Virtual inheritance let's subclasses belong to the same base class, the benefits are that calling the correct function of the derived class, does not have to be done by explicitly calling the class itself.

7) Do you need to use the term "virtual" in derived classes if the base class already has the function marked as virtual? Should you?
A) No, it's not necessary, but you should to make things more clear.

8) How do you create an abstract data type(abstract class)?
A) You create it by creating a pure virtual function in the class, if one of it's functions is pure, then the class is an ADT(Abstract Data Type).

9) Can you intantiate an object of an abstract data type?
A) No, you can't instantiate an object of an abstract data type, trying to do so will cause a compile error.

10) What must you do in a class derived from an abstract data type before you can instantiate an object of the derived type?
A) You must override the pure virtual functions your class inherited from the ADT.

11) Is it possible to provide an implementation for a pure virtual function? Why would you do it?
A) Yes, it can, it would be done to for instance provide common functionality to all the overriden functions.
Quote:
3) If you're using multiple inheritance with classes that have no default constructor, but does have constructors with parameters what must you do?
A) Write your own default constructor.


That is one way [smile], but not always possible/appropriate. Instead, use the initialiser list to pass arguments to the base classes. Just like you would in a non-multiple inheritance situation if you wanted to pass parameters to the base class.

Quote:
5) What must you do if you multiply inherit two classes which share a common base class?
A) You must make the base class virtual.


By this do you mean inherit virtually?

I.E.

class Super {};
class SubOne : virtual public Super {};
class SubTwo : virtual public Super {};
class Example : public SubOne, SubTwo {};

Read more here.

Quote:
6) What does virtual inheritance do? What are the benefits?
A) Virtual inheritance let's subclasses belong to the same base class, the benefits are that calling the correct function of the derived class, does not have to be done by explicitly calling the class itself.


You're wording here is ambiguous (which is understandable, its a difficult subject to put into words).

What virtually inheritance enables is further down in the inheritance chain, there will only be one object of the "Super" ( in the example above ).

Without virtual inheritance, your object looks like this (note: this is for illustrative purposes only, compilers have freedom in memory layouts for objects):
Super -   - class Super dataSubOne   - class Super data   - class SubOne dataSubTwo   - class Super data   - class SubTwo dataExample -   - class Super data (for subone)   - class SubOne data   - class Super data (for subtwo)   - class SubTwo data   - class Example data


With virtual inheritance, the compiler uses pixie magic (or equivalent) to figure out a system like this:

Super -   - class Super dataSubOne   - class Super data reference   - class SubOne dataSubTwo   - class Super data reference   - class SubTwo dataExample -   - class Super data (shared)   - class SubOne data   - class SubTwo data   - class Example data

This topic is closed to new replies.

Advertisement