---
title: "Default arguments in class template"
subject: "Object Oriented Programming"
chapter: 9
key: "semester_3/object_oriented_programming:class-template-default-args"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/class-template-default-args
site: BCT Engineering
---

# Default arguments in class template

Object Oriented Programming · Chapter 9: Templates

- Asked in: 2070 Chaitra, 3 marks
- Video: [C++ Templates: Class Templates with Default Parameters | C++ Tutorials for Beginners #66](https://www.youtube.com/watch?v=IdY8t0n8VBs) — CodeWithHarry
- 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-default-args
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-9

## Question

How can default arguments be used in class template?

## Short answer

Class templates can specify default types for parameters (e.g., template<class T1=int, class T2=float>). If omitted, defaults are used; empty angle brackets <> must be written. Once a parameter has a default, subsequent parameters must also have defaults.

## Answer

A class template can give a **default type** to its type parameters, in the same way functions give default argument values. If the user does not supply a type while creating an object, the default is used.

Syntax:

```cpp
template <class T1 = int, class T2 = float>
class Test { ... };
```

Rules:
- Defaults are written in the template header (only once, in the class declaration, not in out-of-class member definitions).
- Once a parameter has a default, the parameters after it must also have defaults.
- Empty angle brackets `<>` must still be written when all defaults are used.
- A non-type parameter can also have a default value, e.g. `int size = 10`.

#### Example

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

template <class T = int, int size = 5>
class Array {
    T a[size];
public:
    void set(int i, T v) { a[i] = v; }
    T get(int i) { return a[i]; }
    int length() { return size; }
};

int main() {
    Array<> x;                 // int, size 5 (both defaults)
    Array<float> y;            // float, size 5
    Array<char, 20> z;         // char, size 20
    x.set(0, 7);
    cout << x.get(0) << " " << x.length() << endl;   // 7 5
    cout << y.length() << " " << z.length() << endl; // 5 20
    return 0;
}
```

## Check yourself

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

1. What does a default type in a class template do?
   A. It forces all objects to use it
   B. It is used for functions only
   C. It is used when the user supplies no type
   D. It is ignored by the compiler
   Answer: C. If no type is supplied while creating the object, the default is used. (Key point: Default types)

2. How do you create an object using all default parameters of Array?
   A. Array x;
   B. Array(x);
   C. Array<void> x;
   D. Array<> x;
   Answer: D. Empty angle brackets <> must still be written when all defaults are used. (Key point: Empty brackets)

3. Which rule applies to defaults in template parameters?
   A. Once a parameter has a default, the following ones must also have defaults
   B. Defaults must be written in every member definition
   C. Only type parameters can have defaults
   D. Defaults must be given for the last parameter only
   Answer: A. After a defaulted parameter, all later parameters must have defaults too. (Key point: Order rule)

## Related in Object Oriented Programming

- [Default arguments and function overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-args-function-overloading) — Object Oriented Programming, chapter 3, asked 5×
- [Default arguments and their syntax](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-arg-define-syntax) — Object Oriented Programming, chapter 3, asked 2×
- [Display N characters using default arguments](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3An-chars-default-args-program) — Object Oriented Programming, chapter 3
- [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
- [Class template with suitable example](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-example) — Object Oriented Programming, chapter 9
