Describe how to construct function templates and class templates.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Describe how to construct function templates and class templates.

Expert Solution
Step 1

Templates:

 

Templates are used for writing generic programs. These work like macros.

The compiler checks the type before template expansion. The source code have class or function although the compiled code may have several copies of same class or function.

Data type is passed as a parameter so that same code is not needed to be written for different data types.

Function templates:

These templates are used for working with generic types. They help in creating a function template whose functionality may be used by more than one data type.

A template parameter is used for passing a type as argument.

It works like regular function arguments which are used for passing values to a function, template parameters allow to pass also types to a function.

 

The syntax is as follows:

The difference between following prototypes is the use the keywords typename or class.

template <class > declaration;

template <typename > declaration;

 

Class template:

Class templates are like function templates which are useful for defining classes which do not depend on the data type. The type is specified when the object of the class is declared.

The syntax is as follows:

template<class Ttype> 

class class_name 

  //data members

  //member functions

 

Creating object of that class:

class_name<type> obj;

Step 2

Example of function template:

 

#include <iostream> 

using namespace std; 

 

//function template

//function that adds two values

template<class Temp> Temp add(Temp &a,Temp &b) 

    Temp sum = a+b; 

    return sum; 

 

int main() 

  //int variables

  int i =3; 

  int j =3; 

 

  //float variables

  float m = 4.3; 

  float n = 5.2; 

 

  //sum of int variables

  cout<<"Sum of i and j is :"<<add(i,j)<<endl;

 

  //sum of float variables

  cout<<"Sum of m and n is :"<<add(m,n); 

  return 0; 

}

 

Output:

Computer Science homework question answer, step 2, image 1

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Introduction to Template
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education