Thread: Overloading(creating) operators (BCPPB 5.0)

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    31

    Overloading(creating) operators (BCPPB 5.0)

    Hello. I would like to ask you to explain or give some example of overloading operators to me. I mean something like ==. Thank you.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    struct foo
    {
        int age;
        string fname;
        string lname;
    };
    
    bool operator==(const foo& lhs,const foo& rhs)
    {
        return lhs.age == rhs.age && lhs.fname == rhs.fname && lhs.lname == rhs.lname;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Apple
    { 
    private:
    	string color;
    
    public:
    	Apple(string c)
    	{
    		color = c;
    	}
    
    	Apple& operator=(const Apple& rhs)  //the return value is so you can write:
                                                   //apple1 = apple2 = apple3;
    	{
    		if(this == &rhs)  //if they are the same objects, 
                                     //no need to do any assignments
    			return *this;  
    
    		else if(rhs.color == "black") //don't do the assignment
    		{
    			cout<<"The Apple is rotten."<<endl;
    			return *this;  
    		}
    		else //do the assignment
    		{
    			this->color = rhs.color;
    			return *this;
    		}
    
    
    
    
    	}
    
    	void show()
    	{
    		cout<<"My apple's color is: "<<color<<endl;
    	}
    };
    
    int main()
    {
    	Apple redApple("red");
    	Apple blackApple("black");
    	Apple greenApple("green");
    	
    	redApple = greenApple;
    	redApple.show();
    	cout<<endl;
    
    	greenApple = blackApple;
    	greenApple.show();
    	cout<<endl;
    
    	blackApple = greenApple = redApple;
    	blackApple.show();
    	greenApple.show();
    	redApple.show();
    
    	
    	return 0;
    }
    Last edited by 7stud; 08-15-2005 at 01:30 PM.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    31
    Thak all of you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Invisible process ( BCPPB 5.0 )
    By MiraX33 in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2005, 12:30 AM
  2. Associating file types ( BCPPB 5.0 )
    By MiraX33 in forum Windows Programming
    Replies: 2
    Last Post: 10-15-2005, 10:38 AM
  3. AnsiString to char (BCPPB 5.0)
    By MiraX33 in forum C++ Programming
    Replies: 2
    Last Post: 09-01-2005, 11:48 AM
  4. Changing color of the text in TListBox (BCPPB 5.0)
    By MiraX33 in forum C++ Programming
    Replies: 0
    Last Post: 08-20-2005, 02:57 AM
  5. Docking windows (BCPPB 5.0)
    By MiraX33 in forum C++ Programming
    Replies: 0
    Last Post: 08-15-2005, 11:16 AM