Exercise 5:

Create a directory ex5 and store in this directory all the files corresponding to this exercise.

Question 1: Overloading.

In this question, you will exercise your understanding of overloading. Please create 3 overloaded functions min() and 3 overloaded functions max(). These functions will take as argument respectively: two int, two float and two double and will return respectively: an int, a float and a double corresponding to the max (or min) of the two arguments. You can write the functions declarations in a header file named "utils.h" and the code in a file named "utils.cpp".

To test your functions, create a file "test_utils.cpp" and type (and complete) the following code:

// test_utils.cpp
// Include needed headers here

int main()
{
  int a=1, b=2;
  float af=1.5, bf=3.5;
  double ad=2.5, bd=2.2;

  std::cout << min(a,b) << " " << max(a,b) << std::endl;
  std::cout << min(af,bf) << " " << max(af,bf) << std::endl;
  // write here the call to min(double, double) and max(double, double)
}

Question 2: Constructor.

In the following 3 questions, you will exercise your understanding of constructor, copy constructor and assignment operator. Create a file "A.h" that will contain the definition of a class A. Type and complete the following code in the file "A.h":

// A.h

#include <string>
#include <iostream>

#ifndef A_H
#define A_H

class A {
 private:
  int size;
  int* array;
  std::string name;

 public:
  // ctors:
  A() /* ... init size to 0,
       * array to null and 
       * name to "A" using an initialization list. 
       * Then in the body of the constructor, 
       * print: A(). */
  A(int sz) 
      /* ... init size to sz, 
       * array with a pointer to an array of sz int 
       * and name to "A" using an initialization list. 
       * Then in the body of the constructor, print: A(sz). */
  A(int sz, const std::string& na) 
      /* ... init size to sz, 
       * array with a ptr to an array of sz int 
       * and name to na using an initialization list. 
       * Then in the body of the constructor, print: A(sz, na). */

  // dtor:
  ~A() {
    if (array != 0) delete[] array;
    std::cout << "~A()" << std::endl;
  }
};

#endif // A_H

In order to test this class, type the following code in a file named "test_A.cpp":


// test_A.cpp
#include "A.h"

int main()
{
  /* create an instance of A named a1 using the default constructor */
  /* create an instance of A named a2 using the ctor A(int sz) */
  /* create an instance of A named a3 using the ctor A(int sz, std::string na) */
}

Question 3: Copy constructor.

Please complete the definition of the class A by adding a copy constructor. The copy constructor should carefully manage the owned data (i.e. the ptrs). In the copy constructor, please also add a line (of information) that prints: A(a). You can add the copy constructor directly in the file "A.h". In order to test your class, edit the file "test.cpp" by adding the following code in the main function:

int main() {
// ...
// here is the code from question 2
// ...

 A a4(a2);
 A a5 = a3;
}

Question 4: Assignment operator.

To complete our class A, we need to add now an assignment operator. Please edit the file "A.h" and add the code for the assignment operator. Please be careful to handle self-assignment correctly. The assignment operator should carefully manage the owned data (i.e. the ptrs). In the assignment operator, please also add a line (of information) that prints: a = b. In order to test your class, edit the file "test.cpp" from the previous question by adding the following code in the main function:

int main()
{
// ...
// here is the code from the previous questions
// ...

 A a6(5);
 a6 = a2;
 A a7(5);
 a7 = a7;
}