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

Please solve in C++ only class is not templated You need to write a class called...
Please solve in C++ only class is not templated You need to write a class called LinkedList that implements the following List operations: public void add(int index, Object item); // adds an item to the list at the given index, that index may be at start, end or after or before the // specific element 2.public void remove(int index); // removes the item from the list that has the given index 3.public void remove(Object item); // finds the item from...
(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...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to create a rectangle with user-specified height and width. 4. Method getArea() that returns...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Method getArea() that returns the area. Method...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT