Template Inheritance and Polymorphism

Template Basics

Templates in C++ allow developers to write generic code that can work with multiple data types without duplicating implementation. A template is a blueprint that can be instantiated with different data types, enabling code reuse and flexibility. The syntax for templates involves using angle brackets <> and specifying the type parameter explicitly or implicitly.

Template Inheritance

Template inheritance extends the concept of class inheritance to templates, allowing a base template to define a structure that can be inherited by derived templates. This enables the creation of a hierarchy of generic classes.

Base Template

A base template defines the common structure for multiple data types. For example:

template <typename T>  
class Vector {  
public:  
    T x;  
    T y;  
    void print() const { std::cout << x << " " << y << std::endl; }  
};  

The Vector class is a template that can be instantiated with any type, such as int, double, or std::string.

Derived Template

A derived template inherits from a base template, reusing its implementation while adapting to specific data types. For instance:

template <typename T>  
class Vector2D : public Vector<T> {  
public:  
    void print() const {  
        Vector<T>::print();  
        std::cout << " (2D)" << std::endl;  
    }  
};  

Here, Vector2D inherits from Vector<T> and adds a 2D-specific print method.

Template Inheritance Benefits

Template inheritance promotes code reuse and reduces redundancy. It allows developers to create a base template that can be adapted for various data types, ensuring consistency across different implementations.

Template Polymorphism

While templates cannot directly support polymorphism in the traditional sense (as C++ does not allow templates to be virtual), they can be used in conjunction with other features to achieve polymorphic behavior.

Polymorphic Templates

Polymorphic templates enable the use of a single template implementation across multiple data types, leveraging the concept of virtual functions. For example:

template <typename T>  
class Shape {  
public:  
    virtual void draw() const = 0;  
    virtual ~Shape() = default;  
};  

template <typename T>  
class Circle : public Shape<T> {  
public:  
    void draw() const override {  
        std::cout << "Drawing a circle of type " << typeid(T).name() << std::endl;  
    }  
};  

Here, Shape is a template class with a virtual draw() method. Circle is a derived template class that overrides draw() to implement specific behavior for circles.

Template Polymorphism in Practice

Templates can be used to create a family of classes that share common behaviors but differ in implementation. For instance, a Shape template can be extended to create Circle, Triangle, and Square, each with their own draw() implementations. This approach ensures flexibility and maintainability.

Conclusion

Template inheritance and polymorphism are powerful tools in C++ for creating generic, reusable, and flexible code. By leveraging templates, developers can write code that works with multiple data types without duplication, while still benefiting from the principles of polymorphism through virtual function overrides. This combination enables efficient, scalable, and maintainable software design.