You are on page 1of 5

Object Oriented Design Exercise 4 Function Overloading 5 Points

DUE: TUESDAY, March 5, 2013, AT 7:00 A.M.

Function Overloading

In this exercise, you will dene a complex data type complx, to manipulate complex numbers: a + bi. In complx class where you can declare complex data, input/output complex data and check if two complex numbers equal each other or not using the same operators used for other built-in data types. You need to partially implement a few functions (in red) in class complx. This exercise is a supplement and preparation to the Assignment.

// Complx.h #include <iostream> #include <fstream> using namespace std; class complx { double real,} imag; public: complx( double real = 0., double imag = 0.); // constructor complx(); // destructor friend int operator== (const complx&, const complx&); 1

//Sets private data members. void Set(double new_real, double new_imaginary) { real = new_real; imag = new_imaginary; } //Returns the real part of the complex number. double Real() { return real; } //Returns the imaginary part of the complex number. double Imaginary() { return imag; } }; extern ifstream &operator >> ( ifstream &in_file, complx &number );

extern istream &operator >> ( istream &in_file, complx &number ); extern ostream &operator << ( ostream &out_file, complx &number ); The following code implements two functions in the class. You need to implement the rest of the operators (in red). You may refer to the sample program on class complx in week 6 lectures Objects and Classes and the solutions to question 5 in Quiz 3 on how to implement functions/operators related to class complx. For the creation and compilation of multiple source le project in Dev-C++, please refer to Separate Compilation in Module 7. // Complx.cpp #include "complx.h" complx::complx() {

...

} ostream &operator<< ( ostream &out_file, complx &number ) {

...

extern ifstream &operator >> ( ifstream &in_file, complx &number ) { double re, is; char ch; if (in_file >> ch && ch == (&& in_file >> re >> ch && ch == , && in_file >> is >> ch && ch == )) number.Set(re,is); else cerr << "Finish the input"<<endl; return in_file; } extern istream &operator >> ( istream &in_file, complx &number ) {

...

} // define constructor complx::complx( double r, double i ) { real = r; imag = i; } int operator== (const complx& a, const complx& b){

...

Submission Instructions

1. Run the following main program using the input sequence (2,3) (6,7) (6,7) (4,5) (2,3) 2. Capture the screen output using Ctrl + Alt + P rintScreen and Paste and save the screen image into a Word le output.doc.

//call_complx.cpp #include "complx.h" int main() { int i=0; complx c1, c2(3,5),in[5]; cout<< "Please input a complex number in the format of (a,b) while ((cin >> in[i])&& (i<4)){ cout << in[i] << endl; i++; } if (c1 == in[0] ) cout << c1 << " equals to " << in[0] << endl; else cout << c1 << " not equal to " << in[0]<< endl; if (in[1] == in[2] ) cout << in[1] << " equals to " << in[2] << endl; else cout << in[1] << " not equal to " << in[2]<< endl; char c; cin >> c; return 0; //successful termination } 4

This is a sample output screen for the input and the output Please input a complex number in the format of (a,b) (2,3) (2,3) (6,7) (6,7) (6,7) (6,7) (4,5) (4,5) (2,3) (0,0) not equal to (2,3) (6,7) equals to (6,7) Package source les (complx.h, complx.cpp, call complx.cpp and output.doc) into < Y OU R N ET ID > exer4.zip and upload it online using Assignments menu.

You might also like