67 272
Assignments Done
99,1%
Successfully Done
In November 2018

Answer to Question #24381 in C++ for Nourah

Question #24381
Suppose I have a struct called HeightType that stores a person's height using two fields: feet and inches. Write an overloaded operator for the > sign that will return true if the first HeightType value is greater than the second one.
Expert's answer
//======================================================
// overload operator '>'
//======================================================
bool operator>(HeightType & a, HeightType & b){
// if fields 'feet'coincide, then we should compare fields 'inches'
if (a.feet ==b.feet)
{
return(a.inches>b.inches);
}
return (a.feet> b.feet);
}
//======================================================




Below is an example of usage


#include <iostream>
struct HeightType
{
int feet;
int inches;
HeightType(intf, int i)
{
feet=f;
inches=i;
}
};

// overload operator '>'
bool operator>(HeightType & a, HeightType & b)
{
// if fields'feet' coincide, then we should compare fields 'inches'
if (a.feet ==b.feet)
{
return(a.inches>b.inches);
}
return (a.feet> b.feet);
}


int main()
{
// now a>b
HeightType a =HeightType(11,6);
HeightType b =HeightType(11,5);

if (a>b)
{
std::cout<< "a > b
";
}
else
{
std::cout<< "a <= b
";
};
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions