Witam, mam do zrobienia projekt na uczelnię. Mam problem z jedną kwestią: " implementuj większość funkcjonalności jako funkcje składowe klasy. Pisz funkcje nie składowe tylko gdy jest to uzasadnione". Udało mi się napisać działający kod gdzie wszystkie funkcje są funkcjami składowymi klasy, jednak wiem,że nie powinno tak być. Pytanie brzmi które funkcje powinienem wyrzucić do complex.cpp i dlaczego?
complex.hpp
#ifndef COMPLEX_HPP_
#define COMPLEX_HPP_
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex(double=0,double=0);
double getReal() const
{
return real;
}
double getImag() const
{
return imag;
} //zwraca cz. urojon¹
double getModule() const
{
double R,I;
R=real;
I=imag;
return sqrt(R*R+I*I);
}
double getPhase() const
{
double R, I, z, cos;
R=real;
I=imag;
z=sqrt(R*R+I*I);
cos=R/z;
if(I>0)
return (acos(cos)*180)/M_PI; //JESLI RADIANY, TO SAM ARCSIN
else
return (((acos(cos)*180)/M_PI)-360)*(-1);
} //zwraca fazê
friend ostream& operator << (ostream& out, const Complex& C)
{
if(C.real!=0)
out<<C.real;
else
out<<"";
if(C.imag==0 && C.real!=0)
out<<"";
else if(C.imag==0 && C.real==0)
out<<0;
else if(C.imag==1 && C.real!=0)
out<<" + i";
else if(C.imag==1 && C.real==0)
out<<" i";
else if(C.imag==-1)
out<<" - i";
else if(C.imag>0 && C.imag!=1 && C.real!=0)
out<<" + "<<C.imag<<"i";
else if(C.imag>0 && C.imag!=1 && C.real==0)
out<<C.imag<<"i";
else if(C.imag<0 && C.imag!=-1)
out<<" - "<<(-1)*C.imag<<"i";
out<<endl;
return out;
}
bool operator == (const Complex& C) const
{
if(this->real==C.real && this->imag==C.imag)
{
return true;
}
else
{
return false;
}
}
friend Complex operator + (const Complex& C1, const Complex& C2)
{
Complex tmp;
tmp.real=C1.real+C2.real;
tmp.imag=C1.imag+C2.imag;
return tmp;
}
Complex& operator += (const Complex& C)
{
*this=*this+C;
return *this;
}
friend Complex operator - (const Complex& C1, const Complex& C2)
{
Complex tmp;
tmp.real=C1.real-C2.real;
tmp.imag=C1.imag-C2.imag;
return tmp;
}
Complex& operator -= (const Complex& C)
{
*this=*this-C;
return *this;
}
friend Complex operator * (const Complex& C1, const Complex& C2)
{
Complex tmp;
tmp.real=C1.real*C2.real-C1.imag*C2.imag;
tmp.imag=C1.real*C2.imag+C1.imag*C2.real;
return tmp;
}
Complex& operator *= (const Complex& C)
{
*this=*this*C;
return *this;
}
friend Complex operator / (const Complex& C1, const Complex& C2)
{
Complex tmp, dzielna;
double dzielnik;
dzielna.real=C1.real*C2.real+C1.imag*C2.imag;
dzielna.imag=C1.real*(-1)*C2.imag+C1.imag*C2.real;
dzielnik=C2.real*C2.real+C2.imag*C2.imag;
tmp.real=dzielna.real/dzielnik;
tmp.imag=dzielna.imag/dzielnik;
return tmp;
}
Complex& operator /= (const Complex& C)
{
*this=*this/C;
return *this;
}
};
#endif
complex.cpp
#include "Complex.hpp"
using namespace std;
Complex::Complex(double R, double I)
{
real=R;
imag=I;
}
main
#include "Complex.hpp"
using namespace std;
int main()
{
Complex a(1,2), b(1,-2), c(1,1), d;
int x;
cout<<"a = "<<a<<"b = "<<b<<"c ="<<c<<"d ="<<d;
cout<<endl;
cout<<"a + b = "<<a+b;
cout<<"a - b = "<<a-b;
cout<<"a * b = "<<a*b;
cout<<"a / b = "<<a/b;
cout<<10*a;
cout<<a*10;
a+=b+=c;
if(!(c==a))cout<<"ok"<<endl;
cout<<a;
cout<<endl;
cout<<"Czesc rzeczywista : "<<a.getReal()<<endl;
cout<<"Czesc urojona : "<<a.getImag()<<endl;
cout<<"Modul : "<<a.getModule()<<endl;
cout<<"Faza : "<<a.getPhase()<<endl;
return 0;
}
- proj.rar (1 KB) - ściągnięć: 50