Software Design (C++) - Exercises IV  (1 - 4),   27 - 28 Nov , 2014   


  1. (a) Explain the benefits of using distinct public and private parts in a class definition. When would we define private member functions for a class? Would we ever define public data members for a class? Why/why not? Give illustrative examples.

    (b) Explain the benefits of making the public interface of a class be as small as possible. When should functions be declared as member functions inside the class definition, and when should they be declared outside the class as separate helper functions (possibly friends - find out what the friend keyword means)? Give illustrative examples.

  2. Explain the reason for declaring a constructor that takes one argument as explicit (using the explicit keyword). Use the text book or on-line resources to find the answer. Write some code that demonstrates what may happen if such a constructor is not declared explicit.

  3. Write a very simple class (let's call it by name 'X') that has just one data member of a primitive type (like char or int). The class must have the following functions defined: In each function, add a print statement that prints a log message to std::cerr that tells which function was executed and on which object (e.g. use the data member of the object or the this pointer to distinguish between objects).
    Now, write a test program that uses the class. The program should have one global variable of type X (defined outside of the main() function). In the main() function, perform at least the following operations on objects of type X: Observe the trace of the log messages printed to std::cerr and map those messages to the execution of the statements of your progam. That is, explain which statements (lines) in your program caused the printing of each message.

  4. Add to your class written in the previous exercise a move constructor and a move assignment operator (don't forget the print statements to trace the calls to the functions). Write also two other functions outside the class: Add to your test program calls that execute both of the new move functions and the two other functions (look at the code examples in the course slides). Build and execute your program and observe the trace of log messages with two different configurations: Explain the differences in the traces.