Question no.1 Write a program which defines aTemplate function named Add () whic
ID: 3608729 • Letter: Q
Question
Question no.1
Write a program which defines aTemplate function named Add () which adds twovariables and then returns the sum.
In main function, define twovariables of type int, two variables of typedouble and two variables of typefloat, call Add function three times for thesedifferent data types.
Your output should look likethis:
Enter Two integer values tobe added
Enter Firstvalue :
12
Enter Secondvalue :
25
Enter Two doublevalues to be added
Enter Firstvalue :
12354.23
Enter Secondvalue :
2563.25
Enter Two floatvalues to be added
Enter Firstvalue :
13.5
Enter Secondvalue :
14.2
Addition of twovariable of different data type
Sum = 37
Sum =14917.5
Sum =27.7
Question no. 2
Overload the Unary -- (pre-decrement) operator.
Write a program which has a class Date, Thisclass should have four data members, day, month, year,firstday (i.e. first day of the month).
The class should further contain two constructors (i.e. Defaultand parameterize), a Display Function which display the date informat ( e.g 10-01-2007) and - - Operatorfunction, which decrement the day memberof the Date class. Keep in mind that if the day isfirst day of the month then it should also decrement themonth data member and if month is the first monthof the year then it should also decrement the yeardata member of the class.
In main function define an object of the Date class anddecrement it 1 time.
Question no. 3
Write a program which defines three variables of typedouble which store three different values including decimal points,using setprecision manipulators to print all these values withdifferent number of digits after the decimal number.
Question no. 4
Define shallow copy.
Explanation / Answer
//Hope this will helpyou..
#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.