Overloading "less than" operator
Hi,
I this problem i try to overload the "less than" operator in three ways :
bool operator < (const Box& aBox) const;
bool operator < (const double aValue) const;
bool operator < (const double aValue, const Box& aBox);
but i receive an error :
"error : 'bool Box::operator < (const double aValue, const Box& aBox)' must take exactly one argument"
What i supossed to do ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Box
{
public:
Box(double aLength = 1.0, double aWidth = 1.0, double aHeight = 1.0);
double volume() const;
double getLength() const;
double getWidth() const;
double getHeight() const;
bool operator < (const Box& aBox) const;
bool operator < (const double aValue) const;
bool operator < (const double aValue, const Box& aBox)
private:
double length;
double width;
double height;
};
Box::Box(double aLength, double aWidth, double aHeight)
{
length = aLength;
width = aWidth;
height = aHeight;
}
double Box::volume() const
{
return (length*width*height);
}
double Box::getLength() const
{
return length;
}
double Box::getWidth() const
{
return width;
}
double Box::getHeight() const
{
return height;
}
bool Box::operator < (const Box& aBox) const
{
return this->volume() < aBox.volume();
}
bool Box::operator < (const double aValue) const
{
return this->volume() < aValue;
}
bool Box::operator < (const double aValue, const Box& aBox)
{
return aValue < aBox.volume();
}
inline int random(int count)
{
return 1+static_cast<int>(count*static_cast<double>(rand()) / (RAND_MAX+1.0));
}
void show(const Box& aBox)
{
cout <<endl << aBox.getLength() << " by " << aBox.getWidth() << " by " << aBox.getLength();
}
int main()
{
const int dimLimit = 100;
srand((unsigned)time(0));
const int boxCount = 20;
Box boxes[boxCount];
for(int i=0; i<boxCount; i++)
boxes[i] = Box(random(dimLimit), random(dimLimit), random(dimLimit));
Box *pLargest = &boxes[0];
for(int i=1; i<boxCount; i++)
if(*pLargest < boxes[i])
pLargest = &boxes[i];
cout << "\n The largest box in the array has dimensions : ";
show(*pLargest);
int volMin = 100000.0;
int volMax = 500000.0;
cout <<endl << " Box with volumes between " << volMin << " and " << volMax << " are : ";
for(int i=0; i<boxCount; i++)
if(volMin < boxes[i] && boxes[i] < volMax)
show(boxes[i]);
return 0;
}
|
Topic archived. No new replies allowed.