Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Templates: A mechanism that allows you to use a generic placeholder for a data type when defining a class. So instead of having several classes

Templates: A mechanism that allows you to use a generic placeholder for a data type when defining a class. So
instead of having several classes that are all the same except for the data type (e.g.
CResizableShortArray, CResizableIntegerArray, CResizableLongArray) you can have just one class
CResizableArray. When an instance is created, the actual data type to be used is specified. The
compiler substitutes that data type for the placeholder in the class.
How to convert a class to a template in nine easy steps:
1) Add template immediately before the class declaration.
2) Change the data type for any class properties to GenericDataType.
3) Change the data type for any class method parameters or return values to GenericDataType.
4) Wrap the class declaration in #ifndef/#define/#endif commands. (See example below)
5) Include the source file at the botton of the header file. For example:
#ifndef CRESIZABLE_ARRAY_H
#define CRESIZABLE_ARRAY_H
template
class CResizableArray
{
...
};
// Must include source file in header file or define all methods in header file after class declaration.
#include CResizableArray.cpp
#endif
6) Wrap the class method definitions in #ifndef/#define/#endif commands.
#ifndef CRESIZABLE_ARRAY_CPP
#define CRESIZABLE_ARRAY_CPP

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Decisions Based On Data Analytics For Business Excellence

Authors: Bastian Weber

1st Edition

9358681683, 978-9358681680

More Books

Students also viewed these Databases questions