Overloading < or > operators

This is a discussion on Overloading < or > operators within the C++ Programming forums, part of the General Programming Boards category; Hi all, I want to use a greater than operator and overload it to compare two objects of the same ...

  1. #1
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    Overloading < or > operators

    Hi all,
    I want to use a greater than operator and overload it to compare two objects of the same class thereby doing a bubble or selection sort to alphabetize...
    I understand the syntax (I think) I have it sans the overlaod my question it the delaration of this operator..
    should it be a friend or a member function and what it is's setup?

    the alphabetize function I use is as follow:
    Code:
    //Seat::& operator < (const Seat *seat_o )
    //the above doesn't fly....
    //alphabetize:client function
    void Airplane::alpha ()
    //in: *s[], Mnum; out:none
    	{	
    	char Ln[20]={""};
    	char Ln1[20]={""};
    	char Fn[20]={""};
    	
    	Seat *tempname;
    	for (int i = 0; i < MAX_ITEMS; i++)
    		{
    		for (int j = i+1; j < MAX_ITEMS; j++)
    			{
    			seats[i]->getLastname(Ln1);
    			seats[j]->getLastname(Ln);
    			
    			if ((Ln1[0] > Ln[0]))
    				{
    				tempname = seats[i];
    				seats[i] = seats[j];
    				seats[j] = tempname;
    				
    				}//if	
    			}//for
    		}//
    	
    	printalpha();
    it seems there is not alot of info about thhis operator overload..there are tons of things about insertion and extraction and mathematical..
    any help MUCH appreciated as always..
    thanks from
    the bouncing Mouse

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    comparison operator < less than...
    Code:
    alternative 1. negligably faster than alt 2 but depends on private members and friendship.
    
    class pointless
    {
    int x;
    
    public:
    friend bool operator < ( const pointless& lhs, const pointless& rhs )
    { return lhs.x < rhs.x ; }
    };
    
    alternative 2. depends on class interface only.
    
    class pointless
    {
    int x;
    
    public:
    int Getx() { return x;}
    };
    bool operator < ( const pointless& lhs, const pointless& rhs)
    { return lhs.Getx() < rhs.Getx(); }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    more ??

    Hi & thanks for the response..
    maybe you can help more...
    I am using the > operator as a member function to compare
    one object to the 'this' object...
    it works and compiles but when I attempt to use it in my Alphabetize function (to alpha by LastName...I am not alphabetized) so I figure there must be a logic error somewhere.
    (I also want to organize sequentionally by seat iDnum, so that
    the seat numbers aren't messed up after the alpha function.)
    here is my code...
    http://micheles-myocardium.com/6.txt

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    I could not find the actual implementation of '<'. My suggestion is to loose char LastName[20]; and use std::string LastName; This makes < much easyer to write.
    Code:
    bool operator < (const Seat &rhs) const {
        if (LastName < rhs.LastName)
            return true;
        else if(LastName == rhs.LastName)
            return FirstName < rhs.FirstName;
        else
            return false;
    }
    This will sort by last name, then first name if two people have the same last names. Changing Airplane::seats to std::vector<Seat> seats; will let you use
    std::sort(seats.begin(), seats.end()); instead of alpha() and handle airplanes with more than twelve passengers, though given our current economy twelve passengers is probably generous.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 04:17 AM
  2. > > > Urgent Help < < <
    By CodeCypher in forum C Programming
    Replies: 2
    Last Post: 01-31-2006, 02:06 PM
  3. C++ Overloading < > == for use with char * or string
    By neolyn in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2004, 03:37 PM
  4. Opinion on Overloading < and > For My Number Class
    By golfinguy4 in forum C++ Programming
    Replies: 5
    Last Post: 07-01-2003, 06:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21