Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Sunday, September 29, 2024

Inheritance and Polymorphism in C++

 

Inheritance and Polymorphism in C++

 

Inheritance and Polymorphism are key principles of object-oriented programming (OOP) that enable the creation of a hierarchical class structure and enhance the flexibility and extensibility of code. Let’s explore each concept in detail with examples in C++.

 

 Inheritance

Inheritance is a mechanism that allows a new class (derived class) to acquire properties and behaviors (methods) from an existing class (base class). It helps in reusing code and establishing a hierarchical relationship between classes.

 

Types of Inheritance:

1. Single Inheritance: A class derives from one base class.

2. Multiple Inheritance: A class derives from more than one base class.

3. Multilevel Inheritance: A class derives from a derived class, forming a chain of inheritance.

4. Hierarchical Inheritance: Multiple classes derive from a single base class.

5. Hybrid Inheritance: A combination of two or more types of inheritance.

 

Example of Inheritance:

 

#include <iostream>

using namespace std;

 

// Base class

class Animal {

public:

    void eat() {

        cout << "Animal eats food." << endl;

    }

   

    void sleep() {

        cout << "Animal sleeps." << endl;

    }

};

 

// Derived class

class Dog : public Animal {

public:

    void bark() {

        cout << "Dog barks." << endl;

    }

};

 

// Derived class

class Cat : public Animal {

public:

    void meow() {

        cout << "Cat meows." << endl;

    }

};

 

int main() {

    Dog dog;

    dog.eat();    // Inherited method

    dog.bark();   // Derived class method

   

    Cat cat;

    cat.eat();    // Inherited method

    cat.meow();   // Derived class method

   

    return 0;

}

 

In this example:

- `Animal` is the base class with common methods `eat()` and `sleep()`.

- `Dog` and `Cat` are derived classes that inherit from `Animal`. They can use the methods of `Animal` and also define their own specific methods (`bark()` for `Dog` and `meow()` for `Cat`).

 

 Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common base class. It is the ability to call the same method on different objects and have each object respond in its own way. Polymorphism is achieved through:

1. Compile-time Polymorphism (Method Overloading and Operator Overloading)

2. Runtime Polymorphism (Method Overriding using Virtual Functions)

 

 

 

Compile-time Polymorphism:

- Method Overloading: Multiple methods with the same name but different parameter lists.

- Operator Overloading: Overloading standard operators to work with user-defined classes.

 

Example of Method Overloading:

 

#include <iostream>

using namespace std;

 

class Printer {

public:

    void print(int i) {

        cout << "Printing integer: " << i << endl;

    }

 

    void print(double d) {

        cout << "Printing double: " << d << endl;

    }

 

    void print(const string& s) {

        cout << "Printing string: " << s << endl;

    }

};

 

int main() {

    Printer printer;

    printer.print(5);        // Calls print(int)

    printer.print(3.14);     // Calls print(double)

    printer.print("Hello");  // Calls print(string)

 

    return 0;

}

 

Runtime Polymorphism:

- Method Overriding: A derived class provides a specific implementation of a method that is already defined in its base class. It uses the `virtual` keyword in the base class and `override` keyword in the derived class.

 

Example of Method Overriding:

 

#include <iostream>

using namespace std;

 

// Base class

class Base {

public:

    virtual void show() {

        cout << "Base class show function." << endl;

    }

};

 

// Derived class

class Derived : public Base {

public:

    void show() override {

        cout << "Derived class show function." << endl;

    }

};

 

void display(Base* b) {

    b->show(); // Calls the appropriate show() based on the object type

}

 

int main() {

    Base base;

    Derived derived;

 

    display(&base);      // Output: Base class show function.

    display(&derived);   // Output: Derived class show function.

   

    return 0;

}

 

In this example:

- `Base` class has a virtual method `show()`.

- `Derived` class overrides the `show()` method.

- The `display()` function takes a pointer to the `Base` class and calls `show()`. At runtime, the correct `show()` function is called based on the actual object type (either `Base` or `Derived`).

 

 

 

 

 Summary

 

- Inheritance allows a class to inherit properties and methods from another class, promoting code reuse and establishing a class hierarchy.

- Polymorphism enables objects of different classes to be treated as objects of a common base class, providing flexibility in method calls and enhancing code extensibility.

 

These principles are crucial for designing robust and flexible object-oriented systems, allowing for better management of complex codebases

and promoting code reuse and maintainability.

No comments:

Post a Comment