Base Class Pointer with Derived Class Object in C++

 


1️⃣ What is Base Class Pointer with Derived Class Object?

In C++, a base class pointer can point to an object of a derived class.
This allows us to achieve polymorphism, enabling dynamic method overriding.

Key Features:

✅ A base class pointer can hold the address of a derived class object.
✅ Only base class members (non-overridden ones) can be accessed unless virtual functions are used.
✅ Enables dynamic dispatch when using virtual functions.


2️⃣ Example: Base Class Pointer Holding Derived Class Object

#include <iostream>
using namespace std;

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

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

int main() {
    Derived d;
    Base* ptr = &d; // Base class pointer holding derived class object

    ptr->show(); // Calls Base class function (NO dynamic binding)
    
    return 0;
}

🔹 Output

Base class function

🔴 Why did it call Base::show() instead of Derived::show()?
Because without virtual, function binding is static (decided at compile-time).


3️⃣ Using virtual for Dynamic Binding (Method Overriding)

Solution: Use virtual Keyword

#include <iostream>
using namespace std;

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;
    }
};

int main() {
    Derived d;
    Base* ptr = &d; // Base class pointer holding derived class object

    ptr->show(); // Calls Derived class function (DYNAMIC BINDING)
    
    return 0;
}

🔹 Output

Derived class function

Now, Derived::show() is called dynamically at runtime.


4️⃣ Another Example: Virtual Function with Multiple Derived Classes

#include <iostream>
using namespace std;

class Animal {
public:
    virtual void makeSound() {
        cout << "Animal makes a sound" << endl;
    }
};

class Dog : public Animal {
public:
    void makeSound() override {
        cout << "Dog barks" << endl;
    }
};

class Cat : public Animal {
public:
    void makeSound() override {
        cout << "Cat meows" << endl;
    }
};

int main() {
    Dog d;
    Cat c;
    Animal* ptr;

    ptr = &d;
    ptr->makeSound(); // Calls Dog::makeSound()

    ptr = &c;
    ptr->makeSound(); // Calls Cat::makeSound()

    return 0;
}

🔹 Output

Dog barks
Cat meows

Why does it work?
Because makeSound() is virtual, so the correct function is called at runtime.


5️⃣ Important Points About Base Class Pointers

Without virtual, only base class functions are called.
With virtual, derived class functions override base functions dynamically.
Polymorphism is achieved using base class pointers & virtual functions.
Helps in runtime method selection (dynamic dispatch).

Would you like an example with multiple levels of inheritance? 🚀

Post a Comment

0 Comments