Write a program that defines a template function named add(). This function take
ID: 3608426 • Letter: W
Question
Write a program that defines a template function namedadd(). This function takes two arguments, add twovariables and then return the sum.
In main function, define two variables of type int, twovariables of type float and two objects of type‘String’. Now call the add() function three times forthese different data types.
Note: String is a user-defineddata type and for this you have to define a class named‘String’. When template function will be called to addtwo objects of type String then it must concatenate twostrings.
Your output should look like this:
Sample Output:
Enter two integer values to be added
Enter First value: 12
Enter Second value: 25
Enter two float values to be added
Enter First value: 13.5
Enter Second value: 14.2
Enter two Strings to be added
Enter First value: Virtual
Enter Second value: University
Addition of two variables of different data types
Sum of values of type int = 37
Sum of values of type float = 27.7
Explanation / Answer
//Hope this will helpyou..
// Dear i updated the code, but udon't forget to rate it...
#include<iostream>
using namespace std;
/* Class String that will used tooverload the string class for concat of string*/
class String
{
public:
string str;
/* Constructor */
String(string s)
{
str=s;
}
/* print the string */
void print(){
cout<<str;
}
/* concat the string*/
String operator+(String obj)
{
string s1=str + obj.str;
return String(s1);
}
};
/* Template that will used to addint,float and String */
template <class myType>
myType add(myType a, myType b) {
myType res;
res = a+b;
return res;
}
int main()
{
int i1,i2;
float f1,f2;
string s1,s2;
cout<<"Enter two integer values to beadded"<<endl;
/* Get two integer from user*/
cout<<"Enter First value:";
cin>>i1;
cout<<"Enter Second value:";
cin>>i2;
cout<<"Enter two integer values to beadded"<<endl;
/*get two float from user*/
cout<<"Enter First value:";
cin>>f1;
cout<<"Enter Second value:";
cin>>f2;
cout<<"Enter two Strings values to beadded"<<endl;
/* get two string from user*/
cout<<"Enter First value:";
cin>>s1;
cout<<"Enter Second value:";
cin>>s2;
cout<<"Sum of values of typeint:"<<add(i1,i2)<<endl;
cout<<"Sum of values of typefloat:"<<add(f1,f2)<<endl;
String str1(s1); /* Create instance of classString */
String str2(s2);
String str =add(s1,s2); /* Add the string usingtemplate add */
cout<<"Sum of values of typeString:";str.print(); /*Print the result */
cout<<endl;
system("pause"); /*Wait for user input*/
}
/* Sampleoutput
Enter two integer values to beadded
Enter Firstvalue:12
Enter Secondvalue:25
Enter two integer values to beadded
Enter Firstvalue:13.5
Enter Secondvalue:14.2
Enter two Strings values to beadded
Enter Firstvalue:Virtual
Enter Secondvalue:University
Sum of values of typeint:37
Sum of values of typefloat:27.7
Sum of values of typeString:VirtualUniversity
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.