Question

In: Computer Science

This question is in C++ Problem 3 Write a templated C++ class to represent an itinerary....

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)


you have to add them so let's say you had

It1: A -> B1 -> B2 -> C
It2: D -> E1 -> F

It3 = It1 + It2
It3: A -> B1 -> B2 -> C -> D -> E1 -> F

Solutions

Expert Solution

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.


Related Solutions

(In C++) Write a class that will represent a card in a standard deck of playing...
(In C++) Write a class that will represent a card in a standard deck of playing cards. You will need to represent both the suit (clubs, diamonds, hearts or spades) as well as the rank (A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2) of each card. Write methods to • Initialize the deck of cards • Perform a perfect shuffle In a perfect shuffle, the deck is broken exactly in half and rearranged so that...
Write a program in which define a templated class mySort with private data members as a...
Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.
USE C++ 1. Write a class that will represent a card in a standard deck of...
USE C++ 1. Write a class that will represent a card in a standard deck of playing cards. You will need to represent both the suit (clubs, diamonds, hearts or spades) as well as the rank (A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2) of each card. Note: Use enumeration instead of strings For example: Do something like this...        enum suits        {            CLUBS,            DIAMONDS,   ...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector class to hold the data array
(Use the string class to solve the problem) Write a program (in c++) that can be...
(Use the string class to solve the problem) Write a program (in c++) that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or him”. Be sure to preserve...
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given...
DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given below. Use and modify them) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue. NOTE: A humble request, please don't just copy and paste the answer from chegg. I need more specific answer. Also I don't have much question remaining to...
The assignment is to write a class called data. A Date object is intented to represent...
The assignment is to write a class called data. A Date object is intented to represent a particuar date's month, day and year. It should be represented as an int. -- write a method called earlier_date. The method should return True or False, depending on whether or not one date is earlier than another. Keep in mind that a method is called using the "dot" syntax. Therefore, assuming that d1 and d2 are Date objects, a valid method called to...
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a...
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a member function named      int distance2origin() that calculates the distance from the (x, y, z) point to the origin (0, 0, 0) the ‘prototype’ in the class definition will be      int distance2origin() outside the class definition             int Coord :: distance2origin() {                         // your code } _______________________________________________________________________________________________________ /************************************************** * * program name: Coord02 * Author: * date due: 10/19/20 * remarks:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT