I need help with the following course assignment. Operator overloading shall be implemented in an existing composite Class. The overloaded operator shall be used to compare objects (strings) in a bubble sort sequence. It is not allowed to use Friend or Inheritance.
My problem is if operator overloading only should be used in the "base" class or if it is a god design practise to include it also in the included class. Object design teory says that only interfaces should be used to access data in other objects. Or are there strong arguments to also include operator overloading in the included class?
I have omitted constructors, destructors, setfunctions etc in the examples.
Do you think that both ways will work?
class ClassA
{
private:
string stringA;
ClassB objectB;
public:
string getstringA() const { return stringA; }
ClassB getobjectB() const { return objectB; }
};
class ClassB
{
private:
string stringB;
public:
string getstringB() const { return stringB;}
};
If operator overloading is implemented only in ClassA then code will look like this:
bool operator<(const ClassA &ob) const;
bool ClassA::operator<(const ClassA &ob) const
{
return (stringA) < (ob.stringA);
}
The client code will be like this:
ClassA objectA[SIZE];
if (objectA[i] < objectA[i+x]) || (objectA[i].getobjectB().getstringB() < obejectA[i+x].getobjectB().getstringB())
.....//sort objects
or should the overloaded operator also be implemented in the included ClassB:
bool operator<(const ClassB &ob) const;
bool ClassB::operator<(const ClassB &ob) const
{
return (stringB) < (ob.stringB);
}
The client code will then be like this:
ClassA objectA[SIZE];
if (objectA[i] < objectA[i+x]) || (objectA[i].getobjectB() < objectA[i+x].getobjectB())
.....//sort objects