Define two classes Fahrenheit and celsius to represent temperature in Fahrenheit and celsius respectively.Use conversion function to convert from one system to the other
#include <iostream>
#include <string>
using namespace std;
class Fahrenheit
{
float Fvalue;
public:
Fahrenheit(float _Fvalue):Fvalue(_Fvalue){}
float GetValue() const{ return Fvalue; }
void Display()
{
cout << "\nValue is " << Fvalue << " Fahrenheit";
}
};
class Celsius
{
float Cvalue;
public:
Celsius(float _Cvalue):Cvalue(_Cvalue){}
float GetValue()const { return Cvalue; }
void Display()
{
cout << "\nValue is " << Cvalue << " Celsius";
}
};
Celsius Convert(const Fahrenheit& f)
{
float a = (f.GetValue() - 32) * 5 / 9;
return Celsius(a);
}
Fahrenheit Convert(const Celsius& c)
{
float a = (c.GetValue() *9 )/ 5 + 32;
return Fahrenheit(a);
}
int main()
{
Fahrenheit f(100);
f.Display();
Celsius c = Convert(f);
cout << "\nis equal to ";
c.Display();
cout << endl;
Celsius cl(50);
cl.Display();
cout << "\nis equal to ";
Fahrenheit fr = Convert(cl);
fr.Display();
}
Comments
Leave a comment