Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a programthat defines a template function named add(). This function takes

ID: 3608371 • Letter: W

Question

Write a programthat defines a template function named add(). This function takestwo arguments, add two variables and then return the sum.

In mainfunction, define two variables of type int, two variables of typefloat and two objects of type ‘String’. Now call theadd() function three times for these different data types.

Note: String isa user-defined data type and for this you have to define a classnamed ‘String’. When template function will be calledto add two objects of type String then it must concatenate twostrings.

  

Your outputshould look like this:

SampleOutput:

Enter twointeger values to be added

Enter Firstvalue: 12

Enter Secondvalue: 25

Enter two floatvalues to be added

Enter Firstvalue: 13.5

Enter Secondvalue: 14.2

Enter twoStrings to be added

Enter Firstvalue: Virtual

Enter Secondvalue: University

Addition of twovariables of different data types

Sum of valuesof type int = 37

Sum of valuesof type float = 27.7

Sum of valuesof 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:

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

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote