0% found this document useful (0 votes)
41 views4 pages

Exercise 1: - Exercises

The document presents two exercises to extend the MyDistance class to support additional units of measurement and calculations. Exercise 1 asks to support feet and mile units. Exercise 2 asks to calculate a formula for total weekly drive distance incorporating work, abbreviation to work, workout, and shop distances in different units. The next lesson will provide the solutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views4 pages

Exercise 1: - Exercises

The document presents two exercises to extend the MyDistance class to support additional units of measurement and calculations. Exercise 1 asks to support feet and mile units. Exercise 2 asks to calculate a formula for total weekly drive distance incorporating work, abbreviation to work, workout, and shop distances in different units. The next lesson will provide the solutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

- Exercises

Let's test our knowledge of literals with these exercises.

WE'LL COVER THE FOLLOWING

• 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

Choose good suffixes for these units.

Write an implementation below:

#include <iostream>
#include <ostream>

namespace Distance{
class MyDistance{
public:
MyDistance(double i):m(i){}

friend MyDistance operator +(const MyDistance& a, const MyDistance& b){


return MyDistance(a.m + b.m);
}
friend MyDistance operator -(const MyDistance& a, const MyDistance& b){
return MyDistance(a.m - b.m);
}

friend std::ostream& operator<< (std::ostream &out, const MyDistance& myDist){


out << myDist.m << " m";
return out;
}
private:
double m;

};

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);
}
}
}

using namespace Distance::Unit;

int main(){

std:: cout << std::endl;

std::cout << "1.0_km: " << 1.0_km << std::endl;


std::cout << "1.0_m: " << 1.0_m << std::endl;
std::cout << "1.0_dm: " << 1.0_dm << std::endl;
std::cout << "1.0_cm: " << 1.0_cm << std::endl;

std::cout << std::endl;


std::cout << "1.0_km + 2.0_dm + 3.0_dm - 4.0_cm: " << 1.0_km + 2.0_dm + 3.0_dm - 4.0_cm <
std::cout << std::endl;

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:

myDistP erW eek = 4 ∗ work ∗ 2 − 3 ∗ abbrevationT oW ork + workout + shop

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){}

friend MyDistance operator +(const MyDistance& a, const MyDistance& b){


return MyDistance(a.m + b.m);
}
friend MyDistance operator -(const MyDistance& a, const MyDistance& b){
return MyDistance(a.m - b.m);
}

friend std::ostream& operator<< (std::ostream &out, const MyDistance& myDist){


out << myDist.m << " m";
return out;
}
private:
double m;

};

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);
}
}
}

using namespace Distance::Unit;

int main(){

std:: cout << std::endl;

std::cout << "1.0_km: " << 1.0_km << std::endl;


std::cout << "1.0_m: " << 1.0_m << std::endl;
std::cout << "1.0_dm: " << 1.0_dm << std::endl;
std::cout << "1.0_cm: " << 1.0_cm << std::endl;

std::cout << std::endl;


std::cout << "1.0_km + 2.0_dm + 3.0_dm - 4.0_cm: " << 1.0_km + 2.0_dm + 3.0_dm - 4.0_cm <
std::cout << std::endl;

}
The next lesson contains the solution for both exercises.

You might also like