O'Reilly logo

Object-Oriented Programming with C++ by A.K. Sharma

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8

TEMPLATES: CODE SHARING (GENERICITY)

8.1 INTRODUCTION TO CODE SHARING

There are many programming situations where same set of operations is applied on different data types. For example, the following function exchanges the contents of two variables of type int.

image

Now, consider a situation where we also need to exchange the contents of variable of type: float and char. Accordingly, we will have to define the following two functions:

Function for float type variables:

image

Function for char type variables:

It may be noted that 99% of the code in the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required