Answered step by step
Verified Expert Solution
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 eg
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:
Add template immediately before the class declaration.
Change the data type for any class properties to GenericDataType.
Change the data type for any class method parameters or return values to GenericDataType.
Wrap the class declaration in #ifndef#define#endif commands. See example below
Include the source file at the botton of the header file. For example:
#ifndef CRESIZABLEARRAYH
#define CRESIZABLEARRAYH
template
class CResizableArray
;
Must include source file in header file or define all methods in header file after class declaration.
#include CResizableArraycpp
#endif
Wrap the class method definitions in #ifndef#define#endif commands.
#ifndef CRESIZABLEARRAYCPP
#define CRESIZABLEARRAYCPP
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started