Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!