Switch to full style
For C/C++ coders discussions and solutions
Post a reply

Operator overloading easy code

Sun May 25, 2008 5:44 pm

This an easy code for operating overloading.
cpp code
//So now you ask, what is this operator overloading stuff, how will it help
// me make the supreme burrito, and what the heck does "binary operator overloading"
//mean? Lets take this one step at a time. First, a quick example of operator overloading.

#include <iostream.h>

class Burrito
{
private:
int amtbeef, amtbean;
public:

Burrito(int beef, int bean) {
amtbeef = beef;
amtbean = bean;
}
Burrito operator + (Burrito newburrito)
{
int newbeef = amtbeef + newburrito.amtbeef;
int newbean = amtbean + newburrito.amtbean;
return Burrito(newbeef, newbean);
}
};

void main(void)
{
Burrito b1(5,10), b2(10,5), b3;
b3 = b1 + b2;

// cout<<b3<<endl;
}




Re: Operator overloading easy code

Sun May 25, 2008 5:56 pm

Thanks :gOOd:

Re: Operator overloading easy code

Sun May 25, 2008 6:28 pm

operator overloading help programmer so much in data structure

like stack in this example

Stack outer_stack;
for (int i = 0; i < 1000000; i++) {
Stack inner_stack;
inner_stack.push(some_data);
inner_stack = outer_stack;
}

can you figure out where will be the problem without using operator overloading? :club:

Re: Operator overloading easy code

Fri May 10, 2013 2:35 pm

updated.

Post a reply
  Related Posts  to : Operator overloading easy code
 What is Operator Overloading? !!!     -  
 operator overloading     -  
 unary operator overloading     -  
 overloading << and >>     -  
 Function Overloading     -  
 how to build a rating bar in easy and quick way in website     -  
 What is the % operator     -  
 operator int()     -  
 Using the ? Operator     -  
 trinary operator     -  

Topic Tags

C++ OOP