Operator Overloading Notes

Operator Overloading Notes - 0.1 Friend methods and...

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

View Full Document Right Arrow Icon
0.1 Friend methods and operator overloading Overloading binary operators is a one way operation - it only works for the case where you are adding, subtracting, etc. something to the right of the class you’re working with. Consider the following code. ComplexNumber is a class that represents a certain type of number we Fnd in mathematics. A complex number has an imaginary component, and a real component. We would like to be able to add real numbers to complex numbers and vice-versa, and also to equate the two. 1 // File : ComplexNumber . h 2 3 #ifndef COMPLEX NUMBER H 4 #define COMPLEX NUMBER H 5 6 #include < iostream > 7 8 using namespace std ; 9 10 c l a s s ComplexNumber 11 { 12 public : 13 ComplexNumber( double newReal , 14 double newImaginary ) ; 15 16 const 17 ( double someReal ) ; 18 19 ComplexNumber operator+ 20 ( double someReal ) ; 21 22 << 23 const ComplexNumber& cn ) ; 24 25 private : 26 double real , imaginary ; 27 } ; 28
Background image of page 1

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

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

This note was uploaded on 02/27/2012 for the course IT 101 taught by Professor Unsure during the Spring '11 term at University of South Africa.

Page1 / 4

Operator Overloading Notes - 0.1 Friend methods and...

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

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