This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: C++ Primer CSE 250 Lecture Note 03 C++ Primer (3) Adapted from the KoffmanWolfgang 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 Redistribution 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 Complex Class Complex number: The numbers of the form a + bi are called complex numbers Examples: 3+6i; 4.53.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 unnatural. 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 ....
View
Full
Document
This note was uploaded on 10/31/2010 for the course EE 423 taught by Professor Mitin during the Spring '10 term at SUNY Buffalo.
 Spring '10
 MITIN

Click to edit the document details