Unit 16 - Unit 16 Operator Overloading &...

Info iconThis preview shows pages 1–5. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Unit 16 Operator Overloading & Special Methods Unit Objectives How to define operators for your class. Learn when to use the 'const' keyword with methods. Learn how to create specialized methods: Default Constructors, Copy Constructors, and Destructors. A big example: Putting it all together. Introduction Congratulations! You are almost done! In the last unit, you learned to create classes. This unit is dedicated to adding additional functionality to classes. Don't start taking short cuts just yet! I have both this unit and the final Review Test to ensure you understand the material. Take your time and work the examples or you will end up investing more time in the long-run. At the end of the unit, We will work an example showing how much you have learned in this course. Task 1 - Operator Overloading. Let's return to the ComplexNumber class and start with the code we wrote in the previous unit. For your convenience, here again is the code. #ifndef COMPLEXNUMBER_H #define COMPLEXNUMBER_H class ComplexNumber { public: ComplexNumber( double newReal, double newImag ); ComplexNumber Add( ComplexNumber n ); ComplexNumber Multiply( ComplexNumber n ); void Print( ); void SetReal( double r ); double GetReal( ); void SetImag( double i ); double GetImag( ); private: double real; double imag; }; #endif //COMPLEXNUMBER_H #include <iostream> using namespace std; #include "ComplexNumber.h" ComplexNumber::ComplexNumber( double newReal, double newImag ) { real = newReal; imag = newImag; } ComplexNumber ComplexNumber::Add( ComplexNumber n ) { ComplexNumber temp( 0, 0 ); temp.real = real + n.real; temp.imag = imag + n.imag; return temp; } ComplexNumber ComplexNumber::Multiply( ComplexNumber n ) { ComplexNumber temp( 0, 0 ); temp.real = real*n.real + imag*n.imag*-1; //i^2 = -1 temp.imag = real*n.imag + imag*n.real; return temp; } void ComplexNumber::Print( ) { cout << real << " + " << imag << "i "; } void ComplexNumber::SetReal( double r ) { real = r; return; } double ComplexNumber::GetReal( ) { return real; } void ComplexNumber::SetImag( double i ) { imag = i; return; } double ComplexNumber::GetImag( ) { return imag; } Notice, the data is now private and thus there are methods to access the data. You are going to add methods to this class, so compile it along with this main function. #include <iostream> using namespace std; #include "ComplexNumber.h" int main() { ComplexNumber cnOne( 3, 4 ); ComplexNumber cnTwo( -2, 3); ComplexNumber cnAnswer( 0, 0 ); cnAnswer = cnOne.Add( cnTwo ); cnAnswer.Print(); cout << endl; cnAnswer = cnOne.Multiply( cnTwo ); cnAnswer.Print(); return 1; } Wouldn't it be nice if instead of using words, we could use +,*, and << operators to perform the same actions? Our main function is simpler if we have overloaded operators...
View Full Document

This note was uploaded on 10/02/2011 for the course ME 205 taught by Professor Koen during the Spring '07 term at University of Texas at Austin.

Page1 / 30

Unit 16 - Unit 16 Operator Overloading &...

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

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