0% found this document useful (0 votes)
3 views23 pages

Object Oriented Programming (CS1143) : Department of Computer Science Capital University of Science and Technology (CUST)

This document covers the concepts of function and class templates in C++ as part of Object Oriented Programming (CS1143) at Capital University of Science and Technology. It explains how template programming allows for the creation of generic functions and classes that can operate on different data types without the need for multiple overloaded functions. The document provides examples and outlines the benefits of using templates for code efficiency and generalization.

Uploaded by

abdulmoiz1542005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views23 pages

Object Oriented Programming (CS1143) : Department of Computer Science Capital University of Science and Technology (CUST)

This document covers the concepts of function and class templates in C++ as part of Object Oriented Programming (CS1143) at Capital University of Science and Technology. It explains how template programming allows for the creation of generic functions and classes that can operate on different data types without the need for multiple overloaded functions. The document provides examples and outlines the benefits of using templates for code efficiency and generalization.

Uploaded by

abdulmoiz1542005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Object Oriented Programming

(CS1143)
Week 12

Department of Computer Science


Capital University of Science and Technology (CUST)
Outline

 Function Templates
 Class Templates

2
Template Programming

 In this chapter we concentrate on generalization, which means to


write a general program that can be used in several special cases.
 C++ calls this process template programming.
 We first discuss how to write general functions (called function
templates) and then we discuss general classes (called class
templates).

3
Outline

 Function Templates
 Class Templates

4
Function Template

 When programming in any language, we sometimes need to apply


the same code to different data types.
 For example, we may have a function that finds the smaller of two
integer data types, another function that finds the smaller of two
floating-point data types, and so on.
 We can write a program to solve a problem with a generic data type.
 We can then apply the program to the specific data type we need.
 This is known as generic programming or template programming.
 Using function templates, actions can be defined when we write a
program, and the data types can be defined when we compile the
program.

5
Using a family of functions

 If we do not use template and generic programming, we must define


a family of functions.
 For example, assume that we need to find the smaller between two
characters, two integers, and two floating-point numbers.
 Since the types of the data are different, without templates we would
need to write three functions

6
7
8
Using Function Template

 To create a template function, we can use a placeholder for each


generic type.
 The template header contains the keyword template followed by a
list of symbolic types inside two angle brackets.
 The template function header follows the rules for function headers
except that the types are symbolic as declared in the template
header.

9
Example

10
Description

 We have only one generic type, but the type is used three times:
twice as parameters and once as the return type.
 In this program the type of the parameters and the returned value
are the same.
 We have saved code by writing only one template function instead of
three overloaded functions.

11
Another Example

12
13
template <typename T>
T smaller (T first, T second)
{
...
}

Before Compilation

14
Overloading Function Templates

 We can also overload a function template to have several functions


with the same name but different signatures.
 Normally, the template type is the same, but the number of
parameters is different.
 In the following program, we overload the smaller template function
to accept two or three parameters (we call it smallest because it uses
more than two arguments).
 We have defined the second function in terms of the first one. This is
why the second function is shorter.

15
16
17
Outline

 Function Templates
 Class Templates

18
Class Template

 We know that a class is a combination of data members and member


functions.
 We may also have a class with data types and another class with the
same functionality but with different data types.
 In these cases, we can use a class template.

19
Example

20
21
22
This is all for Week 12

23

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy