Homework 4
Homework 4
complex.h
#ifndef COMPLEX_H_INCLUDED
#define COMPLEX_H_INCLUDED
class Complex
{
public:
Complex();
Complex(double, double);
Complex operator*(Complex);
void Print();
bool operator!=(Complex);
private:
double Real, Imaginary;
};
#endif // COMPLEX_H_INCLUDED
Complex.cpp
#include "complex.h"
#include <iostream>
using namespace std;
Complex::Complex()
{
Real = 0;
Imaginary = 0;
}
Complex::Complex(double r, double i)
{
Real = r;
Imaginary = i;
}
Complex Complex::operator*(Complex a)
{
Complex t;
t.Real = (Real * a.Real)-(Imaginary*a.Imaginary);
t.Imaginary = (Real*a.Imaginary)+(a.Real*Imaginary);
return t;
}
void Complex::Print()
{
cout << Real<<" + "<<Imaginary<<"i"<< endl;
}
bool Complex::operator!=(Complex b)
{
bool result= (Real!=b.Real) || (Imaginary!=b.Imaginary);
return result;
}
main.cpp
#include <iostream>
#include "complex.h"
using namespace std;
int main()
{
Complex c1(11 , 30);
Complex c2(7 , 25);
Complex c3;
c3 = c1 * c2;
c3.Print();
return 0;