L12_operator_overloading

L12_operator_overloading - 1 Operator Overloading For COP...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: 1 Operator Overloading For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Piyush Kumar Operator overloading C++ allows overloading of most of the standard operators. We are allowed to define standard operators for new types. The name of an operator, as a function, is “ operator ” followed by the operator itself. z The operator in “ a - b ” is named “ operator- ”. Suppose we have a class MyNum , a new numerical type. z The syntax for defining operators is z ReturnType operator@(argument list…) z For example: MyNum operator-(const MyNum & a, const MyNum &b); Example class MyNum { public: ... }; © C++ FAQ // Without operator overloading: MyNum add(const MyNum& x, const MyNum& y); MyNum mul(const MyNum& x, const MyNum& y); MyNum f(const MyNum& a, const MyNum& b, const MyNum& c) { return add(add(mul(a,b), mul(b,c)), mul(c,a)); // Yuk... } Example class MyNum { public: ... }; © C++ FAQ // With operator overloading: MyNum operator+ (const MyNum& x, const MyNum& y); MyNum operator* (const MyNum& x, const MyNum& y); MyNum f(const MyNum& a, const MyNum& b, const MyNum& c) { return a*b + b*c + c*a; } More examples? | Here are a few of the many examples of operator overloading: z myString + yourString might concatenate two std::string objects z myDate++ might increment a Date object z a * b might multiply two Number objects z a[i] might access an element of an Array object z x = *p might dereference a "smart pointer" that "points" to a disk record — it could seek to the location on disk where p "points" and return the appropriate record into x Operator Overloading | Operator overloading makes life easier for the users of a class , not for the developer of the class! | Supports more natural usage. | Uniformity with built in types. 2 Operator Overloading: Another example class Array { public: int& elem(unsigned i) { if (i > 99) error(); return data[i]; } private: int data[100]; }; int main() { Array a; a.elem(10) = 42; a.elem(12) += a.elem(13); ... } class Array { public: int& operator (unsigned i) { if (i > 99) error(); return data[i]; } private: int data[100]; }; int main() { Array a; a[10] = 42; a[12] += a[13]; ... } Precedence of Operators | Ival = jval = kval = lval z Right associative z (Ival = (jval = (kval = lval))) | Ival * jval / kval * lval z Left associative z (((Ival * jval) / kval) * lval) Operator Precedence in C++ z :: -> global / class / namespace scopes z . -> z subscript z () Parentheses z Unary operators z Multiplicative operators z Additive operators z Relational ordering z Relational equality z Logical and z Logical or z Assignment Operator precedence | x == y + z; | Precedence and associativity are fixed....
View Full Document

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue.

Page1 / 12

L12_operator_overloading - 1 Operator Overloading For COP...

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

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