C++ Programming : After work guide to master C++ on your own. Build your coding skills and learn how to solve common problems. Transform your passion in ... job career as a computer programmer. by HOOD CODING & Kölling Michail

C++ Programming : After work guide to master C++ on your own. Build your coding skills and learn how to solve common problems. Transform your passion in ... job career as a computer programmer. by HOOD CODING & Kölling Michail

Author:HOOD, CODING & Kölling, Michail [HOOD, CODING]
Language: eng
Format: epub, pdf
Published: 2020-09-24T00:00:00+00:00


Chapter 6: Macros and template

Introduction to Templates

Templates are an important feature in C++ programming language, as it allows functions and classes to use generic types. They play an essential role in how functions and classes work by allowing them to define different data types without rewriting each other.

Using templates, you can write a generic code that can be used with any data type. All you need is to pass the data types as parameters. Templates promote code reusability and improve the flexibility of the program. You can create a simple class or a function and pass data types as parameters and implement the code to be used by any data type. Whenever you want to use the function, you make a call to the function and specify the return type.

For example, if you use sort() function to sort data in a warehouse, you can pass different data types as parameters to the function. You don't have to write and maintain multiple file codes to sort data. Based on the data type passed to the sorting algorithm, your data in the warehouse is sorted irrespective of that particular data type. Therefore, templates are used in situations where you need a code to be reusable with more than one data type.

Templates are of great importance when dealing with multiple inheritance and operator overloading. It is widely applied where code reusability is of prime importance.

Types of Templates

Templates are implemented in two ways:

● Using function templates

● Using class templates

A template can be defined as a macro. That is when you define an object of a specific type, the template definition of that type of class substitutes it with the appropriate data type. Templates are often referred to as parameterized class or functions since a specified data type will replace the defined parameter during execution time.

Class Templates

Class templates offer the specifications for creating a class based on the parameters. A class template accepts members with parameter type. That is, you can instantiate a class template by passing a given set of data type to its parameter list. Class templates are mostly used in implementing containers.

Syntax

Template <class identifier>

Class class-name

{

…

//class member specifications

…

};

Example:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.