---
title: "Member function overriding with virtual function"
subject: "Object Oriented Programming"
chapter: 7
key: "semester_3/object_oriented_programming:virtual-member-override-reason"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/virtual-member-override-reason
site: BCT Engineering
---

# Member function overriding with virtual function

Object Oriented Programming · Chapter 7: Polymorphism and Dynamic Binding

- Asked in: 2072 Kartik, 5 marks
- Video: [C++ Polymorphism and Virtual Member Functions [6]](https://www.youtube.com/watch?v=4NPOIaUxnnk) — Professor Hank Stalica
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/virtual-member-override-reason
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-7

## Question

Explain the reason for member function over-riding when using virtual function.

## Short answer

Overiding a virtual function enables run-time polymorphism, so a base pointer/reference calls the derived version depending on the actual object type. This allows a uniform interface to handle different behaviors, adding new derived classes without modifying existing code.

## Answer

**Overriding** means a derived class redefines a base class member function with the same name, return type and parameters. When the base function is declared `virtual`, the version to run is chosen by the **type of the object**, not by the type of the pointer or reference.

#### Why overriding is needed
- A derived class often needs behaviour different from the base class (a `Circle` computes area differently from a `Rectangle`).
- Without `virtual`, a base pointer always calls the base version (static binding), so the derived behaviour is ignored.
- With `virtual`, the compiler creates a vtable; the call through a base pointer goes to the overriding function of the actual object at run time (dynamic binding).
- It lets one interface (`base->show()`) work for many different object types, so new derived classes can be added without changing the calling code.

#### Example

```cpp
#include <iostream>
using namespace std;

class Base {
public:
    virtual void show() { cout << "Base show\n"; }
    void display()      { cout << "Base display\n"; }  // not virtual
};
class Derived : public Base {
public:
    void show()    { cout << "Derived show\n"; }       // overrides
    void display() { cout << "Derived display\n"; }    // only hides
};

int main() {
    Derived d;
    Base *p = &d;
    p->show();      // Derived show   (virtual, run-time binding)
    p->display();   // Base display   (compile-time binding)
    return 0;
}
```

The result shows that only the virtual function is overridden and resolved at run time; the non-virtual one is bound by the pointer type.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is function overriding?
   A. Same name with different parameters in one class
   B. Derived class redefines a base function with the same name, return type and parameters
   C. Calling a function twice
   D. Making a function private
   Answer: B. Overriding redefines the base function with identical signature in the derived class. (Key point: Overriding)

2. Why is overriding needed with virtual functions?
   A. To make functions private
   B. To save memory
   C. So derived classes can give their own behaviour reached through a base pointer
   D. To prevent inheritance
   Answer: C. Different derived types need different behaviour, called through one base interface. (Key point: Why needed)

3. In the example, p->display() (non-virtual) prints what?
   A. Both
   B. Nothing
   C. Base display
   D. Derived display
   Answer: C. Non-virtual functions are bound by pointer type; Derived::display() only hides the base one. (Key point: Non-virtual result)

## Related in Object Oriented Programming

- [Function overriding versus overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overriding-vs-overloading) — Object Oriented Programming, chapter 6, asked 2×
- [Function overriding and how to resolve it](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overriding-resolve) — Object Oriented Programming, chapter 6
- [Pure virtual function and abstract class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apure-virtual-abstract-class) — Object Oriented Programming, chapter 7, asked 5×
- [Need of virtual function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avirtual-function-need) — Object Oriented Programming, chapter 7, asked 4×
- [Static data member and member function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astatic-member-function) — Object Oriented Programming, chapter 4, asked 3×
- [Member function as friend of another class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amember-function-friend-of-other-class) — Object Oriented Programming, chapter 4

## Related in other subjects

- [Virtual machine structure and benefits](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Avirtual-machine-structure) — Operating System, chapter 1, asked 2×
- [Bridge compression member, ISLC 400, toe to toe](https://www.bctengineer.com/question.md?key=semester_6%2Fdesign_of_steel_and_timber_structure%3Abridge-compression-islc400) — Design of Steel and Timber Structure, chapter 6
- [Valid lossless function; Foster II and Cauer II](https://www.bctengineer.com/question.md?key=semester_5%2Ffilter_design%3Alossless-three-functions-foster-cauer-ii) — Filter Design, chapter 4, asked 2×
- [Valid lossless function; Foster series and Cauer I](https://www.bctengineer.com/question.md?key=semester_5%2Ffilter_design%3Alossless-four-functions-foster-series) — Filter Design, chapter 4, asked 2×
