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

# Class and function template with syntax

Object Oriented Programming · Chapter 9: Templates

- Asked in: 2073 Shrawan, 2+2 marks; 2072 Chaitra, 5 marks; 2069 Chaitra, 5 marks; 2068 Baisakh, 2+2 marks (asked 4 times)
- Video: [C++ Class Template & Function Template Explained with Examples](https://www.youtube.com/watch?v=XA2y18mYJRI) — Programming Horizons
- 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-function-template-syntax
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-9

## Question

What do you mean by Class Template and Function Template? Write down the syntax of Class Template and Function Template.

## Short answer

A function template defines a generic function: template<class T> ret func(T args){...}. The compiler deduces T from calls. A class template defines a generic class: template<class T> class Class { ... }; objects like Class<int> obj; specify T. Outside member definitions require template<class T> and Class<T>:: scope.

## Answer

#### Function template
A **function template** is a single generic function definition that works for many data types. The compiler generates the actual function for each type used (template instantiation).

```cpp
template <class T>            // or: template <typename T>
return_type function_name(T arg1, T arg2) {
    // body using T
}
```

Example:

```cpp
template <class T>
T maxOf(T a, T b) { return (a > b) ? a : b; }
// maxOf(3, 7) -> int version;  maxOf(2.5, 1.5) -> double version
```

#### Class template
A **class template** is a generic class whose data members and member functions can use a type parameter. The class is instantiated for a specific type when an object is declared.

```cpp
template <class T>
class ClassName {
    T data;                       // member of generic type
public:
    ClassName(T d);
    T getData();
};

// member function defined outside the class
template <class T>
ClassName<T>::ClassName(T d) { data = d; }

template <class T>
T ClassName<T>::getData() { return data; }
```

Object creation: `ClassName<int> a(5);  ClassName<float> b(2.5f);`

| Function template | Class template |
|---|---|
| Generic function | Generic class |
| Type deduced from arguments | Type given explicitly in `<>` |
| Called like a normal function | Used to declare objects |

## Check yourself

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

1. What is a function template?
   A. A function that can only return templates
   B. A function stored in a header only
   C. A generic function definition from which the compiler generates versions for each type used
   D. A function that calls itself
   Answer: C. One generic definition is instantiated by the compiler for each data type used. (Key point: Function template meaning)

2. Which declaration is a correct function template header?
   A. class template <T> maxOf(T a, T b)
   B. T template maxOf(T a, T b)
   C. template (T) maxOf(T a, T b)
   D. template <class T> T maxOf(T a, T b)
   Answer: D. A template starts with template <class T> (or typename T) before the function. (Key point: Function template syntax)

3. How is a member function of a class template defined outside the class?
   A. Repeat the template header and use ClassName<T>::
   B. No template header is needed
   C. Use ClassName:: only
   D. Use the keyword friend
   Answer: A. The definition must repeat template <class T> and qualify the name as ClassName<T>::function. (Key point: Defining class template members outside)

4. How is the type provided for a class template versus a function template?
   A. Class: deduced; function: explicit in <>
   B. Class: explicit in <>; function: deduced from arguments
   C. Both always explicit
   D. Neither needs a type
   Answer: B. A class template object is declared as ClassName<int> a; a function template deduces T from the call arguments. (Key point: Creating objects)

## Related in Object Oriented Programming

- [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
- [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
- [Importance of function template](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-template-importance) — Object Oriented Programming, chapter 9, asked 2×
- [Need of template; function template overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Awhy-template-function-overloading) — Object Oriented Programming, chapter 9

## Related in other subjects

- [Top-down view for F = xz + yz'](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Apositive-photoresist-topdown-xz-yzbar) — Embedded System, chapter 8
- [Create classes from design class diagrams](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aclasses-from-design-class-diagram) — Object Oriented Analysis and Design, chapter 4, asked 2×
- [Class C pool for 32, 65, 10, 21, 9](https://www.bctengineer.com/question.md?key=semester_7%2Fcomputer_networks_and_security%3Asubnetting-class-c-five-depts) — Computer Networks and Security, chapter 4, asked 2×
- [Class C pool for 10, 12, 8, 14](https://www.bctengineer.com/question.md?key=semester_7%2Fcomputer_networks_and_security%3Asubnetting-class-c-four-depts) — Computer Networks and Security, chapter 4, asked 2×
