Lecture-18-OperatorOverloading

Lecture-18-OperatorOverloading - Operator Overloading...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Operator Overloading
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Operator Overloading ± C++ has many operators that work on the built-in types (=, +, *, ->, [], <<, …) ± C++ allows us to define how the built-in operators work on new classes that we create ± String: +, +=, [], << ± Most, but not all, built-in operators may be defined for user-defined types
Background image of page 2
Operator Overloading ± Why do they call it "operator overloading"? ± Because the same operator has different meanings depending on what type it is applied to ± There are multiple implementations of the same operator (sometimes even on the same class) ± Operator overloading allows us to fully integrate new types into the C++ language (i.e., they look just like built-in types)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Strings Without Operator Overloading class String { }; void main() { String byu("BYU"); String gatech; gatech.Append("Georgia Tech"); String msg = gatech.Concat(" threw an interception.") ; msg.Write(cout); msg = byu.Concat(" kicked a field goal.") ; msg.Write(cout); for (int x=0; x < msg.Length(); ++x) { cout << msg.GetChar(x) ; } }
Background image of page 4
Strings With Operator Overloading class String { }; void main() { String byu("BYU");
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/02/2012 for the course C S 240 taught by Professor Kenrodham during the Winter '12 term at BYU.

Page1 / 20

Lecture-18-OperatorOverloading - Operator Overloading...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online