HTW Berlin Medieninformatik

HTW Berlin
Fachbereich 4
Internationaler Studiengang
Internationale Medieninformatik (Bachelor)
Info 2: Informatik II
Winter Term 2010/11


Exercise 15: Pointers and Classes, the C++ Way

Lab exercises:

  1. Write a method in C++ void removeSpaces (char* s) that removes all blanks in a char array s. For example, "a bb ccc d" will be changed "to abbcccd". Use pointers! Is there a difference in calling this with a string or a character array? Now overload removeSpaces and make it take an array of char arrays, and remove the spaces out of all elements of the array, using pointers.
  2. Write a class Format that formats numbers right-justified and with a specific number of decimal places. How will you handle numbers less than 1, but greater than 0? Negative numbers? For example:

    Format f(12,3); // Output 12 characters wide, 3 decimal places

    // Using f
    cout << f.toString(789.90687) << endl;
    cout << f.toString(-123456789.45907) << endl;

    The result of the first line should be
         789.907
    and the second line (note that the number is too big!):
    -123456789.459
  3. Write a good test harness to test your Format code.
  4. Extend Format to a class Currency that outputs a currency symbol with the number. Note that they can either be before or after the number - how will you determine this? Write another test harness for the class Currency.
  5. (For the bored) Extend Format to output number in scientific notation.


Your report is due by 10.2.2011, 23.00!


Copyright 2010 Prof. Dr. Debora Weber-Wulff
Questions or comments: <weberwu@htw-berlin.de>
Some rights reserved. - Copyright and Warranty