class Tower {
private:
int height;
public:
Tower(int i) { height = i;}
booloperator<(Tower rhs) { return (height < rhs.height); } }; // end of class
I need to use this class to create user functions, they cant be member or friends to do the following:
1 2 3 4 5 6
Tower a(5), b(10), c(5);
if ( a == c) cout << "Tower a has same height as c";
if(b>a) cout<<"Towerbisbiggerthana";
if ( b >= a) cout << "Tower b is at least as big as a";
if ( c <= b) cout << "Tower c is no bigger than a";
if ( a != b) cout << "The height of Tower a is not same as that of b";
I've been racking my brain for 2 days on this, and am at my wits end. Does anyone have any suggestions?
so you need to express equality with less. if a==b, then is a<b? no. is b<a? no. is there any pair of values a,b so that a is not <b and b is not <a but a is not == b? no. so a==b is true when both a<b and b<a are false. is it clear?
All of those operations (==, >, >=, <=, !=) can be replaced with the operator < (with a little more code of course).
For example, to find (a == c), you can check if a is less than c, and then check if a is greater than c (in other words, if c is less than a because you only have the less than operator to use). If neither are less than the other, then they must be equal.