0% found this document useful (0 votes)
2 views4 pages

Homework 4

The document contains the implementation of a Complex class in C++ that supports basic operations such as multiplication and comparison. It includes a header file 'complex.h' defining the class and its methods, and a source file 'Complex.cpp' that implements these methods. The 'main.cpp' file demonstrates the usage of the Complex class by performing multiplication and equality checks on complex numbers.

Uploaded by

ahmed.sadik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Homework 4

The document contains the implementation of a Complex class in C++ that supports basic operations such as multiplication and comparison. It includes a header file 'complex.h' defining the class and its methods, and a source file 'Complex.cpp' that implements these methods. The 'main.cpp' file demonstrates the usage of the Complex class by performing multiplication and equality checks on complex numbers.

Uploaded by

ahmed.sadik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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;

cout<<"Result of two complex numbers multiplication : " << endl;

c3 = c1 * c2;
c3.Print();

bool result = c1 != c2;


if(result == true)
{
cout << "The objects are not equal" << endl;
}
else
{
cout << "The objects are equal" << endl;
}

return 0;

You might also like