Link to home
Start Free TrialLog in
Avatar of oggiemc
oggiemcFlag for Ireland

asked on

C++ operator overloading

Can someone tell me where the overloaded operator functions are called in the code attached?

Thanks
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

class Fish
{
	string name;
public:
    Fish();
	Fish(string);
	virtual void display();

    bool operator < (const Fish& f) const {
      return (name < f.name ? true : false ); }

    bool operator == (const Fish& f) const {
      return (name == f.name ? true : false ); }
};

Fish::Fish(): name("") {}
Fish::Fish(string nm): name(nm) {}

void Fish::display()
{
	cout << "I am a fish and my name is " << name << endl;
}

void outputFunction(Fish x)
{       
	x.display();
}
 
int main(void)
{
      string x;                                                                               
      vector<Fish> vect;  // declare a vector container of Fish
                                                              
      cout << "Please enter five Fish:" << endl;      
      for(int i=0; i<5; i++)                 
      {
        cin >> x;
      	vect.push_back(Fish(x)); 
      }

      sort(vect.begin(), vect.end());
             
      // loop through vector with for_each loop and execute 
      // outputFunction() for each element       
      
      for_each(vect.begin(), vect.end(), outputFunction);
}

Open in new window

Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Is this an assignment?
Where do you think they are called?
Avatar of oggiemc

ASKER

i am studying for an exam..Please note i am not just looking for quick answers to "homework"..I have done some research on the topic:
I have seen the operator == () function used before..The way i seen it called was something like:

String result = (variableA == variableB);

from my understanding, the compiler knows to call the overloaded operator function when it sees the double equal sign..

I cannot see a similar call above, nor can i see a call to the operator < () function..
Ok... If a < b and you were creating a list of ordered items in ascending order what order would you give to a & b? Can you see anywhere in you code where this might be happening?
Avatar of oggiemc

ASKER

sort(vect.begin(), vect.end());
 is where the objects are being sorted..Where the call to the overloaded operator is, i do not know..
Well, how do you think sort works? Do you think it could determine if a is less than b using the == operator?
Avatar of oggiemc

ASKER

i would imagine sort uses the < operator to determine if one variable is less than another..i can see the < operator in this context in the following lines 17 and 42..

That's right. It uses less than to determine list ordering in the call to sort.
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of oggiemc

ASKER

ok..So basically what your saying is, the call to the overloaded operator is in the call to sort?Therefore i cant see it in the code above..

>>1. You can simplify those operators, thus.
Ok, fair enough..

>>2.Try running the code in a debugger with break points on the operator == and operator < code. Look at the call stack and you can see from where they are called.
I am unfamiliar with using brekpoints in Dev-cpp..I will have to look into this..

>>3. Can you see anywhere that the == operator is called in this code (test this using the debugger, you should see that it's never called)? I'll give you a hint... can you see any where there is a need to test equality.
 no..I believe there is only need for the less than operator function..

I would agree with you on all accounts. Does it all make sensew now? Take some time to play with this. There is no rush to close this question. Post back if you need further clarification. Good luck :)
nice teamwork :)

just to add (don't give points for it) :

if you would use the find function after each input to check for duplicates, the find would use operator==

Sara