#include <iostream>
using namespace std;
class Kelvin;
class TemperatureUnit
{
public:
explicit TemperatureUnit(int value) :
m_value(value)
{
}
virtual void print() = 0;
int value() const
{
return m_value;
}
private:
int m_value;
};
class Celsius : public TemperatureUnit
{
public:
Celsius(int value) :
TemperatureUnit(value)
{
}
void print() override
{
cout<<value()<<"[C]\n";
}
Celsius operator +(const Kelvin& kelvin);
};
class Kelvin : public TemperatureUnit
{
public:
Kelvin(int value) :
TemperatureUnit(value)
{
}
void print() override
{
cout<<value()<<"[K]\n";
}
Kelvin operator +(const Celsius& celsius);
};
//operator definitions
Celsius Celsius::operator +(const Kelvin& kelvin)
{
Celsius c(this->value() + kelvin.value() - 273);
return c;
}
Kelvin Kelvin::operator +(const Celsius& celsius)
{
Kelvin k(this->value() + celsius.value() + 273);
return k;
}
int main() {
Kelvin kelvin(293);
Celsius celsius(20);
(celsius + kelvin).print();
(kelvin + celsius).print();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3MgS2VsdmluOwoKY2xhc3MgVGVtcGVyYXR1cmVVbml0CnsKcHVibGljOgogICAgZXhwbGljaXQgVGVtcGVyYXR1cmVVbml0KGludCB2YWx1ZSkgOgogICAgICAgIG1fdmFsdWUodmFsdWUpCiAgICB7CiAgICB9CgogICAgdmlydHVhbCB2b2lkIHByaW50KCkgPSAwOwogICAgCiAgICBpbnQgdmFsdWUoKSBjb25zdAogICAgewogICAgICAgIHJldHVybiBtX3ZhbHVlOwogICAgfQpwcml2YXRlOgogICAgaW50IG1fdmFsdWU7Cn07CgpjbGFzcyBDZWxzaXVzIDogcHVibGljIFRlbXBlcmF0dXJlVW5pdAp7IAoKcHVibGljOgogICAgQ2Vsc2l1cyhpbnQgdmFsdWUpIDoKICAgICAgICBUZW1wZXJhdHVyZVVuaXQodmFsdWUpCiAgICB7CiAgICAgICAgCiAgICB9CiAgICB2b2lkIHByaW50KCkgb3ZlcnJpZGUKICAgIHsK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