Thread: Operator overloading example...

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    Operator overloading example...

    Would someone be able to post me a simple example of operator overloading. The book I'm using really doesn't explain it well. So any help you guys can give would be greatly appreciated.

    Thanks,

    ...Dan

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You could read this article.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Thanks

    ...Dan

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    Let's say you have a simple class defined like this:
    Code:
    class Box
    {
    public:
    	Box(string str, int n)  //constructor
    	{
    		type = str;
    		items = n;
    	}
    	Box()  //default constructor
    	{
    		type = "";
    		items = 0;
    	}
    
    	void show() //displays member variables
    	{
    		cout<<"Box type: "<<type<<endl
    	            <<"Box items: "<<items<<endl;
    	}
    
    private:
    	string type;
    	int items;
    };
    Now suppose you want to add two boxes together. If you write:
    Code:
    int main()
    {
    	Box A("candy", 3);
    	Box B("popcorn", 5);
    
    	Box C;
    
    	C = A + B; 
    
    	return 0;
    }
    You will get an error because the compiler doesn't know what you mean when you say you want to add two Box objects together. What should the result be? When you create objects of your classes, you have to tell the compiler what result you want when you add two of your objects together. So you need to "overload" the operator+() in order to tell the compiler what the result should be. Each operator overload you write requires a unique format, so you need some resource for looking up the correct format. Pay particular attention to the parameter types and the return type. Here is the format for overloading operator+():
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Box
    {
    public:
    	Box(string str, int n)
    	{
    		type = str;
    		items = n;
    	}
    	Box()
    	{
    		type = "";
    		items = 0;
    	}
    
    	void show()
    	{
    		cout<<"Box type: "<<type<<endl
                        <<item count: "<<items<<endl;
    	}
    
    	Box operator+(const Box& rhs) const
    	{
    		Box combined;
    		combined.type = "combined items";
    		combined.items = this->items + rhs.items;
    
    		return combined;
    	}
    		
    
    private:
    	string type;
    	int items;
    };
    
    
    int main ()
    {
    
    	Box A("candy", 3);
    	Box B("popcorn", 5);
    
    	Box C = A + B;
    	C.show();  //Box type: combined items
                       //item count: 8
       
    	return 0;
    }
    You should note that the line:
    Code:
    C = A + B;
    calls the default assignment operator: operator=(). The compiler provides a default assignment operator for your classes, and the default assignment operator makes a member by member copy of what's on the right hand side(rhs) into the members of the object on the left hand side(lhs). If that is not the behavior you want, then you have to overload the operator=() for your class. You will usually overload operator=() if your objects have members that are pointers to dynamically created memory. In that case, you can overload operator=() to prevent two objects from pointing to the same dynamically created memory, which would cause a change in one object to affect the other object.
    Last edited by 7stud; 02-22-2006 at 01:23 PM.

  5. #5
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Thanks for the great explanation. This helps a lot.

    Thanks,

    ...Dan

Popular pages Recent additions subscribe to a feed