In: Computer Science
This question is in C++
Problem 3
Write a templated C++ class to represent an itinerary.
An itinerary has a title, a source position and a destination position. In addition, it may include intermediate positions.
All positions have the same abstract type T. The real type can be decided later, for example it can be a cartesian point with x,y,z coordinates, or an airport code, or a city name, or a country name, etc ..
The itinerary includes a vector of times: departure time, arrival time at the next position, time of departure from the next position, and so on. The last entry in this vector is the time of arrival at the destination position.
Write a function to print the itinerary with the time schedule.
Overload the addition operator to add two itineraries.
Test your templated class in a main function.
It only requires time not date, based on a 24 hour clock (eg. 1 PM would be 13:00)
Hi,
Please find the code below.
#include <iostream>
#include <vector>
using namespace std;
struct timeDataType
{
int tm_hour;
int tm_min;
timeDataType(int h,int m)
{
tm_hour = h;
tm_min = m;
}
};
template <typename T>
class Itinerary
{
public:
T title;
T source;
T destination;
std::vector<T> intermediates;
std::vector<timeDataType> vectOfTimes;
void print()
{
cout << "Title: " << title << " Source: "<<
source << " Destination: " << destination <<
endl;
cout << "Intermediates :" << endl;
for (auto i = intermediates.begin(); i != intermediates.end();
++i)
cout << *i << " ";
cout << endl;
}
void printVectOfTimes()
{
cout << "Vector of Times" << endl;
for (int count = 0; count < vectOfTimes.size(); count++)
{
cout << vectOfTimes[count].tm_hour << ":" <<
vectOfTimes[count].tm_min << endl;
}
}
friend Itinerary& operator+(Itinerary& lhs, const
Itinerary& rhs)
{
lhs.intermediates.push_back(lhs.destination);
lhs.destination = rhs.destination;
lhs.intermediates.push_back(rhs.source);
for (auto i = rhs.intermediates.begin(); i !=
rhs.intermediates.end(); ++i)
{
lhs.intermediates.push_back(*i);
}
return lhs;
}
};
int main()
{
Itinerary<string> it1;
it1.title = "It1";
it1.source = "A";
it1.destination = "C";
it1.intermediates.push_back("B1");
it1.intermediates.push_back("B2");
// All the times in hour:min format are taken as input.
// they can be hardcoded also.
int hour,min;
cout << "Enter hour in 24 hr(hh:mm) format of source :"
<< endl;
cin >> hour >> min;
timeDataType mysource(hour,min);
it1.vectOfTimes.push_back(mysource);
cout << " No of intermediates in itinerary 1 are : " <<
it1.intermediates.size() << endl;
for (int count = 1; count <= it1.intermediates.size();
count++)
{
cout << "Enter hour in 24 hr(hh:mm) format of intermediate :"
<< count << endl;
cin >> hour >> min;
timeDataType intermediate(hour,min);
it1.vectOfTimes.push_back(intermediate);
}
cout << "Enter hour in 24 hr(hh:mm) format of destination :"
<< endl;
cin >> hour >> min;
timeDataType destin(hour,min);
it1.vectOfTimes.push_back(destin);
Itinerary<string> it2;
it2.title = "It2";
it2.source = "D";
it2.destination = "F";
it2.intermediates.push_back("E1");
cout << "Enter hour in 24 hr(hh:mm) format of source of
Itinerary 2 :" << endl;
cin >> hour >> min;
timeDataType mysource1(hour,min);
it2.vectOfTimes.push_back(mysource1);
cout << " No of intermediates in itinerary 1 are : " <<
it2.intermediates.size() << endl;
for (int count = 1; count <= it2.intermediates.size();
count++)
{
cout << "Enter hour in 24 hr(hh:mm) format of intermediate of
Itinerary 2 :" << count << endl;
cin >> hour >> min;
timeDataType intermediate(hour,min);
it2.vectOfTimes.push_back(intermediate);
}
cout << "Enter hour in 24 hr(hh:mm) format of destination of
Itinerary 2 :" << endl;
cin >> hour >> min;
timeDataType destin1(hour,min);
it2.vectOfTimes.push_back(destin1);
cout << "Itinerary 1" << endl;
it1.print();
it1.printVectOfTimes();
cout << "Itinerary 2" << endl;
it2.print();
it2.printVectOfTimes();
// adding itinerary using overloaded operator +
Itinerary<string> it3;
it3 = it1 + it2;
cout << "Printing Itinerary 3" << endl;
it3.print();
return 0;
}
output attached:
Thanks,
kindly upvote.