---
title: "Class template with suitable example"
subject: "Object Oriented Programming"
chapter: 9
key: "semester_3/object_oriented_programming:class-template-example"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/class-template-example
site: BCT Engineering
---

# Class template with suitable example

Object Oriented Programming · Chapter 9: Templates

- Asked in: 2072 Kartik, 5 marks
- Video: [Class Templates | C++ Tutorial](https://www.youtube.com/watch?v=mQqzP9EWu58) — Portfolio Courses
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/class-template-example
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-9

## Question

Explain class template with suitable example.

## Short answer

A class template defines a generic class with type parameter. For example, a Stack template<class T> stores an array of T and push/pop return T. Objects are Stack<int> and Stack<char>. This one class works for any type, avoiding code duplication. The compiler instantiates a separate class for each T used.

## Answer

A **class template** is a blueprint for a generic class; data members, function arguments and return types can use a type parameter. The compiler creates a real class for each type used, so one class works for `int`, `float`, `char` and so on.

Syntax:

```cpp
template <class T>
class ClassName {
    // members using T
};
ClassName<int> obj;      // instantiation
```

#### Example: generic Stack

```cpp
#include <iostream>
using namespace std;

template <class T>
class Stack {
    T arr[10];
    int top;
public:
    Stack() { top = -1; }
    void push(T x) {
        if (top == 9) cout << "Stack full\n";
        else arr[++top] = x;
    }
    T pop() {
        if (top == -1) { cout << "Stack empty\n"; return T(); }
        return arr[top--];
    }
};

int main() {
    Stack<int> s1;
    s1.push(10); s1.push(20);
    cout << s1.pop() << endl;        // 20

    Stack<char> s2;
    s2.push('A'); s2.push('B');
    cout << s2.pop() << endl;        // B
    return 0;
}
```

The same `Stack` code is used once for integers and once for characters, avoiding two separate classes.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What does the compiler do with a class template?
   A. Creates a real class for each type used
   B. Creates one class for int only
   C. Ignores the type parameter
   D. Converts it to a function
   Answer: A. A class template is a blueprint instantiated into a real class for each type used. (Key point: Class template meaning)

2. Which statement creates a Stack of characters?
   A. Stack s2<char>;
   B. Stack<char> s2;
   C. char Stack s2;
   D. Stack(char) s2;
   Answer: B. The type is given in angle brackets: Stack<char> s2;. (Key point: Instantiation)

3. After s1.push(10); s1.push(20); what does s1.pop() return?
   A. 10
   B. Stack empty
   C. 20
   D. 0
   Answer: C. A stack is last-in first-out, so the last pushed value 20 is popped first. (Key point: Stack behaviour)

## Related in Object Oriented Programming

- [Class and function template with syntax](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-function-template-syntax) — Object Oriented Programming, chapter 9, asked 4×
- [Class template with multiple template types](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-multiple-types) — Object Oriented Programming, chapter 9
- [Default arguments in class template](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-default-args) — Object Oriented Programming, chapter 9
- [Array class template with max, min, sort](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-array-max-min-sort) — Object Oriented Programming, chapter 9
