{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Classnote03-1x2

# Classnote03-1x2 - C Primer CSE 250 Lecture Note 03 C...

This preview shows pages 1–5. Sign up to view the full content.

C++ Primer CSE 250 Lecture Note 03 C++ Primer (3) Adapted from the Koffman-Wolfgang C++ Primer, from the Instructor Materials slides by Elliot Koffman and Paul Wolfgang. This material is used by permission of John Wiley & Sons, Inc For private use only by students in CSE250 Re-distribution or public posting in any form is expressly forbidden. c Xin He (University at Buffalo) CSE 250 Data Structures Classnote 03 1 / 109 Additional Features and Concepts A number of additional C++ features and concepts are shown in textbook by using complex examples. The main ideas somehow are not easily seen (or not shown). We will show these features and concepts by using simple examples. Keep them simple, in order to highlight the main ideas and concerns involved. c Xin He (University at Buffalo) CSE 250 Data Structures Classnote 03 4 / 109

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

View Full Document
Complex Class Complex number: The numbers of the form a + bi are called complex numbers Examples: 3+6i; 4.5-3.18i. Addition : ( a + bi ) + ( c + di ) = ( a + b ) + ( c + d ) i multiplication : ( a + bi ) * ( c + di ) = ( ac - bd ) + ( ad + bc ) i Complex class will implement complex numbers and operations. c Xin He (University at Buffalo) CSE 250 Data Structures Classnote 03 6 / 109 Complex.h class Complex { private: int real; // the real part of the complex number int imag; // the imaginary part of the complex number public: Complex(): real(0), imag(0) {} // two constructors Complex(int a, int b): real(a), imag(b) {} ˜Complex() {} //destructor ... Complex add(const Complex& x); // add method Complex multiply(const Complex& x); // multiply method Complex& operator = (const Complex& x) // assignment operator { real = x.real; imag = x.imag; return * this; } bool operator == (const Complex& x) // comparison operator { return ((real = x.real) && (imag = x.imag)); } //addition and multiplication operators. Complex operator+ (const Complex& x) {return this->add(x);} Complex operator * (const Complex& x) {return this->multiply(x);} friend ostream& operator << (ostream& os, Complex x); }; c Xin He (University at Buffalo) CSE 250 Data Structures Classnote 03 8 / 109
+ and * operators You call add function: Complex add(const Complex& x); by Complex x (3, 4), y(5,-6); Complex z = x.add(y); it looks un-natural. With add operator+ , Complex operator+ (const Complex& x) {return this->add(x);} you can: Complex z = x+y; It looks more natural. Similar for multiply operator * . c Xin He (University at Buffalo) CSE 250 Data Structures Classnote 03 10 / 109 assignment operator = The assignment operator= : Complex& operator = (const Complex& x) { real = x.real; imag = x.imag; return * this; } Allows us: Complex x(4,3); Complex y; y = x; These statements create a new Complex object y ; and assign the content of x into y . Without this operator, the statement y = x would be illegal. If a class C contains a lots data members, you should provide a assignment operator. c Xin He (University at Buffalo) CSE 250 Data Structures Classnote 03 12 / 109

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

View Full Document
Comparison operator == The comparison operator== : bool operator == (const Complex& x) { return ((real = x.real) && (imag = x.imag)); } Allows us: if (x == y) { .... } Without this, you cannot compare them.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 26

Classnote03-1x2 - C Primer CSE 250 Lecture Note 03 C...

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

View Full Document
Ask a homework question - tutors are online