Cześć
Ćwiczac przeciazanie operatorow, natknolem sie na problem wynikajacy z braku zdefiniowanej wczesniej klasy Kelvin, oczywiscie czytalem o predefiniowaniu klasy tuz po include ale nie pomaga.To co niechce dzialac, zakomentowalem .
Caly poniższy kod znajduje sie w jednym pliku
#include <iostream>
using namespace std;
class Kelvin;
class CelsiusDeg;
class CelsiusDeg{
private:
double value;
public:
double getValue() const{
return this->value;
}
void setValue(double value){
this->value=value;
}
CelsiusDeg(){
this->setValue(0);
}
CelsiusDeg(double value){
this->setValue(value);
}
friend ostream& operator<<(ostream& os,const CelsiusDeg& obj){
return os<<obj.getValue()<<" [st. C]";
}
CelsiusDeg operator + (const CelsiusDeg &obj) {
CelsiusDeg result(obj.getValue()+this->value);
return result;
}
CelsiusDeg operator - (const CelsiusDeg &obj) {
CelsiusDeg result(this->value-obj.getValue());
return result;
}
/* CelsiusDeg operator - (const Kelvin &obj) {
CelsiusDeg result(this->value-(obj.getValue()+273.15));
return result;
}
CelsiusDeg operator + (const Kelvin &obj) {
CelsiusDeg result(this->value+(obj.getValue()+273.15));
return result;
} **/
};
class Kelvin{
private:
double value;
public:
double getValue() const{
return this->value;
}
void setValue(double value){
this->value=value;
}
Kelvin(){
this->setValue(0);
}
Kelvin(double value){
this->setValue(value);
}
friend ostream& operator<<(ostream& os,const Kelvin& obj){
return os<<obj.getValue()<<" [K]";
}
Kelvin operator + (const Kelvin &obj) {
Kelvin result(obj.getValue()+this->value);
return result;
}
Kelvin operator - (const Kelvin &obj) {
Kelvin result(this->value-obj.getValue());
return result;
}
Kelvin operator - (const CelsiusDeg &obj) {
Kelvin result(this->value-(obj.getValue()-273.15));
return result;
}
Kelvin operator + (const CelsiusDeg &obj) {
Kelvin result(this->value+(obj.getValue()-273.15));
return result;
}
};
int main(int argc, char** argv) {
Kelvin* j1=new Kelvin();
CelsiusDeg* m1=new CelsiusDeg();
Kelvin* j2=new Kelvin(23.15);
CelsiusDeg* m2=new CelsiusDeg(56.01);
//referencje bo latwiej na nich operowac niz na wskaznikach
Kelvin &J1= *j1;//dereferencja, dostanie sie do wartosci wskaznika
Kelvin &J2= *j2;
CelsiusDeg &M1= *m1;//dereferencja, dostanie sie do wartosci wskaznika
CelsiusDeg &M2= *m2;
cout<<"Takie same typy"<<endl;
cout<<(J1+J2)<<endl;
cout<<(J1-J2)<<endl;
cout<<(M1+M2)<<endl;
cout<<(M1-M2)<<endl;
cout<<"Rózne typy"<<endl;
// cout<<(M1+J2)<<endl;
// cout<<(M2-J1)<<endl;
cout<<(J2+M1)<<endl;
cout<<(J1-J2)<<endl;
delete j1,j2,m1,m2;
return 0;
}