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

oggiemcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
Is this an assignment?
Where do you think they are called?
0
oggiemcAuthor Commented:
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..
0
evilrixSenior Software Engineer (Avast)Commented:
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?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

oggiemcAuthor Commented:
sort(vect.begin(), vect.end());
 is where the objects are being sorted..Where the call to the overloaded operator is, i do not know..
0
evilrixSenior Software Engineer (Avast)Commented:
Well, how do you think sort works? Do you think it could determine if a is less than b using the == operator?
0
oggiemcAuthor Commented:
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..

0
evilrixSenior Software Engineer (Avast)Commented:
That's right. It uses less than to determine list ordering in the call to sort.
0
evilrixSenior Software Engineer (Avast)Commented:
A couple of other things...

1. You can simplify those operators, thus.

    bool operator < (const Fish& f) const {
      return name < f.name; }

    bool operator == (const Fish& f) const {
      return name == f.name; }

Open in new window


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.

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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oggiemcAuthor Commented:
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..

0
evilrixSenior Software Engineer (Avast)Commented:
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 :)
0
sarabandeCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.