Answered step by step
Verified Expert Solution
Link Copied!

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

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

Current Trends In Database Technology Edbt 2006 Edbt 2006 Workshops Phd Datax Iidb Iiha Icsnw Qlqp Pim Parma And Reactivity On The Web Munich Germany March 2006 Revised Selected Papers Lncs 4254

Authors: Torsten Grust ,Hagen Hopfner ,Arantza Illarramendi ,Stefan Jablonski ,Marco Mesiti ,Sascha Muller ,Paula-Lavinia Patranjan ,Kai-Uwe Sattler ,Myra Spiliopoulou ,Jef Wijsen

2006th Edition

3540467882, 978-3540467885

More Books

Students also viewed these Databases questions

Question

2. How should this be dealt with by the organisation?

Answered: 1 week ago

Question

explain what is meant by the term fair dismissal

Answered: 1 week ago