Here's the code:
Expand|Select|Wrap|Line Numbers
- //#include "complex.h"//implementation file
- #include <iostream>
- //#include<math.h>
- using namespace std;
- class Complex
- {
- public:
- Complex(double, double);
- Complex();
- Complex(double);
- friend ostream& operator <<(ostream& out, const Complex& c);
- friend istream& operator >>(istream& in, Complex& c);
- friend const Complex operator *(const Complex& l, const Complex& r);
- friend const Complex operator -(const Complex& l, const Complex& r);
- friend const Complex operator +(const Complex& l, const Complex& r);
- friend bool operator ==(const Complex& l, const Complex& r);
- private:
- double real, imag;
- };
- // define constructor
- Complex::Complex( double r, double i )
- {
- real = r; imag = i;
- }
- Complex::Complex()
- {
- real = 0;
- imag = 0;
- }
- Complex::Complex(double realPart ) //constructor that takes a single parameter
- {
- realPart = 0;
- int i = 1;
- }
- ostream& operator <<(ostream& out, const Complex& c)
- {
- out << c.real << "+" << c.imag << "i";
- return out;
- }
- istream& operator >>(istream& in, Complex& c)
- {
- char op;
- char i;
- in >> c.real >> op >> c.imag >> i;
- return in;
- }
- // define overloaded + (plus) operator
- const Complex operator +(const Complex& l, const Complex& r)
- {
- Complex result;
- result.real = (l.real + r.real);
- result.imag = (l.imag + r.imag);
- return result;
- }
- const Complex operator -(const Complex& l, const Complex& r)
- {
- Complex result;
- result.real = (l.real - r.real);
- result.imag = (l.imag - r.imag);
- return result;
- }
- const Complex operator *(const Complex& l, const Complex& r)
- {
- int a = (l.real*r.real - l.imag*r.imag);
- int b = (l.imag*r.real + l.real*r.imag);
- Complex result(a,b);
- return result;
- }
- bool operator ==(const Complex& l, const Complex& r)
- {
- return ((l.real == r.real) && (l.imag == r.imag));
- }
- int main()
- {
- Complex a(4,5), b(6,5);
- double realPart = 0;
- Complex i(0,1);
- cout << "First complex number is " << a << endl;
- cout << "Second complex number is " << b << endl << endl;
- cout << "Testing constant i..."<<endl;
- cout << "The value of our constant i is: " << i << endl <<endl;
- cout << "Enter 2 new complex numbers in the form a+bi(with no spaces):" << endl;
- cin >> a >> b;
- cout << endl;
- cout << a << " + " << b <<" = "<< a + b<< endl;
- cout << a << " - " << b <<" = "<< a - b<< endl;
- cout << a << " * " << b <<" = "<< a * b<< endl << endl;
- cout << "Testing equality operator..." << endl;
- if (a == b)
- cout << a << " is equal to " << b << endl;
- else
- cout << a << " is not equal to " << b << endl;
- return 0;
- }
C:\Documents and Settings\Default\Desktop>cl /EHsc pr3d.cpp
pr3.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version
13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights
reserved.
pr3d.cpp
c1xx : fatal error C1083: Cannot open source file:
'pr3d.cpp': No such file or d
irectory
pr3.cpp
c1xx : fatal error C1083: Cannot open source file:
'pr3.cpp': No such file or di
rectory
Generating Code...
C:\Documents and Settings\Default\Desktop>cd exP-3\
C:\Documents and Settings\Default\Desktop\exP-3>cl /EHsc
pr3d.cpp pr3.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version
13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights
reserved.