Operator Overloading - EECE 230 Lectures Notes S Karaki 47...

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

View Full Document Right Arrow Icon
EECE 230 – Lectures Notes: S. Karaki 47 8 Operator Overloading 8.1 Introduction In function overloading more than one function definition may share the same name. The compiler recognizes the required function by argument type matching. Function overloading forms the foundation of operator overloading. Assume a class complex: class complex { private: double real, imag; public: complex(double r, double i = 0.0) { real = r; imag = i;} friend complex sqrt(complex); }; complex sqrt(const complex c) { } With such definition, it is now possible to evaluate the square root of a complex number as follows: double d = 9; double t = sqrt(d) complex c(3.0, 4.0); complex z = sqrt(c); Exercise: Implement function complex sqrt to calculate the square root of a complex number. 8.2 Fundamentals of Operator Overloading C++ allows (most) operators to be overloaded, i.e. to be given mew meanings. Note that one cannot redefine the associativity and arity of an operator, and one cannot also define new operators!
Background image of page 1

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

View Full DocumentRight Arrow Icon
EECE 230 – Lectures Notes: S. Karaki 48 The operation ‘+’ is overloaded by defining a global function overload with the name ‘operator+’. The following example illustrates how to overload the operator + to allow the addition of two complex numbers. Example: class complex { private: double real, imag; public: complex(double r, double i = 0.0) { real = r; imag = i;} friend complex operator+(complex, complex); }; complex operator +(complex a, complex b) { return complex (a. real + b. real, a. imag +b. imag); } So the complex operation ‘+’ is now possible: complex a(1.0, 2.0); complex b(2.0, 2.0); complex c = a + b; But since operator definitions are given as functions, the last statement above can be written as follows to give the same result: complex c = operator + (a, b); Notes: The assignment operator = may be used without explicit overloading The reference operator & may be used without explicit overloading 8.3 Restrictions Predefined operators can be overloaded. No new operators can be defined. The arity (unary, binary) cannot be changed. The priority and associativity of operators cannot be changed. Operators sizeof, ::, . , . * ,?: cannot be overloaded. Operators =, ->, -> * , ( ), [ ] can be overloaded as member functions only. Operator on primitive types (i.e. int, double, char) cannot be redefined.
Background image of page 2
EECE 230 – Lectures Notes: S. Karaki 49 8.3 Operator Overloads as class members or Friends The operator + can also be overloaded using a member function as compared to overloading it using a global function as shown above. Note that in this case the operator is overloaded as a member function with one argument. The other argument is implicit; it is the object invoking the member function. This is illustrated in the following example: class complex { private: double real, imag; public: complex(double r, double i = 0.0) { real = r; imag = i;} complex operator+(complex); }; complex complex::operator +(complex a) { return complex (real + a.real, imag + a.imag);
Background image of page 3

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

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

This note was uploaded on 02/26/2010 for the course EECE 230 taught by Professor Samikaraki during the Spring '07 term at American University of Beirut.

Page1 / 13

Operator Overloading - EECE 230 Lectures Notes S Karaki 47...

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

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