WriteasimplefunctiontemplateforpredicatefunctionisEqualTothatcomparesitstwo argu
ID: 3635539 • Letter: W
Question
WriteasimplefunctiontemplateforpredicatefunctionisEqualTothatcomparesitstwoargumentsofthesametypewiththeequalityoperator(==)andreturnstrueiftheyareequaland
falseiftheyarenotequal.UsethisfunctiontemplateinaprogramthatcallsisEqualToonlywith
avarietyofbuilt-intypes.NowwriteaseparateversionoftheprogramthatcallsisEqualTowitha
user-definedclasstype,butdoesnotoverloadtheequalityoperator.Whathappenswhenyouat-
tempttorun thisprogram?Nowoverload theequalityoperator(with theoperatorfunction)
operator==.Nowwhathappenswhenyouattempttorunthisprogram?
Explanation / Answer
#include<iostream>
template<class T>
bool isEqual (T num1,T num2)
{
if(num1==num2)
return true;
else
return false;
class Operator
{
int i,j;
public:
Operator()
bool isEqualTo(int num1,int num2)
{
//Here the funtion ask for return type if we ignore what to return
return ;
}
};
int main()
int i=4,j=3;
if(isEqual(i,j))
cout<< i<<" and "<<j<<"Equal";
else
cout<< i<<" and "<<j<<" Not Equal";
float f1=3.4,f2=3.4;
if(isEqual(f1,f2))
cout<< f1<<" and "<<f2<<"Equal";
else
Operator op;
if(op.isEqualTo(i,j))
cout<< i<<" and "<<j<<"Equal";
else
cout<< i<<" and "<<j<<" Not Equal";
system("pause");
return 0;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.