0% found this document useful (0 votes)
41 views

Primer 5

The document defines a Complex class to represent complex numbers with real and imaginary parts. The class defines constructors, member functions to get/set parts, and operators for addition, subtraction, multiplication, equality comparison, output streaming. Friend functions are used to define operators that take Complex objects as parameters or return Complex objects. The code provides an implementation of the class and tests various operations.

Uploaded by

bboldi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Primer 5

The document defines a Complex class to represent complex numbers with real and imaginary parts. The class defines constructors, member functions to get/set parts, and operators for addition, subtraction, multiplication, equality comparison, output streaming. Friend functions are used to define operators that take Complex objects as parameters or return Complex objects. The code provides an implementation of the class and tests various operations.

Uploaded by

bboldi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

// complex.

hpp
#ifndef COMPLEX_HPP_INCLUDED
#define COMPLEX_HPP_INCLUDED

#include <iostream>
using namespace std;

class Complex {
private:
double re;
double im;
public:
Complex() { re=1; im=1;}
Complex(double re1, double im1) {re=re1; im=im1;}
Complex(const Complex& z) {re=z.re; im=z.im;}
double getRe() const {return re;}
double getIm() const {return im;}
void setRe(double re1) {re=re1;}
void setIm(double im1) {im=im1;}
Complex& operator=(const Complex&);
Complex& operator+=(const Complex&);
Complex& operator-=(const Complex&);
Complex& operator*=(const Complex&);
friend Complex operator+(const Complex&, const Complex&);
friend Complex operator-(const Complex&, const Complex&);
friend Complex operator*(const Complex&, const Complex&);
friend bool operator==(const Complex&, const Complex&);
friend bool operator!=(const Complex&, const Complex&);
friend ostream& operator<<(ostream&, const Complex&);
};

#endif // COMPLEX_HPP_INCLUDED

// complex.cpp
#include "complex.hpp"

Complex& Complex::operator=(const Complex &z) {


re = z.re; im=z.im;
return *this;
}

Complex& Complex::operator+=(const Complex &z) {


re+=z.re; im+=z.im;
return *this;
}
Complex& Complex::operator-=(const Complex &z) {
re-=z.re; im-=z.im;
return *this;
}

Complex& Complex::operator*=(const Complex &z) {


double r=re*z.re - im*z.im;
double i=re*z.im + im*z.re;
re=r; im=i;
return *this;
}

Complex operator+(const Complex &z1, const Complex &z2) {


Complex w(z1.re+z2.re, z1.im+z2.im);
return w;
}

Complex operator-(const Complex &z1, const Complex &z2) {


Complex w(z1.re-z2.re, z1.im-z2.im);
return w;
}

Complex operator*(const Complex &z1, const Complex &z2) {


double r=z1.re*z2.re - z1.im*z2.im;
double i=z1.re*z2.im + z1.im*z2.re;
Complex w(r, i);
return w;
}

bool operator==(const Complex &z1, const Complex &z2) {


if(z1.re==z2.re && z1.im==z2.im)
return true;
else
return false;
}

bool operator!=(const Complex &z1, const Complex &z2) {


if(z1.re!=z2.re || z1.im!=z2.im)
return true;
else
return false;
}

ostream& operator<<(ostream &out, const Complex &z) {


if(z.re==0 && z.im!=0) out<<z.im<<"i";
if(z.re!=0 && z.im==0) out<<z.re;
if(z.re!=0 && z.im>0) out<<z.re<<"+"<<z.im<<"i";
if(z.re!=0 && z.im<0) out<<z.re<<z.im<<"i";
return out;
}

// main.cpp
#include "complex.hpp"

int main()
{
Complex c(3,5);
Complex d(4,6);
cout <<c<< endl;
cout <<d<< endl;
c+=d;
cout <<c<< endl;
c*=d;
cout <<c<< endl;
c=c+d+c;
cout <<c<< endl;
return 0;
}

You might also like