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
    Template Specialization
     
    If we want to define a different implementation for a template when a specific type is passed as template parameter, we can declare a specialization of that template. For example, let's suppose that we have a very simple class called Container that can store one element of any type and that it has just one member function called increase that increases its value and also returns the increased value. But we find that when it stores an element of type char it would be more convenient to have a completely different implementation of the Container class with a function member uppercase that changes the case of the stored character to the upper case and returns the uppercase character, so we decide to declare a class template specialized for that type. 
     
    The general class template looks like:
     
     
    template 
    class Container
    {
     T data;
     
    };
     
     
    and the special class template (for char type data) is declared separately as:
     
     
    template<>
    class Container
    {
     char data;
     
    };
     
     
     
     
     
    Exercise 1:
     
    Now do the following,
  • Complete the declaration and implementation of these templates with member function increase in the first template and uppercase in the second with the required functionality as stated above. (Note that you are not allowed to use the toupper() function) also you are not allowed to create inline functions.
  • Add the following main to your program and watch it run.
  • }
     
    int main () 
    {
     Container myint (7);
     Container mychar (j);
     cout << myint.increase() << endl;
     cout << mychar.uppercase() << endl;
     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!