Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C++ Function Overriding Explained – Practical Examples & Best Practices

C++ Function Overriding Explained

This guide demystifies function overriding in C++, providing clear examples, best practices, and a step‑by‑step walk‑through that a seasoned C++ developer would use in production.

Why Function Overriding Matters

In object‑oriented programming, inheritance lets a derived class extend or modify the behavior of a base class. When the same member function is declared in both the base and the derived class, the derived implementation “overrides” the base one. When you call that function through a derived‑class object, the overridden version runs.


Example 1: Basic Overriding

#include <iostream>
using namespace std;

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

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

int main() {
    Derived d;
    d.print();
    return 0;
}

Output

Derived Function

Calling print() on a Derived instance executes the overridden function, not the base version.

C++ Function Overriding Explained – Practical Examples & Best Practices

If you create a Base object and call print(), you’ll see the original base implementation.

Base b;
b.print(); // Output: Base Function

Accessing the Overridden Base Function

Sometimes you need to invoke the base class’s version explicitly. Two common techniques are:

Example 2: Scope Resolution to Call Base Function

#include <iostream>
using namespace std;

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

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

int main() {
    Derived d1, d2;
    d1.print();
    d2.Base::print(); // Explicit call to Base::print
    return 0;
}

Output

Derived Function
Base Function

Example 3: Calling Base Function from Within Derived

#include <iostream>
using namespace std;

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

class Derived : public Base {
public:
    void print() {
        cout << "Derived Function" << endl;
        Base::print(); // Invoke base version
    }
};

int main() {
    Derived d;
    d.print();
    return 0;
}

Output

Derived Function
Base Function

Example 4: Using a Base Pointer to Access Base Function

#include <iostream>
using namespace std;

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

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

int main() {
    Derived d;
    Base* ptr = &d; // Pointer to Base pointing to Derived object
    ptr->print(); // Calls Base::print because the function isn’t virtual
    return 0;
}

Output

Base Function

When the function is not declared virtual, a base‑class pointer invokes the base implementation. To achieve true polymorphic behavior, declare the base function virtual and override it in the derived class.


These examples illustrate the mechanics of function overriding in C++. By mastering these concepts, you can write cleaner, more maintainable code that leverages polymorphism effectively.

C Language

  1. Mastering C++ Functions: From Basics to Advanced Usage
  2. C++ Function Overloading: A Practical Guide
  3. How to Pass Arrays to Functions in C++: A Practical Guide
  4. C++ Friend Functions and Friend Classes: Mastering Access Control
  5. Mastering C++ Virtual Functions: Concepts, Examples, and the Override Keyword
  6. C++ Operator Overloading – A Practical Guide with Code Examples
  7. C++ Polymorphism Explained: Practical Examples & Key Concepts
  8. C++ Functions Explained with Practical Code Examples
  9. Mastering C++ Overloading: Functions & Operators Explained
  10. Understanding Polymorphism in C++: A Practical Guide