Exercise 1:

Submit your solutions (source code) to the questions below by email to your instructor and TA(s) by Monday, October 17th (16:30).

Question 1: A first C++ program (30 points).

Create a file named "hello_main_q1.cpp" and type the following code in it:

// hello_main_q1.cpp
#include <iostream> // stream declarations

int main() 
{
  int num = 1;
  std::cout << "A " << num << "st C++ program" << std::endl;
}

Some comments:

  1. #include <iostream> is used to import the io stream function and class declarations like: "<<", "cout" and "endl"
  2. cout corresponds to the standard output (your terminal)
  3. endl adds an end of line character ("\n") to the created string
  4. the operator "<<" is handling arguments to the cout object. These arguments will be printed out from left to right

Compile the program by typing on your shell ("$>" is the shell prompt):

$> g++ hello_main_q1.cpp -o hello_main_q1

Question 2: Separate compilation (30 points).

Create a file "hello_main_q2.cpp" and rewrite the main function as follow:

// hello_main_q2.cpp
#include <iostream>
#include "display.h"

int main()
{
  int num = 1;
  display(num);
}

Now create the files "display.h" and "display.cpp".

  1. "display.h" will contain the declaration of the function display: void display(int a); Do not forget the conditional directives to prevent multiple inclusion of the header.
  2. "display.cpp" will contain the implementation of the function display(). The function display() should produce the same result as the result obtained from the program hello_main_q1

Now compile all the files corresponding to this question to produce a binary named "hello_main_q2". Results of "hello_main_q2" and "hello_main_q1" should be identical.


Question 3: Date class (40 points).

Create files: "Date.h", "Date.cpp" and "test_date.cpp". In the file "Date.h", type the definition of the class Date seen in today's lecture (in the slides on separate compilation). Do not forget to add conditional directives to prevent multiple inclusion of the header "Date.h". In the file "Date.cpp", type the implementation of the class Date as seen in today's lecture. In the file "test_date.cpp", type the code for the main function testing our class Date as seen in today's lecture. Compile these files to produce a binary named: "test_date" and run it.