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

Both would work, but my view here is that both classes should provide the overloaded operator.

Obviously these classes are contrived, in a real world example the method of only providing the overload in the one class could very quickly get messy if the contained classes were complex.

With operator overloading just think of it as a function. You need to decide:

1 - do you need this function now?
2 - what is an appropriate name for what it does
3 - how to write the function
4 - is this the correct place to have the function
5 - do you need a test class to ensure that you have coded it correctly
6 - is a function that is unsed but makes sense with the object


The reason for considering these points is to keep your code maintainable. I assume that the answer to 1 is no or you wouldn't be asking.

With an overload 2 is answered for you and you have shown that you can do 3 and 5 if necessary

This leaves 4 & 6 and without knowing what you are trying to do

Does your class B want to be used directly with a < IF the answer is that you are not sure, you do not need to include it instantly.

You only have to implement a function when you need it. The overloaded operators are used for certain sort algorithms so it won't hurt to have one if it makes sense for the object.

what might need answering is your behaviour for class A what you want to happen with < or what is placed in your client code.

If you can be more precise about what class A and B are doing would be good

Thanks for your input. This is not a real example, it is one of a series of course assigments. The Class's and the structure (a composite Class) was designed in the previous course assignments and now the mission is to implement operator overloading. I suppose that I'm not allowed to change the Class structure(ClassA is a composite Class).
The background for my question is that I'm not sure if it's wise to push down operator overloading in the Class hierarchy. I have designed both ClassA and ClassB but that would perhaps not be the case in the real world. I have started to implement alternative 2 where also ClassB will contain operator overloading. It works so far.

If it is just an assignment to overload operators then you will probably get bonus marks for doing both.

However, this is not a question that can be fully answered in the abstract.

suppose we had a class like

class x_coord
{
public:
x_coord(int nx = 0);
bool operator>(const test_op &t) const;
int x;
}

the > is easy to define

but if you then have
a coord

class coord
{
public:
coord(int nx = 0, int ny = 0);
int y;
x_coord x;
}

how do you decide if one coord is greater than the other ?

this is the problem you have with classA and it is undefined
and your example suggests that

bool classA::operator>(const classA &c) const
{
  bool ret(false);
  if(stringA > c.getStringA())
  {
    ret = true;
  }
  else if(stringA == c.getStringA())
 {
    ret = (objectB > c.getObjectB());
  }
return ret;
}

but even this could be incorrect

as an abstract the > is undefined == is defined but cannot be used by sort
so for classB I would define all the operators if you have time and for classA only those that you know make sense +=, -=, ==

and if you are intending to sort classA you might want a clear understanding for classA behaviour

It is a flaw in your assignment that the class has not been given a clearly defined purpose and it is why your question is not easy to answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.