Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 30th, 2006, 3:14 PM   #21
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
In C++ you can assign custom operators to classes. For instance, lets say I have a class Pet, and I consider two Pets as being equal if they have the same name and the same species. I can go about this one of two ways. I could have a function on the class like so:
C++ Syntax (Toggle Plain Text)
  1. bool Pet::equals(const Pet& pet) {
  2. return this->name == pet.name && this->species == pet.species;
  3. }
Then later on, I can compare two Pet instances like so:
C++ Syntax (Toggle Plain Text)
  1. if (fluffy.equals(unknownCat)) { ... }
However, a better solution would be if we put a custom "==" operator on the Pet class:
C++ Syntax (Toggle Plain Text)
  1. bool Pet::operator==(const Pet& pet) {
  2. return this->name == pet.name && this->species == pet.species;
  3. }
Then we could do this:
C++ Syntax (Toggle Plain Text)
  1. if (fluffy == unknownCat) { ... }
As well as making things look nicer, operator overloading also gives one a standard ways of accessing objects. For instance, in order to sort a set of objects, we need to have some information about how the objects should be ordered. The < and > operators give this capability; in strings, "A" < "B", and in ints, 1 < 2. In fact, we don't need both operators; only one is needed, and this happens to be the less than operator.

So all you need do is override the less than operator, and sort your container class with std::sort. For instance, if you have a vector<Customer> object called customers:
C++ Syntax (Toggle Plain Text)
  1. std::sort(customers.begin(), customers.end());
The less than operator on the Customer class gives std::sort the information to achieve the right ordering.
Arevos is offline   Reply With Quote
Old Nov 30th, 2006, 6:35 PM   #22
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
I'm pretty familiar with operator overloading, my question is where in the sort function syntax is there a greater than or lesser than operator. Could you explain the use of the sort function with these operators inside the function call. Or could you give me a link to a site that explains the use of the sort function with respect to these operators.

Quote:
The less than operator on the Customer class gives std::sort the information to achieve the right ordering.

C++ Code:
std::sort(customers.begin(), customers.end());
The less than operator on the Customer class gives std::sort the information to achieve the right ordering.
Where is the operator?
codylee270 is offline   Reply With Quote
Old Dec 1st, 2006, 2:59 AM   #23
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
The operator is called within the sort function itself.
Arevos is offline   Reply With Quote
Old Dec 1st, 2006, 12:00 PM   #24
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
So you just overload the < operator as member function for the Customer class? And due to the implementation of the sort function, where ever it is located, will recognize to use the overloaded operator since it is comparing 2 customer objects. Correct?
codylee270 is offline   Reply With Quote
Old Dec 1st, 2006, 2:59 PM   #25
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
You got it.
Arevos is offline   Reply With Quote
Old Dec 1st, 2006, 3:41 PM   #26
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
Using your implementation a few posts above, why did you pass a reference to a customer object. I see that you are using this, so how does the sort function know the invoking object? Does the object on the left side invoke the overloaded function, thus requires accepting the other one as an argument? Does that make sense? Also, to take into account sorting by first name, if the last names are equal, would it make sense to use some conditionals to choose wha treturn to make so if the last names are equal, you would return one as less than the other.

e.g. -
if( (customer.lastName == this->lastName) && customer.firstName < this->firstName)
     return (customer.lastName < this->lastName);
else if( (customer.lastName == this->lastName) && customer.firstName) > this->firstName)
     return (this->lastName < customer.lastName);
else
     return ( customer.lastName < this->lastName);
}

Would something like this work in this situation?
codylee270 is offline   Reply With Quote
Old Dec 1st, 2006, 4:02 PM   #27
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by codylee270 View Post
I see that you are using this, so how does the sort function know the invoking object? Does the object on the left side invoke the overloaded function, thus requires accepting the other one as an argument?
If I'm understanding you right, yes, that's what happens. The object on the left hand side is the object that calls the operator and the object on the right is passed in as an argument.


Quote:
Originally Posted by codylee270 View Post
Also, to take into account sorting by first name, if the last names are equal, would it make sense to use some conditionals to choose wha treturn to make so if the last names are equal, you would return one as less than the other.

e.g. -
if( (customer.lastName == this->lastName) && customer.firstName < this->firstName)
     return (customer.lastName < this->lastName);
else if( (customer.lastName == this->lastName) && customer.firstName) > this->firstName)
     return (this->lastName < customer.lastName);
else
     return ( customer.lastName < this->lastName);
}
Erm. The above code doesn't make logical sense. Your code is logically equivalent to this:
C++ Syntax (Toggle Plain Text)
  1. if (customer.lastName == this->lastName && customer.firstName != this->firstName)
  2. return false;
  3. else
  4. return ( customer.lastName < this->lastName);
  5. }
Which is clearly incorrect. That said, my previous function was also incorrect, so I'm not really one to talk :p

Here's the correct boolean logic:
C++ Syntax (Toggle Plain Text)
  1. return this->lastName < customer.lastName ||
  2. (this->lastName == customer.lastName &&
  3. this->firstName < customer.firstName);
Arevos is offline   Reply With Quote
Old Dec 1st, 2006, 7:29 PM   #28
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
Haha, well at least you understood me! I'm probably going to start coding this tonight or tomorrow, so be on the lookout for actual source. I'm thinking of using a large MegaStore class, as you stated before to handle all of the queues inside the store, and manage statistics for each line. Sound good? Looks like that will be the easiest way to make function calls and what not and not have to worry about what object is invoking what and where/if I need to pass this object so it can be used to process this, etc...
codylee270 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Game programming forum msk420 Community Announcements and Feedback 18 Jun 6th, 2006 8:47 PM
Game Programming ReggaetonKing Coder's Corner Lounge 9 Apr 25th, 2006 4:59 PM
Python programming game? commodore Python 7 Feb 7th, 2006 6:26 PM
game programming gammaman C++ 17 Oct 23rd, 2005 2:42 AM
Java programmers, game developers, artists, be ware! RPG game team is recruiting! atcomputers.us Paid Job Offers 7 Sep 25th, 2005 7:25 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:58 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC