Write a program having a Class Mile with data member dis_mile. Another Class Kilometer with data member dis_kilo. Both classes stores distances. In main function create objects these two classes with parameterized constructors. Write down conversion function for converting Kilometer object to Mile class object
#include <iostream>
class Mile
{
double dis_mile;
public:
explicit Mile(double mile) : dis_mile(mile)
{
}
double Get() const { return dis_mile; }
};
class Kilometer
{
double dis_kilo;
public:
explicit Kilometer(double km) : dis_kilo(km)
{
}
double Get() const { return dis_kilo; }
};
Mile Convert(Kilometer km)
{
return Mile(km.Get() * 0.621371);
}
int main()
{
Kilometer km(1);
Mile mile(1);
std::cout << km.Get() << " km = " << Convert(km).Get() << " miles\n";
return 0;
}
Comments
Thank you
Leave a comment