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

In main function, define two variables of type int, twovariables of type float a

ID: 3608508 • Letter: I

Question

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: BABAR

Enter Second value: LAHORE

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 = BABAR LAHORE

Explanation / Answer

#include // For I/O operations // Declaration of template function template T add(T , T) ; /* Class String */ class String { private: char str[30]; // private data member to hold the string public: // default constructor String() { strcpy(str, "") ; // initialize the data member 'str' with emptystring } // Function to display the string void show() { cout str; // input string from user } // Overloading of + operator to concatenate two strings String & operator +(String s) { strcat(str, s.str); // concatenate two strings return *this ; } }; // definition of template function template T add(T x, T y){ // takes two arguments of type T and return T return x+y ; // return the addition of two variables } /* main function*/ int main() { String str1,str2, str3 ; int x , y ; float a , b ; // prompt the user to enter values of type int cout x ; cout > y ; // prompt the user to enter values of type float cout a ; cout > b ; // prompt the user to enter two strings cout
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