DM560, Introduction to Programming in C++

Sheet 6

Task 1

Implement the different versions of the class Date saw in class and in the text book and make them to work. For each version define a Date called today initialized to June 25, 1978. Then, define a Date called tomorrow and give it a value by copying today into it and increasing its day by one using add_day(). Finally, output today and tomorrow using a << defined as in paragraph 9.8 of the text book. Your check for a valid date may be very simple. Feel free to ignore leap years. However, don’t accept a month that is not in the $[1,12]$ range or day of the month that is not in the $[1,31]$ range. Test each version with at least one invalid date (e.g., 2004, 13, –5).

You find the files of the final version described in paragraph 9.8 here.

Test the following lines:

Date d1; 
Date d2=d1; 

is the copy constructor called or the overloaded operator =?

Task 2

Graphs are mathematical structures used to model several real-life applications, for example, social networks, electric circuits, pipeline networks, lattices. In this task you are asked to write a program that defines:

  • a struct node containing the information on the name and the coordinates of a node;

  • a struct edge containing information on the tail (from node) and head (to node) of an edge;

  • a class Graph containing a vector of node and a vector of edge.

Make sure you can initialize, copy and print correctly the graph. In particular define the operator << to print the graph.

Finally, implement a reader that opens a given file and reads a graph in DIMACS format. The file marco10.dimacs is an example. Try to understand the encoding used and make sure the reader can parse such file. Include all error checks described in Chapter 10. Print the graph read and make sure it is correct.