#include <iostream>
#include "header.h"
using namespace std;
int main()
{
Complexx a(3.0,4.0);
Complexx c;
cout<<"Podaj Liczbe zespolona (k, aby zakonczyc):\n";
while(cin >> c)
{
cout<<"c to "<<c<<endl;
//cout <<"sprzezona z c to" << ~c<<endl;
cout<<"a to "<<a<<endl;
cout<<"a + c wynosi"<<a + c<<endl;
cout<<"a - c wynosi"<<a - c<<endl;/*
cout<<"a * c wynosi"<<a * c<<endl;
cout<<"2 * c wynosi"<<2 * c<<endl;*/
cout<<"Podaj Liczbe zespolona (k, aby zakonczyc):\n";
}
cout << "Gotowe !"<<endl;
return 0;
}
#ifndef HEADER_H_
#define HEADER_H_
#include <iostream>
using namespace std;
class Complexx
{
private:
double real;
double imaginary;
public:
Complexx();
Complexx(double a, double b);
//~Complexx();
friend Complexx & operator+(const Complexx & a, const Complexx & b);
friend Complexx & operator-(const Complexx & a, const Complexx & b);/*
Complexx operator*(const Complexx & a);*/
friend Complexx & operator*(double a, const Complexx & v);
friend ostream & operator<<(ostream & os, const Complexx & v);
friend istream & operator>>(istream & ws, Complexx & v);
};
#endif // HEADER_H_
#include "header.h"
using namespace std;
Complexx::Complexx()
{
real = imaginary = 0.0;
}
Complexx::Complexx(double a, double b)
{
real = a;
imaginary = b;
}
/*Complexx::~Complexx()
{
imaginary = -imaginary;
}*/
Complexx & operator+(const Complexx & a, const Complexx & b)
{
Complexx res;
res.real = a.real + b.real;
res.imaginary = a.imaginary + b.imaginary;
return Complexx(res.real,res.imaginary);
}
Complexx & operator-(const Complexx & a, const Complexx & b)
{
Complexx res;
res.real = a.real - b.real;
res.imaginary = a.imaginary - b.imaginary;
return res;
}
/*
Complexx Complexx::operator*(const Complexx & a)
{
real * a.real;
imaginary * a.imaginary;
}
Complexx & operator*(double a, const Complexx & v)
{
v.real * a;
v.imaginary * a;
}*/
ostream & operator<<(ostream & os, const Complexx & v)
{
os<<"("<<v.real<<", "<<v.imaginary<<"i)";
return os;
}
istream & operator>>(istream & ws, Complexx & v)
{
ws >> v.real >> v.imaginary;
return ws;
}
Mam pytanie jak ma wyglądać definicja operatora+, żeby wywołanie a + c zwracało wynik jak w operatorze <<
Dam przykładowe wyjście o jakie mi chodzi:
wpisuje na wejsciu 10 i 12
a + c wynosi (13,16i)
a - c wynosi (-7,-8i)
Widzicie o co mi chodzi wywołanie a + c nie może być zwracane do 3 obiektu. Do obiektu a dodaje obiekt c wypisuje wynik ale nie zmieniam obiektu a (tak abstrakcyjnie xD)
Nie wiem czy zrozumiale napisałem o co mi chodzi
dzięki z góry