Template in C++, Template Types, C++ Tutorial

Assignment Help >>  Template

Template 

Introduction

Template supports the generic programming, which allows to develop the reusable software components such s functions, classes etc., which will support different data type within a single framework.

Consider the following prototypes.

void display(int a);         // to display an int value

void display(char a);   // to display a char value

void display(double a);      // to display a double value

in  all cases, we have to the define these functions separately to serve different purposes. However, it is possible to possible to define a single function with generic data type to serve different purpose at different time using template in C++.

Consider the following function code:

void display(T a)

{

     cout<<"a ="<<a<<endl;

}

Assume that when you call the function as display(21), the argument T will behave as an int, when you call the function as display('A'), the argument T will behave as a char, and when you call the function as display('A'), the argument T will behave as a double. Remember that the type T has to be declared before with the following statement as:

template<class T>

Lets start with the detail of its working procedure. 

Template Type

Template in C++  is classified as:      

          Function Template

          Class Template

 

Function Template

When a single function is written for a family of similar functions, they are called as function templates.

The main advantage of using functio0n template is avoiding unnecessary repetition of the source code. The object code becomes more compact and efficient than the conventional way of declaring and defining the functions 

Declaration of function template

The general syntax for declaring a function template in C++ is given as

template <class T>

Return_type  function _name ( formal arguments of type T)

 {

                  //statement1

                  //Return value of type T;

}

Program 8.1

Write a program to display three different values and their size in bytes using function template.

#include<iostream.h>

#include<constream.h>

template<class T>

void display(T x)

{

     cout<<"x  = "<<x<<endl;

     cout<<"Size of x = "<<sizeof(x)<<endl;

}

main()

{

     clrscr();

     display(21);

     display(2.51);

     display('A');

     return(0);

}

Output

x = 21

Size of x = 2

x = 2.51

Size of x = 8

x = A

Size of x = 1

 

Explanation

In the above program, the formal argument T will be replaced with the required data type depending on the type of actual argument given during function call, and exhibit the above output.

8.2.2. Declaration of function template with multiple arguments

The general syntax for declaring a function template with more arguments is given as

template <class T1,class T2,...>

Return_type  function _name ( formal arguments of type T1,T2)

 {

                  //statements

 }

Program 8.2

Write a program to display two different values with different arguments using a single template function.

#include<iostream.h>

#include<constream.h>

template<class T1,class T2>

void display(T1 x,T2 y)

{

     cout<<"1st value = "<<x<<endl;

     cout<<"2nd value = "<<y<<endl;

}

main()

{

     clrscr();

     display(21,"Ashok");

     display(83.5,'E');

     return(0);

}

Output

1st value =21

2nd value =Ashok

1st value =83.5

2nd value =E

 

Explanation

In the above program, the formal arguments T1 and T2 will be replaced with the required data type depending on the type of actual argument given during function call, and exhibit the above output.

8.3. Overloading of function template

The compiler adopts the following rules for selecting suitable function when the program has overloaded function template.

The compiler follows the following rule for selecting suitable function when the program has overloaded function template.

 

  • The compiler look for exact match on functions if found then call it
  • Look for the function template from which function that can be called with an exact match can be generated if found then call it
  • If no match is found in all the alternatives then that call is treated as an error

Program 8.3

Write a program to implement overloading of template function.

#include<iostream.h>

#include<constream.h>

template<class T>

void display(T x)

{

     cout<<"Template value = "<<x<<endl;

}

void display(int x)

{

     cout<<"Integer value  = "<<x<<endl;

}

main()

{

     clrscr();

     display(21);

     display('E');

     display(11.25);

     return(0);

}

Output

Integer value  =21

Template value =E

Template value =11.25

Explanation

In the above program, the compiler found an exact match on its first call as display(21), which matches the prototype display(int x) and no match found in the next calls, so the template function executed automatically.

8.4. Definition of class template

In addition to the function template, C++ also supports the concept of class template. By definition, class template is class definition that describes family of related classes.

C++ provides the user the ability to create class that contains one or more types that  are generic or parameterized .The manner of declaring the class template is the same as that of  function template.

 

The general syntax of the class template :

 

template <class T>

class class_name

{

         private :

                     // Member specification of type T

          // wherever applicable

         public :

          // Member specification of type T

          // wherever applicable

};

 

Once the class template has been defined ,it is required to instantiate  class object using  specific primitives or user defined types to replace the parameterized types as:

         class_name <data type>object_name;

Program 8.4

Write a program to enter and display two data members of a class using class template.

#include<iostream.h>

#include<constream.h>

template<class T>

class demo

{

     private:

          T a;

          T b;

     public:

          void getdata()

          {

              cout<<"Enter two values of generic type";

              cin>>a>>b;

          }

          void display()

          {

              cout<<"1st data member = "<<a<<endl;

              cout<<"2nd data member = "<<b<<endl;

          }

};

main()

{

     clrscr();

     demo<int>d1;

     d1.getdata();

     d1.display();

     return(0);

}

Output

Enter two values of generic type

11

22

1st data member = 11

2nd data member = 22

 

Explanation

In the above program, the class two data members of type T. The object is created with the statement demo<int>d1. That means the object d1 can be allocated to hold two integer data members. Similarly the user can change the type and allocate the object to hold required data type.

8.5. Definition of member function outside the class of a class template

Member function of class template also contains the keyword template whenever it is declared outside of the scope of  class definition as:

 

template <class T>

return_typ class_name<T>::function_name(Formal argument list)

{

// Member function definition

}

Program 8.5

Write a program to enter and display two data members of a class using class template. Define the member functions outside the class.

#include<iostream.h>

#include<constream.h>

template<class T>

class demo

{

     private:

          T a;

          T b;

     public:

          void getdata();

          void display();

 

};

template<class T>

void demo<T>::getdata()

{

     cout<<"Enter two values of same type";

     cin>>a>>b;

}

template<class T>

void demo<T>::display()

{

     cout<<"1st data member = "<<a<<endl;

     cout<<"2nd data member = "<<b<<endl;

}

main()

{

     demo <int>d1;

     d1.getdata();

     d1.display();

     return(0);

}

Output

Enter two values of same type

11

22

1st data member = 11

2nd data member = 22

 

Explanation

In the above program, the class two data members of type T. The object is created with the statement demo<int>d1. That means the object d1 can be allocated to hold two integer data members. Similarly the user can change the type and allocate the object to hold required data type. Here, the member functions are defined outside the class.

Summary

  • Template is one of the most useful characteristic of C++ .It is newly added in C++.
  • Template provides generic programming by defining generic classes .In templates, generic data types are used as arguments and they can handle a variety of data types.
  • Declaration and definition of every template class starts with the keyword template followed by parameter list
  • The class template may contain one or more parameter of generic data type.. The arguments are separated by comma with template declaration
  • Function template can be defined with one or more parameters
  • A template function also supports overloading mechanism .It can be overloaded by normal function or template function
  • It is also possible to define member function definition outside of the class
  • While defining them outside, the function template should define the function and the template classes are parameterized by the type argument

How we help you? - C++ Assignment Help 24x7

We offer Template assignment help, Template assignment writing help, assessments writing service, Template tutors support, step by step solutions to Template problems, Template answers, C++ Tutorial assignment experts help online. Our C++ Tutorial assignment help service is most popular and browsed all over the world for each grade level.

There are key services in Template which are listed below:-

  • Template help
  • Assignment Help
  • Homework Help
  • Template Assessments Writing Service
  • Solutions to problems
  • C++ Tutorial papers writing and editing
  • Paper formatting and referencing
  • Template research papers writing help
  • Thesis and dissertation help
  • Experts support 24x7
  • Online tutoring

Why choose us - The first thing come in your mind that why choose us why not others what is special and different about us in comparison to other site. As we told you our team of expert, they are best in their field and we are always live to help you in your assignment which makes it special.

Key features of services are listed below:

  • Confidentiality of student private information
  • 100% unique and original solutions
  • Step by step explanations of problems
  • Minimum 4 minutes turnaround time - fast and reliable service
  • Secure payment options
  • On time delivery
  • Unlimited clarification till you are done
  • Guaranteed satisfaction
  • Affordable price to cover maximum number of students in service
  • Easy and powerful interface to track your order


Most Matched Writing Services:-
Captcha

Get Academic Excellence with Best Skilled Tutor! Order Assignment Now! Submit Assignment