Question

In: Computer Science

In C++ please. 3. Define templates and explain their purpose. Differentiate standalone function and class templates....

In C++ please.

3. Define templates and explain their purpose. Differentiate

standalone function and class templates. Give an example of a class

template and a function template.

Solutions

Expert Solution

A template is an important feature used in C++, is used for generic programming, and defines generic functions and constructors(classes).

Generally, a template is used to create a function or constructor(class) to work with different data types.
It means that a template function or class can work with any data types with one definition.

There are two types of template:
1) Function template - Concerns with a function
2) class template - concerns with a constructor of a class.


The purpose of the template is to provide a definition that can take any data type, so the main purpose is reusability and flexibility.

In Simple words, a standalone function can't work with the different data types with one definition and does return value, but the class template can work with different data types with one definition but doesn't return anything, as the class template is nothing but a constructor of a class that works with different data types.

Example of a class template and a function template :

class template : (Addition of two numbers )

#include<iostream>
using namespace std;

template <class T1, class T2>

class Demo
{
  
public :
T1 x;
T2 y;
  
Demo(T1 x1, T2 y1)
{
x=x1;
y=y1;
}
  
void addition()
{
cout<<"Addition of two numbers: "<<(x+y)<<endl;
}
};


int main()
{
Demo<int,double> demo1(100,200.500);
demo1.addition();
Demo<int, int> demo2(10,20);
demo2.addition();
return 0;
}

function template: (addition of two numbers)

#include<iostream>
using namespace std;

template<class T1,class T2>
void addition(T1 x, T2 y)
{
cout<<"Addition of "<<x<<" and "<<y<<" is "<<(x+y)<<endl;
}

int main()
{
addition(100,200);
addition(25.85, 36.88);
addition(1000,255.56);
return 0;
}


Related Solutions

CSI 1440 Lab 6 “Class Templates” Templates Templates in C++ is not a difficult idea. This...
CSI 1440 Lab 6 “Class Templates” Templates Templates in C++ is not a difficult idea. This lab is not intended to help you understand the details of how templates work. It is only intented to give students an opportunity to start developing code using templated classes. Everyone in the class has had to come to grips with the usage of variables in general. With templates, you can think of the type of the variable being a variable itself. The programmer...
Explain the purpose or function of: (a) the stockholders' ledger, (b) the minutes book, (c) the...
Explain the purpose or function of: (a) the stockholders' ledger, (b) the minutes book, (c) the stock- transfer agent, and (d) the stock registrar. Explain and provide examples.
Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
C++ Question- Write function templates for 5 sort algorithms: - Quick Sort Apply these algorithms to...
C++ Question- Write function templates for 5 sort algorithms: - Quick Sort Apply these algorithms to 10 different arrays of integers. Each array has 100 integer elements. The arrays are filled with random integer numbers.
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and...
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and returns "true" if all the characters included in the string are ordered in ascending order of their ASCII codes or the input string is a null string, and returns "false" otherwise. For example, if the string "ABXab" is passed to the function, it returns "true" because the ASCII code of 'B' is greater than 'A', 'X' is greater than 'B', 'a' is greater than...
python 3 please Define a function voweliest that takes as input a string and returns as...
python 3 please Define a function voweliest that takes as input a string and returns as output a tuple where the string that has the most vowels in it is the first element and the second is the number of vowels in that string. Note: don't worry about ties or capital letters Hint: consider defining and using a separate function that counts the number of vowels in a given string
Please answer in c++ 6.Define a function to find a given target value in an array,...
Please answer in c++ 6.Define a function to find a given target value in an array, but use pointer notation rather than array notation whenever possible. 7.Write a swap function, that swaps the values of two variables in main, but use pointers instead of reference parameters. 8.Write a function that takes an array of ints and its size as arguments. It should create a new array that is the same size as the argument. It should set the values in...
3.Discuss the purpose of home health services. 4.Differentiate between the purpose of a public health nursing...
3.Discuss the purpose of home health services. 4.Differentiate between the purpose of a public health nursing visit, a home health nursing visit, and a hospice nursing visit.
Please use C++: Data Abstraction, Bags and Stacks: Define a class DoublyLinkedBag that implements the ADT...
Please use C++: Data Abstraction, Bags and Stacks: Define a class DoublyLinkedBag that implements the ADT BagInterface by using a doubly linked chain, as shown in Figure 4-10 of your textbook. You will also need to define the class Node described in Excercise 10 of Chapter 4. Your solution to this problem requires the creation/development of four files: Node.h, Node.cpp, DoublyLinkedBag.h and DoublyLinkedBag.cpp. This repository already contains BagInterface.h that contains the declaration of the BagInterface needed by this problem. Convert...
Differentiate between a primary and a secondary immune response. 2- Define hypersensitivity. 3. Differentiate between numeral...
Differentiate between a primary and a secondary immune response. 2- Define hypersensitivity. 3. Differentiate between numeral and cellular immunity. 4. What accounts for the declining efficiency of the immune system with age? 5. How do insufficient or overactive immune responses create problems? 6. How are the innate and adaptive immune reponses intertwined? 7. Describe the functions of the different parts of the innate immune response? 8. Give the four key characteristics of the adaptive immune response. 9. Describe B and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT