Software Design (C++) - Exercises III  (1 - 4),   20 - 21 Nov, 2014   


  1. (a) Define pre-condition and post-condition. What would be the pre- and post-condition of a pop of a stack?
    (b) Now, consider the following situation. A function does some complicated calculations on its given parameters and an integer overflow occurs - due to the values of the passed parameters. Is this situation a violation of the pre-condition or the post-condition of the function? Hint: You may want to consider whose responsibility is the correctness of input data, in one hand, and the correctness of computations, on the other hand.
    (c) When would you not check a pre-condition? When would you not check a post-condition?

  2. Explain the following items concerning C++ error checking:
    (a) Find out what is the traditional assert macro of ANSI C. What is the purpose of it and how is it used? How can asserts be turned off? Why would we want to turn them off? Compare to the assert facility in Java.
    (b) What is the relation of the assert facility to pre- and post-conditions? When should we use (1) assert, and when should we (2) throw exceptions from a function? Give some examples of both.

  3. Write a Complex class, with floating-point attributes x and y, of your own. Provide some complex operators of your choice. Consider which operations are properly declared as members, and which ones are better declared as helper functions outside the class. Then write a program that reads in floating-point numbers, makes complex numbers out of pairs of numbers read, calculates new complex values from existing complex numbers, and writes out the complex numbers. Compile and try out your program.

  4. (a) What is a class invariant and what is it used for?
    (b) consider the sample class IntStack, discussed in the lecture slides. Implement the two missing function members for the class IntStack: the pop(), and the assignment operator. Use pre- and post-conditions, when appropriate, and implement a check function for the class invariant.