C++ Tutorial

Class and Object

Reference

Inheritance and Derivation

Polymorphism and Virtual Functions

Operator Overloading

Template

Exception

Object Oriented Advanced

Input/Output Stream

File Operations

Class Template in C++

In C++, class templates are a powerful feature that allows you to create generic classes that can work with different data types. A class template is a blueprint for generating classes, depending on the data type provided.

Defining a Class Template

To define a class template, use the template keyword followed by a template parameter list enclosed in angle brackets <>. Then, define the class as usual, but use the template parameter as a placeholder for the data type.

Here's an example of a simple class template for a Box that can store a single item of any data type:

template <typename T>
class Box {
    T item;

public:
    void setItem(T newItem) {
        item = newItem;
    }

    T getItem() {
        return item;
    }
};

In this example, T is a template parameter that serves as a placeholder for the data type. When creating an object of the Box class, you'll provide the data type you want to use.

Creating Objects of a Class Template

To create an object of a class template, you need to provide the data type you want to use within angle brackets <>.

Example:

Box<int> intBox;    // A Box that can store an int
Box<double> dblBox; // A Box that can store a double
Box<std::string> strBox; // A Box that can store a std::string

Using Class Template Objects

You can use objects of class templates just like you would use objects of regular classes.

Example:

#include <iostream>
#include <string>

template <typename T>
class Box {
    T item;

public:
    void setItem(T newItem) {
        item = newItem;
    }

    T getItem() {
        return item;
    }
};

int main() {
    Box<int> intBox;
    intBox.setItem(42);
    std::cout << "Integer box contains: " << intBox.getItem() << std::endl;

    Box<double> dblBox;
    dblBox.setItem(3.14);
    std::cout << "Double box contains: " << dblBox.getItem() << std::endl;

    Box<std::string> strBox;
    strBox.setItem("Hello, World!");
    std::cout << "String box contains: " << strBox.getItem() << std::endl;

    return 0;
}

In this example, we create objects of the Box class template for different data types (int, double, and std::string) and use their member functions to set and get items.

In conclusion, class templates in C++ enable you to create generic classes that can work with different data types. By defining a class template, you can create a single class that can be reused with various data types, making your code more modular and maintainable.

  1. Introduction to class templates in C++:

    • Description: Class templates allow you to create generic classes in C++, enabling you to work with different data types while writing a single class definition.
    • Example Code:
      #include <iostream>
      
      // Class template definition
      template <typename T>
      class MyTemplate {
      public:
          T data;
      
          void display() {
              std::cout << "Data: " << data << std::endl;
          }
      };
      
      int main() {
          // Instantiating class template with int type
          MyTemplate<int> intObj;
          intObj.data = 42;
          intObj.display();
      
          // Instantiating class template with double type
          MyTemplate<double> doubleObj;
          doubleObj.data = 3.14;
          doubleObj.display();
      
          return 0;
      }
      
  2. C++ template specialization for classes:

    • Description: Template specialization allows you to provide custom implementations for specific data types.
    • Example Code:
      #include <iostream>
      
      // Primary template
      template <typename T>
      class MyTemplate {
      public:
          void display() {
              std::cout << "Generic Display" << std::endl;
          }
      };
      
      // Template specialization for int
      template <>
      class MyTemplate<int> {
      public:
          void display() {
              std::cout << "Specialized Display for int" << std::endl;
          }
      };
      
  3. C++ template constraints for class templates:

    • Description: Template constraints ensure that the template is instantiated only with compatible data types.
    • Example Code:
      #include <iostream>
      #include <type_traits>
      
      // Template with constraints
      template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
      class MyTemplate {
      public:
          T data;
      
          void display() {
              std::cout << "Data: " << data << std::endl;
          }
      };
      
  4. C++ template inheritance with class templates:

    • Description: Illustrates how class templates can be used in inheritance hierarchies.
    • Example Code:
      #include <iostream>
      
      // Base class template
      template <typename T>
      class BaseTemplate {
      public:
          T data;
      
          void display() {
              std::cout << "Data: " << data << std::endl;
          }
      };
      
      // Derived class template inheriting from BaseTemplate
      template <typename T>
      class DerivedTemplate : public BaseTemplate<T> {
      public:
          void derivedDisplay() {
              std::cout << "Derived Display" << std::endl;
          }
      };
      
  5. Template member functions in C++ classes:

    • Description: Introduces template member functions within class templates, allowing generic functionality for specific methods.
    • Example Code:
      #include <iostream>
      
      // Class template with template member function
      template <typename T>
      class MyTemplate {
      public:
          T data;
      
          // Template member function
          template <typename U>
          void displayTemplate(U value) {
              std::cout << "Template Display: " << value << std::endl;
          }
      };
      
  6. Using multiple template parameters in C++ classes:

    • Description: Shows how to define class templates with multiple template parameters for increased flexibility.
    • Example Code:
      #include <iostream>
      
      // Class template with multiple parameters
      template <typename T, typename U>
      class Pair {
      public:
          T first;
          U second;
      
          void displayPair() {
              std::cout << "Pair: {" << first << ", " << second << "}" << std::endl;
          }
      };