Software Design (C++) - Exercises II  (1 - 5),   13 - 14 Nov, 2014   


  1. Make a C++ "Hello world" program to compile and run. Its purpose is to help you to familiarize with your compiler and program development environment (IDE or command line tools). Add some input and output operations to the program. Make the program loop around, reading some strings and numbers, and printing out values created from them. Devise a way to exit from the program.
     
    After you get the program to work, make some purposeful mistakes to see how the development tools respond. For example, forget to include the header, forget to terminate a string, write a badly formatted real literal, misspell some keyword, omit a semicolon or a brace ({ or }), etc. Inspect the error diagnostics from the point of view of a programmer. Give examples of some good (helpful or informative) diagnostic messages. Give examples of perhaps not so good (obscure, misleading, or superfluous) diagnostic messages.

  2. Write a program that performs as a very simple calculator. The program takes an operation followed by a series of operands, and calculates and outputs the result. Each expression is terminated by a semicolon (;). The program is stopped by a "quit" command. For example, input could be
       +  100  3.13  4;  
       * 5  537.4;  
       - 10 1 2 3 4;
       quit
    
    Read the operands as values of type double. Implement calculation for operations + and * with their obvious meanings. Note that the minus operation (-) subtracts each consecutive number from the current result, cumulatively. (E.g., the last expression above gives the result zero.) Similar semantics applies for division (/). Compile and run your program.
    Hint. We can use the stream operation clear() to reset any IO error flags so that reading can again continue.

  3. Write two functions that reverse the order of elements in a std::vector. The first reverse function should produce a new vector with the reversed sequence, leaving its original vector unchanged.
    The second function should reverse the elements of its vector without using any other vectors (hint: do in-place swaps of elements). Test your functions with both int values and string values. Hint: You can use the type vector<T>, and provide for the type T a convenient typedef that precedes the function definition.

    1. Define pre-condition and post-condition. What would be the pre- and post-condition of a pop of a stack?
    2. 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.
    3. When would you not check a pre-condition? When would you not check a post-condition?

  4. Explain the following items concerning general C++ programming:
     
    1. What is a reference type in C++? What is the difference between pass-by-value and pass-by-reference? What is the difference between pass-by-reference and pass-by-const-reference?
    2. Would you ever define a function that has a vector<double> as its by-value parameter? What would be the alternatives for using a value parameter?
    3. Why should you avoid any using statements in a header? Hint: Consider why and how header files are being used - and shared in large software systems.
    4. When should operator overloading be used in a C++ program? Give a list of operators that you might want to overload in a C++ program - each with an explanation of a reason.
    5. Explain what is meant by a scoped and unscoped enumeration type, respectively. Which one should we prefer? Why?