Primer 5
Primer 5
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"
// 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;
}