DEV Community

Hafid Saadi
Hafid Saadi

Posted on

Mastering Polymorphism in C++: Understanding and Implementing Virtual Functions and Template Classes

Polymorphism is a fundamental concept in object-oriented programming that allows a single function or object to have multiple forms. In this blog post, we will explore polymorphism in the C++ programming language and provide examples of how it can be implemented.

Image Polymorphism

One way to achieve polymorphism in C++ is through the use of virtual functions. A virtual function is a member function that is declared as virtual in the base class and can be overridden by derived classes. When a virtual function is called through a base class pointer or reference, the program will determine at runtime which version of the function to execute based on the type of the object being pointed to or referenced.

Here's an example of a simple base class, "Shape," that has a virtual function "draw()":

class Shape {
public:
    virtual void draw() = 0;
};
Enter fullscreen mode Exit fullscreen mode

We can then create two derived classes, "Circle" and "Rectangle," that override the "draw()" function:

class Circle : public Shape {
public:
    void draw() {
        // code to draw a circle
    }
};

class Rectangle : public Shape {
public:
    void draw() {
        // code to draw a rectangle
    }
};
Enter fullscreen mode Exit fullscreen mode

We can then create a pointer of type "Shape" and point it to an object of type "Circle" or "Rectangle." When we call the "draw()" function through the pointer, the program will determine at runtime which version of the function to execute:

int main() {
    Shape* shape1 = new Circle();
    shape1->draw();  // calls Circle::draw()

    Shape* shape2 = new Rectangle();
    shape2->draw();  // calls Rectangle::draw()
}
Enter fullscreen mode Exit fullscreen mode

Another way to implement polymorphism in C++ is through the use of template classes. A template class is a generic class that can work with different data types. This allows a single class or function to have multiple forms based on the type of data it is working with.

Here's an example of a simple template class, "Stack," that can hold elements of any data type:

template <typename T>
class Stack {
    T* data;
    int size;
public:
    Stack(int size) {
        this->size = size;
        data = new T[size];
    }
    void push(T element) {
        // code to add element to stack
    }
    T pop() {
        // code to remove and return top element from stack
    }
};
Enter fullscreen mode Exit fullscreen mode

We can then create objects of the "Stack" class for different data types, such as "Stack" and "Stack":

int main() {
    Stack<int> intStack(5);
    intStack.push(1);
    intStack.push(2);
    intStack.pop();  // returns 2

    Stack<string> stringStack(5);
    stringStack.push("hello");
    stringStack.push("world");
    stringStack.pop();  // returns "world"
}
Enter fullscreen mode Exit fullscreen mode

As we have seen in these examples, polymorphism in C++ can be achieved through the use of virtual functions and template classes. These features of the language allow a single function or object to have multiple forms, making the code more flexible and reusable.

Learn more about polymorphism:
wikipedia
stackify.com

Top comments (0)