Write a program that defines a template function named add(). This function take
ID: 3608368 • 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
Sum of values of type String = VirtualUniversity
Explanation / Answer
//Hope this will help you..
// Dear i updated the code, but u don't forget to rateit...
#include<iostream>
using namespacestd;
/*Class String that will used to overload the string class for concatof string */
classString
{
public:
/* Data member that will hold the string*/
stringstr;
/* Constructor*/
String(strings)
{
str=s;
}
/* print the string*/
voidprint(){
cout<<str;
}
/*concat the string */
Stringoperator+(String obj)
{
string s1=str +obj.str;
returnString(s1);
}
};
/*Template that will used to add int,float and String*/
template <classmyType>
myType add(myType a,myType b) {
myTyperes;
res =a+b;
returnres;
}
intmain()
{
inti1,i2;
floatf1,f2;
strings1,s2;
cout<<"Entertwo integer values to be added"<<endl;
/*Get two integer from user */
cout<<"EnterFirst value:";
cin>>i1;
cout<<"EnterSecond value:";
cin>>i2;
cout<<"Entertwo integer values to be added"<<endl;
/*get two float from user */
cout<<"EnterFirst value:";
cin>>f1;
cout<<"EnterSecond value:";
cin>>f2;
cout<<"Entertwo Strings values to be added"<<endl;
/*get two string from user */
cout<<"EnterFirst value:";
cin>>s1;
cout<<"EnterSecond value:";
cin>>s2;
cout<<"Sum ofvalues of type int:"<<add(i1,i2)<<endl;
cout<<"Sum ofvalues of type float:"<<add(f1,f2)<<endl;
Stringstr1(s1); /* Create instance of class String*/
Stringstr2(s2);
String str =add(s1,s2); /* Add the string usingtemplate add */
cout<<"Sum ofvalues of type String:";str.print(); /*Print the result */
cout<<endl;
system("pause");/*Wait for user input */
}
/* Sample output
Enter two integer values to be added
Enter First value:12
Enter Second value:25
Enter two integer values to be added
Enter First value:13.5
Enter Second value:14.2
Enter two Strings values to be added
Enter First value:Virtual
Enter Second value:University
Sum of values of type int:37
Sum of values of type float: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.