2080 12

CSC166 ยท 2080 ยท 5 marks

Explain the reason for member function overriding when using virtual function.

Explain the reason for member function overriding when using virtual function. [5]

Object Oriented ProgrammingChapter 6: Virtual Function, Polymorphism, and miscellaneous C++ FeaturesConcept of Virtual functions

Quick answer

Overriding means defining a member function with the same name in both the base class and the derived class. When such a function exists in both classes, the derived class function overrides the base class function.

Member Function Overriding with Virtual Functions

Definition and Concept

Virtual function is a member function in the base class that is declared using the keyword virtual. When virtual functions are used, different functions can be executed by the same function call statement.


Reason for Member Function Overriding When Using Virtual Functions

The Core Problem (Without Virtual)

When a base class pointer is used to point to a derived class object, without virtual, the function call is resolved at compile time (static binding). This means the base class version of the function always gets called, even when the pointer is pointing to a derived class object. This defeats the purpose of inheritance and polymorphism.

How Virtual Solves It

When a function is declared virtual in the base class, the function call is resolved at runtime (dynamic binding). The function called is determined by the actual type of the object pointed to, not the type of the pointer.

As per the notes: "When a member function is virtual, the function called is determined by the actual most derived type of the object named by the expression left of the dot or pointed to by the expression left of the arrow. This is called the dynamic type."


Types of Polymorphism Involved

TypeHow AchievedBinding
Compile-time (Static)OverloadingAt compile time
Runtime (Dynamic)Overriding with virtualAt runtime

Example

#include <iostream>
using namespace std;

class base {
public:
    virtual void print() {          // virtual function
        cout << "Print base class" << endl;
    }
    void show() {                   // non-virtual function
        cout << "Show base class" << endl;
    }
};

class derived : public base {
public:
    void print() {                  // overrides base class print()
        cout << "Print derived class" << endl;
    }
    void show() {
        cout << "Show derived class" << endl;
    }
};

int main() {
    base *bptr;
    derived d;
    bptr = &d;          // base pointer pointing to derived object

    bptr->print();      // virtual function -> runtime binding
    bptr->show();       // non-virtual function -> compile-time binding
    return 0;
}

Output

Print derived class
Show base class

Explanation of Output

  • bptr->print() calls the derived class version because print() is declared virtual in the base class. The function is bound at runtime based on the actual object type (derived).
  • bptr->show() calls the base class version because show() is not virtual. It is bound at compile time based on the pointer type (base).

Key Points

  1. Runtime polymorphism is achieved only through a pointer or reference of a base class type.
  2. A base class pointer can point to objects of both base and derived classes.
  3. If a virtual function in the base class is overridden in the derived class, the virtual keyword is not required again in the derived class; the function is automatically considered virtual.
  4. Without virtual, member function overriding cannot achieve dynamic dispatch, making the concept of runtime polymorphism impossible.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.