2
Let's make tea: Operator Overloading.
...
int main() { Pot pot;
Burner burner;
Water water(1);
Tealeaves tealeaves(1) Sugar sugar(1);
Milk milk(0.5);
burner.start(pot);
pot.add(water);
pot.add(tealeaves);
burner.boil(2, false);
pot.add(sugar);
pot.add(milk);
burner.boil(2, true);
burner.stop();
std::cout << “Tea is ready.\n”;
return 0;
} ...
int main() { Pot pot;
Burner burner;
Water water(1);
Tealeaves tealeaves(1) Sugar sugar(1);
Milk milk(0.5);
burner.start(pot);
pot += water + tealeaves;
burner.boil(2, false);
pot += sugar + milk;
burner.boil(2, true);
burner.stop();
std::cout << “Tea is ready.\n”;
return 0;
}
3
Do we need to overload operators?
●
Language is for communication
–
English between humans
–
C++ between human and machine
●
A good language enables effective communication.
–
Imagine speaking in C++
–
Imagine creating facebook in assembly
●
A language should naturally be able to express human thoughts.
–
coffee = milk + sugar + coffeepower;
–
value = base ^ exponent;
4
Example
class OpOverload { public:
OpOverload(int n): first(n) { }
int operator +(int second) { return first + second; } private:
int first;
};
int main() {
OpOverload o1(4);
OpOverload *o2 = new OpOverload(10);
std::cout << o1 + 10 << ", " << *o2 + 4 << std::endl;
delete o2;
return 0;
}
Type is
OpOverload + int What about
std::cout << 10 + o1?
5
What does Compiler do?
●
Operator is just another method.
–
milk.operator+(sugar);
●
For supporting polymorphism, compiler tracks function argument types.
–
list.add(integer_value);
–
list.add(struct_value);
●
Together, we can overload operator with different argument types.
–
milk + sugar;
–
milk + coffeepowder;
Homework: Give an example of operator overloading in C?
6
Can you spot second overloading?
...
int main() { Pot pot;
Burner burner;
Water water(1);
Tealeaves tealeaves(1) Sugar sugar(1);
Milk milk(0.5);
burner.start(pot);
pot.add(water);
pot.add(tealeaves);
burner.boil(2, false);
pot.add(sugar);
pot.add(milk);
burner.boil(2, true);
burner.stop();
std::cout << “Tea is ready.\n”;
return 0;
} ...
int main() { Pot pot;
Burner burner;
Water water(1);
Tealeaves tealeaves(1) Sugar sugar(1);
Milk milk(0.5);
burner.start(pot);
pot += water + tealeaves;
burner.boil(2, false);
pot += sugar + milk;
burner.boil(2, true);
burner.stop();
std::cout << “Tea is ready.\n”;
return 0;
}
7
Overloading <<
●
std is a namespace, cout is an object in it, whose type is ostream.
●
ostream has operators overloaded for basic types (int, bool, …).
●
What if I want to output my object?
–
std::cout << s; where s is instance-of Student
–
cout doesn't know about Student or OpOverload classes.
–
Java solves this problem with toString() method.
8
Overloading <<
class OpOverload { public:
OpOverload(int n): first(n) { }
int operator +(int second) { return first + second; }
friend std::ostream& operator <<(std::ostream& os, const OpOverload &oo);
private:
int first;
};
std::ostream& operator <<(std::ostream& os, const OpOverload &oo) { os << oo.first;
return os;
}
int main() {
OpOverload o1(4);
std::cout << o1 << std::endl;
return 0;
}
9
Overloading Restrictions
●
You can't redefine for existing primitive types.
–
At least one operand must be user-defined.
●
Syntax rules cannot be changed.
–
% operator has to take two operands.
●
You cannot create new operator symbols.
–
operator **() is disallowed.
●
Following operators cannot be overloaded.
–
sizeof, ., .*, ::, ?:, typeid, cast operators.
10
Returning a Reference
●
Recall that we returned from operator << a reference to a const object.
–
friend std::ostream& operator <<(std::ostream& os, const OpOverload &oo);
●
This can be done in other situations too.
●
Often the reason is efficiency. However, we should be aware of the following.
– When we return reference, there is no copying.
– When we return value, a copy constructor gets
invoked.
11
In these lectures
✔
Introduction to OOP
✔
Classes and Objects
✔
Operator Overloading
●
Inheritance
Prerequisite:
●
Programming experience
References:
● The C++ Programming Language, Bjarne Stroustrup, 4e, Pearson
● C++ Primer Plus, Stephen Prata, 6e, Pearson
Concepts are applicable in general.
We will use C++ and Linux as the environments.
Concepts are applicable in general.
We will use C++ and Linux as the environments.