Create a function template named \"findLargerValue\", which when given two value
ID: 3841930 • Letter: C
Question
Create a function template named "findLargerValue", which when given two values, will return the larger of the two values.
In the main() function, use the "findLargerValue" function to find and display the larger value/object for the following:
Two integer values
Two double values
Two Trip objectsCreate a class called Trip which has:
an integer data element length
an overloaded "operator >" function to compare two Trip objects. For example, if (myTrip > yourTrip) cout<<"myTrip is longer"<<endl;, where myTrip and yourTrip are objects of the Trip class,
Explanation / Answer
#include <iostream>
using namespace std;
class Trip
{
char c;
public:
void setVal(char cc)
{
c=cc;
}
char printVal()
{
return c;
}
bool operator > (const Trip &yourTrip)
{
return c > yourTrip.c;
}
};
// template function
template <class T>
T findLargerValue(T n1, T n2)
{
return (n1 > n2) ? n1 : n2;
}
int main()
{
int i1, i2;
double f1, f2;
Trip myTrip, yourTrip;
cout << "Enter two integers: ";
cin >> i1 >> i2;
cout << findLargerValue(i1, i2) <<" is Large Value." << endl;
cout << " Enter two double numbers: ";
cin >> f1 >> f2;
cout << findLargerValue(f1, f2) <<" is Large Value." << endl;
char t1,t2;
cout << " Enter two characters for myTrip and yourTrip: ";
cin >> t1 >> t2;
myTrip.setVal(t1);
yourTrip.setVal(t2);
Trip c3=findLargerValue(myTrip, yourTrip);
if(c3.printVal()==t1)
{
cout<< " myTrip is longer value.";
}
else
{
cout<< " yourTrip is longer value.";
}
return 0;
}
================================================================
akshay@akshay-Inspiron-3537:~/Chegg$ g++ temp.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Enter two integers:
2
3
3 is Large Value.
Enter two double numbers:
5.6
32
32 is Large Value.
Enter two characters for myTrip and yourTrip:
a
z
yourTrip is longer value
=================================================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.