Thread: Operator overloading

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    Operator overloading

    Hello,

    I am new to C++ and am trying to learn about operator overloading. I can not seem to wrap my mind around how it works. Could someone please point me in the right direction.



    PHP Code:

    #include <iostream>
    #include <string>
    using namespace std;

    //--- CLASSES ---

    class contact
    { private: char fname[6];
               
    char number[6];
       public:
               
    void get();
               
    void show();
               
    bool operator == (emp);
    };

    //--- FUNCTIONS ---

    void contact::get()
    cout<< "name: ";
      
    cin.getline(fname,6);
      
    cout<< "number: ";
      
    cin>> number;
    }
    bool emp::operator == (emp find)
    {
       *
    ptr[0]==find;    // I would like to be able to search by
    }                            // fname or by number.   I jsut can't figure 
                                  // out the syntax and what am I passing

    //--- PROGRAM BODY ---

    int main()
    emp *ptr[2];
       
    emp find;

       
    ptr[0]=new emp;
       
    ptr[0]->get();
       
    cout<< "Find:  ";
       
    cin>> find;

       if(*
    ptr[0]==find)    
         
    cout << "Pass = 1 \n";
        else 
    cout<< "Fail = 0\n";

          
    system("PAUSE");
          return 
    0;

    Last edited by Mr_roboto; 09-27-2005 at 01:41 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    What is emp? I am a bit confused as to what your intentions are. Well, since all you seemed to want to know is how to overload ==, here's a simple example class.

    Code:
    class Foo
    {
    public:
    	// Constructors
    	Foo(int a) : _a(a) { }
    	~Foo() { }
    
    	// Accessors
    	int getFoo() { return _a; }
    	
    	// Operators
    	bool operator == (Foo& bar)
    	{
    		if(bar.getFoo() == _a)
    			return true;
    		return false;
    	}
    private:
    	int _a;
    };

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You should probably put operator== outside the class, so it isn't inline. But then, it's a "simple example".

    That can't be the whole code. Post the rest of it. [edit]Mr_roboto, I mean.[/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    Code Re Post

    There is not that much to it. I am trying to learn things on a small scale first.

    I think that I implemented the the example in the fallowing code. Seeing how it will not compile I sure that there is something simple that I am leaving out.




    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    //*** Class Declarations ***
    class contact
    { private:
         char fname[10];      // Contacts first name
         char lname[10];      // Contacts last name
         int  phone;          // Contacts phone number
         int find;
      public:
         void getinfo();      // Get contact info
         void showinfo();     // Display contact info
         int lookfor();      // Search for a specific phone number
         bool operator == (contact& find); 
    };
    
    //*** Function Declarations ***
    
    void contact::getinfo()
    { cout << "First Name: ";
      cin>> fname;
    
      cout<< "Last Name: ";
      cin>> lname;
    
      cout<< "Phone Number: ";
      cin>> phone;
    }
    
    void contact::showinfo()
    { cout<< "Name: " << fname << " " << lname << endl;
      cout<< "Number: " << phone << endl;
    }
    int contact::lookfor() 
    { cout<< "Enter a Number: ";
      cin>> find;
      return find;
    }
    bool contact::operator == (contact& temp)
    { if(temp.find() == phone)
       { cout << "Number Found\n"; 
         return true;
       }
      cout<< "No Such Number\n";
      return false;
    }
    
    //*** Program Body ***
    
    int main()
    {
     contact *data[2];        // Array to hold contact info
     int  search[10];         // Number to search for
    
     for(int i=0;i<2;i++)     // Gather contact info
      { data[i] = new contact;
        data[i] -> getinfo();
      }
    
     cout<< endl << endl;
    
     for(int i=0;i<2;i++)     // Show all contacts
      { if(data[i]->showinfo();
      }
    
     cout<< endl << endl;
    
     for(int i=0;i<2;i++)     // Search for a contact by phone#
       { data[i]->find();
       }
      system("pause");
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You are thinking of operator== incorrectly. It should not be used to find an item based on part of the values. It should be used to determin whether two items are the same. Here is a simple example program that can be used to see if the two sets of data input by the user are the same. You can use it to test your operator== to see if it works (you of course have to supply the class definition above this code):
    Code:
    int main()
    {
      contact data1;
      data1.getinfo();
    
      contact data2;
      data2.getinfo();
    
      if (data1 == data2)
        cout << "They match!" << endl;
      else
        cout << "They don't match." << endl;
    
      system("pause");
      return 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    Sometimes learning is an up hill battle

    I realize that I am trying to use the '==' operator in an uncommon way. I would like to over load the '==' operator so that I can use it in a search an array of contacts by phone number and then display their name if they are in the data array.

    Is my code that far off?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It's not very good design to change the natural semantics of an operator. If you want to continue to do that, you will have to try one of two things.

    The first is to overload operator== to work with builtin types. For example, you can make an operator==(int) that checks the phone number passed in with the current objects phone number.

    The second, which is probably a little clearer (but not much), is to create a dummy variable of the type contact and just set it's phone number variable (or whichever variable you want to search for). Then test that dummy variable against the other one to see if they match. Here is an example test program for that:
    Code:
    int main()
    {
      contact data1;
      data1.getinfo();
     
      int find;
      cout<< "Enter a Number: ";
      cin>> find;
     
      contact dummy;
      dummy.setphone(find); // you must create this function
     
      if (data1 == dummy)
    	cout << "They match!" << endl;
      else
    	cout << "They don't match." << endl;
     
      system("pause");
      return 0;
    }

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    Thank you all

    I got it working

    Thank you all especially David and Tonto.

    ~Rich

Popular pages Recent additions subscribe to a feed