Exercise 1: - Exercises
Exercise 1: - Exercises
• Exercise 1
• Exercise 2
Exercise 1 #
Extend the MyDistance class we saw in the previous lesson to support the
following units:
Feet: 1 ft = 0.3048m
Mile: 1 mi = 1609.344m
#include <iostream>
#include <ostream>
namespace Distance{
class MyDistance{
public:
MyDistance(double i):m(i){}
};
namespace Unit{
MyDistance operator "" _km(long double d){
return MyDistance(1000*d);
}
MyDistance operator "" _m(long double m){
return MyDistance(m);
}
MyDistance operator "" _dm(long double d){
return MyDistance(d/10);
}
MyDistance operator "" _cm(long double c){
return MyDistance(c/100);
}
}
}
int main(){
Exercise 2 #
The total distance of someone’s weekly drive consists of many components.
Extend MyDistance so that we can calculate the total distance based on the
following formula:
work is in km whereas all the others are in m . All of them are long doubles.
#include <iostream>
#include <ostream>
namespace Distance{
class MyDistance{
public:
MyDistance(double i):m(i){}
};
namespace Unit{
MyDistance operator "" _km(long double d){
return MyDistance(1000*d);
}
MyDistance operator "" _m(long double m){
return MyDistance(m);
}
MyDistance operator "" _dm(long double d){
return MyDistance(d/10);
}
MyDistance operator "" _cm(long double c){
return MyDistance(c/100);
}
}
}
int main(){
}
The next lesson contains the solution for both exercises.