Answered step by step
Verified Expert Solution
Question
1 Approved Answer
VisualStudio C++ Make sure it runs and add comments to answer the questions Lab Manual - Templates Objective Implement template functions Implement template classes Implement
VisualStudio C++ Make sure it runs and add comments to answer the questions
Lab Manual - Templates
Objective
- Implement template functions
- Implement template classes
- Implement templates with multiple types
Class Templates
We also have the possibility to write class templates, so that a class can have members that use template parameters as types. In this exercise we shall implement one such class template called a Pair that is used to store two variables of the same type.
In order to define a class template we use the following syntax:
template
class_declaration;
Exercise 1:
The specifications for the Pair class are given below:
- The class Pair has a private data member values which is an array of size two and its type is T(identifier/class/template).
- A constructor that takes two parameters.
- A public member function called GetMax that returns the greater of the two stored variables. (This function has to be defined inline i.e. inside the class body).
- A public member function called GetMin that returns the smaller of the two stored variables. (This function has to be defined outside the class body). This is done by using the following syntax.
template
identifier Pair< identifier >::GetMax(){. . .}
- Now replace (which means you have to comment the previous code) the main method with the following, the program should run like a river.
int main ()
{
Pair myobject (1.012, 1.01234);
cout << myobject.getmax();
return 0;
}
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