Table of Contents

1. Reviewing classes and pointers

Review: Class inheritance and function overriding

questioninherit.png

Here we have a parent class Question and a child class MultipleChoiceQuestion:

class Question                        class MultipleChoiceQuestion : public Question
{                                     {
              public:                                   public:
              Question();                               MultipleChoiceQuestion();
              ~Question();                              ~MultipleChoiceQuestion();
              bool AskQuestion();                       bool AskQuestion();
              void DisplayQuestion();                   void ListAllAnswers();

              protected:                                protected:
              string m_question;                        int m_correct;
};                                            string m_options[4];
                                     };

Some things to remember about inheritance with classes:

  • Any public or protected members (functions and variables) are inherited by the child class. (e.g., m_question, DisplayQuestion(), and AskQuestion()). private members remain accessible only to the class it was declared within.
  • A child class can override the a parent's function by declaring and defining a function with the same signature. (e.g., AskQuestion()).
  • If the child class doesn't override a parent's function, then when that function is called via the child object it will call the parent's version of that function. (e.g., Question::DisplayQuestion()).

Review: Pointers to class objects

You can declare a pointer to point to the address of an existing object, or use the pointer to allocate memory for one or more new instances of that class…

  • Pointer to existing address:

    myPtr = &existingQuestion;
    
  • Pointer to allocate memory for 1 variable:

    myPtr = new Question;
    

Then, to access a member of that object via the pointer, we use the -> operator, which is equivalent to dereferencing the pointer and then accessing a member:

  • Arrow operator:

    myPtr->DisplayQuestion();
    
  • Dereference and access:

    (*myPtr).DisplayQuestion();
    

Note that you can use pointers to create a dynamic array by allocating space for an array, OR just allocate space for one variable at a time, which can also be useful.

  Single variable Array
Allocate space TYPE* ptr = new TYPE; TYPE* ptr = new TYPE[size];
Deallocate space delete ptr; delete [] ptr;

Review review :)

Review 1. A child-class will inherit its parents… (Select all correct answers):

  • □ public members
  • □ protected members
  • □ private members

Review 2. The -> operator…

  • ○ accesses an element of an array pointer at some index
  • ○ dereferences an object pointer and accesses a member at the same time.
  • ○ passes a pointer by-reference to a function

2. Design and polymorphism

So much of the design tricks and features we utilize in C++ and other object-oriented programming languages all stem from the concept of "do not repeat yourself". If you're writing the same set of code in multiple places, there is a chance that we could design the program so that we only need to write that code once.

family.png

Polymorphism is a way that we can utilize pointers and something called vtables to have a family of classes (related by inheritance) and be able to write one set of code to handle interfacing with all of those family members. We have a family tree of classes, and we can write our program to treat all the objects as the parent class, but the program will decide which set of functions to call at run time.

~

Form: Using Polymorphism to treat ChildA and ChildB objects as their Parent object

Parent* myPtr = nullptr;
if      ( type == 1 ) { myPtr = new ChildA; }
else if ( type == 2 ) { myPtr = new ChildB; }

myPtr->Display();
delete myPtr;

Review 3. We'll cover how this works in a bit, but for now the main concept is that using Polymorphism…

  • ○ Child-classes can inherit from multiple parent-classes via a pointer.
  • ○ Child-classes can inherit from multiple parent-classes via a pointer.
  • ○ We can treat descendant-classes as their parent-class via a pointer.

Example: Quizzer and multiple question types

Let's say we are writing a quiz program and there are different types of questions: True/false questions, multiple choice, and fill-in-the-blank. They all have a common question string, but how they store their answers is different…

Class diagrams:

Question
# m_question : string
+ bool AskQuestion()
+ void DisplayQuestion()
TrueFalseQuestion
# m_question : string
# m_answer : bool
+ bool AskQuestion()
MultipleChoiceQuestion
# m_question : string
# m_options : string[4]
# m_correct : int
+ bool AskQuestion()
+ void ListAllAnswers()
FillInQuestion
# m_question : string
# m_answer : string
+ bool AskQuestion()

How would you store a series of inter-mixed quiz questions in a program? Without polymorphism, you might think to just have separate vectors or arrays for all the questions:

Example: Not using Polymorphism - Having to create separate vectors for each Question type

vector<TrueFalseQuestion>       tfQuestions;
vector<MultipleChoiceQuestion>  mcQuestions;
vector<FillInQuestion>          fiQuestions;

Utilizing polymorphism in C++, we could simply store an array of pointers of the parent type:

Example: Creating a vector of parent class pointers

vector<Question*> questions;

And then initialize the question as the type we want during creation:

Example: Storing different Question types in one vector

questions.push_back(new TrueFalseQuestion);
questions.push_back(new MultipleChoiceQuestion);
questions.push_back(new FillInQuestion);

Since we are using the new keyword here, we would also need to make sure to delete these items at the end of the program:

~

Example: Iterating over all the Question objects in the vector

for (auto& question : questions)
{
    delete question;
}
Other design considerations

When we're working with polymorphism in this way, we need to be able to treat each child as its parent, from a "calling functions" perspective. Each child can have its own unique member functions and variables, but when we're making calls to functions via a pointer to the parent type, the parent only knows about functions that it, itself, has.

Let's say that the Question class has a DisplayQuestion() function. Since all its children use m_question in the same way and inherit this function, it will be fine to call it via the pointer.

Example: Calling a common function (DisplayQuestion) that is part of the Question family tree

ptrQuestion->DisplayQuestion(); // ok

But with a function that belongs to a child - not the parent's interface - we wouldn't be able to call that function via the pointer without casting.

Example: Calling a function that doesn't belong to the Question family - just one of the subclasses

ptrQuestion->ListAllAnswers();  // not ok

(static_cast<MultipleChoiceQuestion*>(ptrQuestion))
   ->ListAllAnswers(); // ok

You could, however, still call that ListAllAnswers function from within MultipleChoiceQuestion's DisplayQuestion function, and that would still work fine…

Example: Calling the specialized function from within the general function

bool MultipleChoiceQuestion::AskQuestion()
{
    DisplayQuestion();
    ListAllAnswers();
    // etc.
}

Still fuzzy? That's OK, this is just an overview; we're going to step into how all this works more in-depth next.

3. Which version of the method is called?

Let's say we have several objects already declared:

Question q1, q2;
MultipleChoiceQuestion mc1;

We could create a Question* ptr that points to q1 or q2 or even mc1…

Question* ptr;
ptr = &q1;  // Question* pointing to a Question address
ptr = &q2;  // Question* pointing to a Question address
ptr = &mc1; // Question* pointing to a MultipleChoiceQuestion address...?

questioninherit.png

And, any functions that the Question class and the MultipleChoiceQuestion class could be called from this pointer…

// Every Question child just calls the Question::DisplayQuestion() function.
ptr->DisplayQuestion();

This is fine for any member methods not overridden by the child class. But, which version of the function is called if we used an overridden method?

// Every Question family class has its own AskQuestion() function.
ptr->AskQuestion();

So which is called - Question::AskQuestion(), or MultipleChoiceQuestion::AskQuestion()?

whichone.png

No virtual methods - Which AskQuestion() is called?

Let's say our class declarations look like this:

Question parent class declaration:

class Question
{
    public:
    bool AskQuestion();
    // etc.
};

MultipleChoiceQuestion declaration:

class MultipleChoiceQuestion : public Question
{
    public:
    bool AskQuestion();
    // etc.
};

Here are the outputs we could have from using pointers in different ways:

A. Question* pointer – Question's AskQuestion() is called:

Question* ptr = new Question;
bool result = ptr->AskQuestion();

B. MultipleChoiceQuestion* pointer – MultipleChoiceQuestion's AskQuestion() is called:

MultipleChoiceQuestion* ptr = new MultipleChoiceQuestion;
bool result = ptr->AskQuestion();

C. Question* pointer – Question's AskQuestion() is called:

Question* ptr = new MultipleChoiceQuestion;
bool result = ptr->AskQuestion();

Review 4. Given these class declarations:

class Task
{
    public:
    bool Action();
    // etc.
};

class SpecializedTask : public Task
{
    public:
    bool Action();
    // etc.
};
  • a. Which version of Action() is called for the following code?

    SpecializedTask* ptr = new SpecializedTask;
    bool result = ptr->Action();
    

    ○ Task::Action() / ○ SpecializedTask::Action()

  • b. Which version of Action() is called for the following code?

    Task* ptr = new Task;
    bool result = ptr->Action();
    

    ○ Task::Action() / ○ SpecializedTask::Action()

  • c. Which version of Action() is called for the following code?

    Task* ptr = new SpecializedTask;
    bool result = ptr->Action();
    

    ○ Task::Action() / ○ SpecializedTask::Action()

"Well, how is that useful at all? The function called matches the pointer data type!" - true, but we're missing one piece that allows us to call any child's version of the method from a pointer of the parent type…

Virtual methods - Which AskQuestion() is called?

Instead, let's mark our method with the virtual keyword:

Question class with virtual function

class Question
{
    public:
    virtual bool AskQuestion();
    // etc.
};

MultipleChoiceQuestion class with virtual function

class MultipleChoiceQuestion : public Question
{
    public:
    virtual bool AskQuestion();
    // etc.
};

Here are the outputs we could have from using pointers in different ways:

A. Question* pointer – Question's AskQuestion() is called:

Question* ptr = new Question;
bool result = ptr->AskQuestion();

B. MultipleChoiceQuestion* pointer – MultipleChoiceQuestion's AskQuestion() is called:

MultipleChoiceQuestion* ptr = new MultipleChoiceQuestion;
bool result = ptr->AskQuestion();

C. Question* pointer – MultipleChoiceQuestion's AskQuestion() is called:

Question* ptr = new MultipleChoiceQuestion;
bool result = ptr->AskQuestion();

With this, we can now store a list of Question* objects, and each question can be a different child class, but we can write one set of code to interact with each one of them.

Review 5. Given these class declarations:

class Object
{
    public:
    virtual int GetX();
    // etc.
};

class PlayerCharacter : public Object
{
    public:
    virtual int GetX();
    // etc.
};
  • a. Which version of GetX() is called for the following code?

    PlayerCharacter* ptr = new PlayerCharacter;
    bool result = ptr->GetX();
    

    ○ Object::GetX() / ○ PlayerCharacter::GetX()

  • b. Which version of GetX() is called for the following code?

    Object* ptr = new Object;
    bool result = ptr->GetX();
    

    ○ Object::GetX() / ○ PlayerCharacter::GetX()

  • c. Which version of GetX() is called for the following code?

    Object* ptr = new PlayerCharacter;
    bool result = ptr->GetX();
    

    ○ Object::GetX() / ○ PlayerCharacter::GetX()

4. Virtual methods, late binding, and the Virtual Table

By using the virtual keyword, something happens with our functions - it allows the pointer-to-the-parent class to figure out which version of the method to actually call, instead of just defaulting to the parent class' version.

But how does this work?

The virtual keyword tells the compiler that the function called will be figured out later. By marking a function as virtual, it then is added to something called a virtual table - or vtable.

The vtable stores special pointers to functions. If a class contains at least one virtual function, then it will have its own vtable.

vtables1.png

With the Question class, it isn't inheriting any methods from anywhere else so the vtable reflects the same methods it has. But, we also have the child class that inherits DisplayQuestion() and overrides AskQuestion().

vtables2.png

Because of these vtables, we can then have our pointers reference this vtable when figuring out which version of a method to call. Doing this is called late binding or dynamic binding.

Review 6. By marking the AskQuestion() function as virtual…

Question* ptr1 = new Question;
bool result = ptr1->AskQuestion();
  • The vtable for ptr1 tells it to run:

    • ○ Question::AskQuestion()
    • ○ MultipleChoiceQuestion::AskQuestion()

    and

    Question* ptr2 = new MultipleChoiceQuestion;
    bool result = ptr2->AskQuestion();
    
  • The vtable for ptr2 tells it to run:
    • ○ Question::AskQuestion()
    • ○ MultipleChoiceQuestion::AskQuestion()

When should we use virtual?

Destructors should always be virtual.

If you're working with inheritance. By making your destructor virtual for each class in the family, you are ensuring that the correct destructor will be called when the object is destroyed or goes out of scope. If you don't make it virtual and utilize polymorphism, the correct destructor may not be called (i.e., Question's instead of MultipleChoiceQuestion's).

Constructors cannot be marked virtual

When the object is instantiated (e.g., ptr = new MultipleChoiceQuestion;) that class' constructor will be called already.

Not every function needs to be virtual.

It's all about design. Though generally, if you always want the parent's version of a method to be called, you wouldn't override that method in the child class anyway.

5. Designing interfaces with pure virtual functions and abstract classes

Polymorphism works best if you're designing a family of classes around some sort of interface that they will all share. In the C# language, there is an interface type that is available to you, but that's not here in C++, so we implement it via classes.

What is an Interface?

When we're designing a class to be an interface, the idea is that the user (or other programmers) will just see a set of functions it will interface with - none of the behind-the-scenes, how-it-works stuff.

Most of the devices we use have some sort of interface, hiding the more complicated specifics of how it actually works within a case. For example, a calculator has a simple interface of buttons, but if you opened it up you would be able to see its hardware and how everything is hooked up.

We use the same idea with writing software, where we expose some interface (in the form of the class' public methods) as how the "user" interacts with our class.

questionmanager.png

A common design practice is to write the first base (parent) class to be a specification of this sort of interface that all its children will adhere to, and to ensure that each child class must follow the interface by using something that the compiler will enforce itself: pure virtual functions.

When working with our Quiz program idea, our base class is Question, which would define the interface for all other types of Questions. Generally, our base interface class would never be instantiated - it is not complete in and of itself (i.e., a Question with no types of Answers) - but is merely used to outline a common interface for its family members.

Here is a blank diagram with just the member variables defined, but not yet any functionality, so that we can begin to step through thinking about an interface:

questionfamily.png

Thinking in terms of implementing a program that could edit questions (such as the teacher's view of the quiz), as well as that could ask questions (such as the student's view), we can try to think of what kind of functionality we would need from a question…

  • Setup the question, answer(s)
  • Display the question to the user
  • Get the user's answer
  • Check if the user's answer was correct

But, the specifics of how each of these question types stores the correct answer (and what data type it is) and validates it differ between each of them…

  User answer Stored answer Validate
True/false bool bool answer Userinput == answer?
MultiChoice int string options[4] Userinput == answer?
FillIn int string answer Userinput == answer?

We could design our Questions so that they have functionality that interacts with the user directly (e.g., a bool function that asks the user to enter their response and returns true if they got it right and false if not) rather than writing functions around returning the actual answer (which would be more difficult because they have different data types).

  • Set up question
  • Run question
  • We can set up a simple interface for our Questions with these functions. They've been marked as virtual, which allows us to use polymorphism, and they've also been marked with = 0 at the end, marking them as pure virtual - this tells the compiler that child classes must implement their own version of these methods. A function that contains pure virtual methods is called an abstract class.

Example: Question interface with pure virtual functions

class Question
{
  public:
  virtual void Setup() = 0;
  virtual bool Run() = 0;

  protected:
  string m_question;
};

Now our child classes can inherit from Question. They will be required to override Setup() and Run(), and we can also have additional functions as needed for that implementation:

Example: Inheriting from the Question interface class

class MultipleChoiceQuestion : public Question
{
  public:
  virtual void Setup();
  virtual bool Run();
  void ListAllAnswers();

  protected:
  string m_options[4];
  int m_answer;
};
Definitions:
Each class will have its own implementation of these interface functions, but since they're part of an interface, when we build a program around these classes later we can call all of them the same way.

Example: Question - Defining the Setup function

void Question::Setup() {
    cout << "Enter question: ";
    getline( cin, m_question );
}

Example: TrueFalseQuestion - Overwriting the Setup function calling the parent class' Setup function

void TrueFalseQuestion::Setup() {
    Question::Setup();
    cout << "Enter answer (0 = false, 1 = true): ";
    cin >> m_answer;
}

Example: MultipleChoiceQuestion

void MultipleChoiceQuestion::Setup() {
  Question::Setup();

  for ( int i = 0; i < 4; i++ )
    {
      cout << "Enter option " << i << ": ";
      getline( cin, m_options[i] );
    }

  cout << "Which index is correct? ";
  cin >> m_answer;
}

Example: FillInQuestion

void FillInQuestion::Setup() {
    Question::Setup();
    cout << "Enter answer text: ";
    getline( cin, m_answer );
}
Function calls:
Now, no matter what kind of question subclass we're using, we can utilize the same interface - and the same code.

Example: Using Polymorphism to create the question and calling the common Setup and Run functions

// Create the pointer
Question* ptr = nullptr;

// Allocate memory
if ( choice == "true-false" )
{
    ptr = new TrueFalseQuestion();
}
else if ( choice == "multiple-choice" )
{
    ptr = new MultipleChoiceQuestion();
}
else if ( choice == "fill-in" )
{
    ptr = new FillInQuestion();
}

// Set up the question
ptr->Setup();

// Run the question
ptr->Run();

// Free the memory
delete ptr;

And, utilizing this interface, we could then store a vector<Question*> and set up each question as any question subclass without any duplicate code.

Review 7. Marking a function as pure virtual in a class looks like…

  • ○ pure virtual RETURN FUNC( PARAMS );
  • ○ virtual RETURN FUNC( PARAMS ) = 0;
  • ○ virtual RETURN FUNC( PARAMS ) pure;

Review 8. When a function is made pure virtual, that means…

  • ○ Any child classes must implement their own version of that function.
  • ○ Any child classes cannot have their own implementation of that function and must use their parents' version.

Review 9. A class with pure virtual functions cannot be instantiated (that is, we cannot declare a variable whose data type is that class). Another term for this type of class is…

  • ○ An abstract class
  • ○ A polymorphic class
  • ○ A virtual class

6. Example usage: Game objects

Let's say we have created a family tree of game objects, starting at the most basic object that has an \((x, y)\) coordinate and dimensions:

GameObject
+ GameObject()
+ Setup( ... ) : void
+ SetTexture( ... ) : void
+ GetName() : string
+ Update() : void
+ Draw() : void
(etc)
# position : Vector2f
# name : string
# sprite : Sprite
# tx_coord : IntRect

Then we might have something like an unanimated item in the world, but maybe it has physics so it needs the ability to update, and maybe it has some properties you wouldn't see on a character, like the ability to pick it up, or heal a character.

Item (inherits from GameObject)
+ Setup( ... ) : void
(etc)
# can_pick_up : bool
# heal_amount : int

But then our player and NPC characters also have animated sprites and the ability to move with keyboard input or rudimentary AI:

Character (inherits from GameObject)
+ Setup( ... ) : void
+ SetSpeed( float speed ) : void
+ SetDirection( int dir ) : void
+ Move( int dir ) : void
+ Animate() : void
(etc)
# speed : float
# direction : int

If we didn't use polymorphism, we would have to store all objects in their own vector:h

Example: Example of storing objects separately

vector<Character> m_npcList;
vector<Item> m_pickups;
vector<GameObject> m_decor;

But utilizing polymorphism, we can store one vector of GameObject* objects initialized on the heap, and any common functionality they have (Update, Draw, etc.) could be accessed via that pointer.

Example: Using Polymorphism for the game entities

// Our storage
vector<GameObject*> m_entities;

// Creating a new item (elseware in program)
GameObject* newItem = new Item;

// Adding it to the list
m_entities.push_back( newItem );

// Accessing it later
for ( auto& entity : entities )
{
  entity->Update();
}

Author: RachelWil

Created: 2026-10-14 Wed 11:12

Validate