M07_OPO - bEgInSlIdE Copyright 1995 2007 C Gotsman& Y.M Kimchi Computer Science Dept Technion 1 Chapter 7 The C Programming Language Operator

Info iconThis preview shows pages 1–7. 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

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

View Full DocumentRight 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: bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion 1 Chapter 7 The C++ Programming Language Operator Overloading bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion 2 Operator Overloading Having defined the class Complex , we want to be able to add two complex numbers. Defining a standard function would make the code harder to read. If only we had + as a function we could have overloaded it. Complex z0(1,2), z1(3.5), z2; z2 = z0 + z1; // Syntax error The compiler knows that it does not know how to add complex numbers z = x + y ; // Add two double s Z2 = z0.add(z1); // Add two Complexes // Yes, operator + is a function operator +() // The name of the function bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion 3 Operator Overloading Almost all standard C++ operators can be overloaded for classes (and structures) by defining functions of the form operator OP , where OP is one of the predefined C++ operators. Here is the first version of operator +() , implemented as a member function: class Complex { public : Complex( double r = 0, double i = 0) : real(r), imag(i) {} Complex operator + ( const Complex&) const ; private : double real,imag; }; Complex Complex:: operator + ( const Complex& z) const { return Complex(real + z.real, imag + z.imag);} operator+ does not modify its arguments bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion 4 Using Overloaded Operators Let z1 , z2 and z3 be Complex: Complex z1(1,2), z2(-1,4), z3; then the C++ code: z3 = z1 + z2; actually calls two operator functions: z3. operator = (z1. operator + (z2)); where the function operator =() is predefined by the compiler . bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion 5 Pre-Defined Operators  The only operators defined by default for all classes are:  Address of: operator & (unary)  Assignment: operator = (binary)  Allows assignment of one object of the class to another.  Memberwise (recursive) copy assignment  Sequencing (comma): operator , (binary) - separates two expressions, guaranteeing they are evaluated left to right.  Even these are just a result of a “Historical Accident” (compatibility with C). bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion 6 Basic Rules of Operator Overloading  The overloaded instance of an operator must contain at least one argument of a class type.  Otherwise other versions of the operator may be invoked.  Only the predefined operators may be overloaded.  The predefined precedence and associativity direction of any operator cannot be changed ....
View Full Document

This note was uploaded on 04/14/2011 for the course CS 234122 taught by Professor Gillbarequet during the Spring '11 term at Technion.

Page1 / 46

M07_OPO - bEgInSlIdE Copyright 1995 2007 C Gotsman& Y.M Kimchi Computer Science Dept Technion 1 Chapter 7 The C Programming Language Operator

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

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