---
title: "Function overriding and how to resolve it"
subject: "Object Oriented Programming"
chapter: 6
key: "semester_3/object_oriented_programming:function-overriding-resolve"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/function-overriding-resolve
site: BCT Engineering
---

# Function overriding and how to resolve it

Object Oriented Programming · Chapter 6: Inheritance

- Asked in: 2074 Chaitra, 4 marks
- Video: [Function Overriding in C++ | Inheritance | OOP in C++ programming](https://www.youtube.com/watch?v=Zd_4xa071nc) — Simple Snippets
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/function-overriding-resolve
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-6

## Question

What is function overriding? How can it be resolved? Explain with a suitable example.

## Short answer

Function overriding is redefining a base function with identical signature in a derived class. Resolution is static (based on pointer/reference type) for non-virtual functions; dynamic via virtual functions where the actual object type decides at runtime; the overridden base version can be explicitly called with Base::function.

## Answer

**Function overriding** means a derived class defines a function with the same name, return type and parameters as a function of its base class. The derived version hides the base version for derived objects. The question of "resolving" is deciding which version is called.

**Resolution:**
1. **Static (compile-time) resolution** - the version is chosen by the type of the object or pointer. A derived object calls the derived function; a base pointer calls the base function.
2. **Scope resolution operator** - the hidden base version can be called explicitly as `Base::fun()`.
3. **Virtual functions (dynamic resolution)** - if the base function is `virtual`, the version is chosen at run time by the actual object type, even through a base pointer.

**Example:**

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

class Base {
public:
    void show()          { cout << "Base::show" << endl; }
    virtual void print() { cout << "Base::print" << endl; }
};

class Derived : public Base {
public:
    void show()  { cout << "Derived::show" << endl; }
    void print() { cout << "Derived::print" << endl; }
};

int main() {
    Derived d;
    Base *p = &d;

    d.show();          // Derived::show (derived object)
    d.Base::show();    // Base::show (scope resolution)
    p->show();         // Base::show (non-virtual: pointer type decides)
    p->print();        // Derived::print (virtual: object type decides)
    return 0;
}
```

**Output:**

```text
Derived::show
Base::show
Base::show
Derived::print
```

## Check yourself

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

1. For non-virtual show() overridden in Derived, what does Base *p = &d; p->show() call?
   A. Derived::show
   B. Base::show, since pointer type decides
   C. Neither
   D. Both
   Answer: B. Without virtual, the version is chosen at compile time by the pointer type. (Key point: Static resolution)

2. With virtual print() in Base, what does p->print() call?
   A. Base::print
   B. Derived::print, decided by the actual object at run time
   C. Compile error
   D. It depends on the pointer type
   Answer: B. Virtual functions use dynamic resolution by object type. (Key point: Virtual)

3. How can the hidden base version be called on a derived object d?
   A. d.Base::show()
   B. d.show()
   C. Base(d)
   D. d->show
   Answer: A. Base::show() with the scope resolution operator selects it explicitly. (Key point: Scope operator)

## 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×
- [Member function overriding with virtual function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avirtual-member-override-reason) — Object Oriented Programming, chapter 7
- [Ambiguity and how to resolve it](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aambiguity-resolve-example) — Object Oriented Programming, chapter 6
