Function Overriding in C++

 


1️⃣ What is Function Overriding?

Function Overriding in C++ occurs when a derived class provides a new implementation of a function that is already defined in its base class.

Key Features:

✅ Same function name in both base and derived classes.
✅ Same parameters in both functions.
✅ Uses virtual keyword in the base class to enable runtime polymorphism.
✅ Achieved through inheritance.


2️⃣ Syntax of Function Overriding

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

class Derived : public Base {
public:
    void show() override { // Overriding function
        cout << "Derived class function" << endl;
    }
};

3️⃣ Example: Function Overriding in C++

#include <iostream>
using namespace std;

// Base Class
class Parent {
public:
    virtual void show() {  // Virtual function
        cout << "This is the Parent class function." << endl;
    }
};

// Derived Class
class Child : public Parent {
public:
    void show() override { // Overriding the function
        cout << "This is the Child class function." << endl;
    }
};

int main() {
    Parent* p;  // Pointer of base class
    Child c;   
    p = &c;     // Assign child object to base class pointer
    p->show();  // Calls Child's show() function (Runtime Polymorphism)
    
    return 0;
}

🔹 Output

This is the Child class function.

4️⃣ Why Use Function Overriding?

🔹 Achieves Runtime Polymorphism - Calls the correct function at runtime.
🔹 Improves Code Reusability - Allows specialized behavior for derived classes.
🔹 Supports Object-Oriented Design - Enables flexibility in base class inheritance.


5️⃣ Rules of Function Overriding

✔ The base class function must be virtual (virtual void functionName()).
✔ The overridden function in the derived class must have the same signature (name, parameters, and return type).
✔ Access specifier (public/private/protected) must be compatible.
✔ Use override keyword (optional but recommended) to avoid accidental mistakes.


6️⃣ Function Overriding vs Function Overloading

Feature Function Overriding Function Overloading
Definition Redefining a function in a derived class Defining multiple functions with the same name but different parameters
Inheritance Required? ✅ Yes ❌ No
Function Signature Must be identical to base class Must have different parameters
Access Specifier Must be public/protected Can be any access specifier
Polymorphism Runtime Polymorphism Compile-time Polymorphism

7️⃣ Real-World Example: Banking System

#include <iostream>
using namespace std;

// Base Class
class BankAccount {
public:
    virtual void accountType() {
        cout << "This is a generic bank account." << endl;
    }
};

// Derived Class: Savings Account
class SavingsAccount : public BankAccount {
public:
    void accountType() override {  // Overriding function
        cout << "This is a Savings Account." << endl;
    }
};

// Derived Class: Current Account
class CurrentAccount : public BankAccount {
public:
    void accountType() override {  // Overriding function
        cout << "This is a Current Account." << endl;
    }
};

int main() {
    BankAccount* account1;
    SavingsAccount savings;
    CurrentAccount current;

    account1 = &savings;
    account1->accountType(); // Calls SavingsAccount's function

    account1 = &current;
    account1->accountType(); // Calls CurrentAccount's function

    return 0;
}

🔹 Output

This is a Savings Account.
This is a Current Account.

8️⃣ Summary

✔ Function Overriding allows a derived class to redefine a function from the base class.
✔ Uses virtual keyword in the base class for runtime polymorphism.
✔ Base class pointers can call overridden functions in derived classes dynamically.
✔ Helps create flexible and reusable object-oriented programs.

Would you like an example with abstract classes (pure virtual functions)? 🚀

Post a Comment

0 Comments