lunduniversity.lu.se

Computer Science

Faculty of Engineering, LTH

Denna sida på svenska This page in English

Exercise 2

About   Files   Solutions  
Memory, strings, static members, object lifetimes. exercise2.tar.gz  sol-exercise2.tar.gz 
  1. Run the program memerrortest.cc -- it illustrates what happens upon memory-related execution errors. Some errors are detected, some are not. In a larger program, the undetected errors would probably lead to catastrophic errors much later during the execution. Run again, under control of Valgrind (see lab 1).

  2. Morse code is a method of transmitting text information as a series of short and long signals ('dots' and 'dashes'). For example, the letter 'a' is coded as '.–', 'b' as '–...', and so on. We ignore spaces between words. Implement the class MorseCode, test with morsetest.cc. The file morsecode.def contains the morse code definitions of the letters a–z. Characters outside this range should be ignored. Hint: in decode(), use an istringstream to read morse codes from the code string.

  3. Editors for program text usually help with matching of parentheses: when you type a ), ] or } the editor highlights the corresponding left parenthesis. Example (... is a sequence of characters except parentheses):
        ...(...(...[...]...)...)...{...}...
                   |---|           |---|
               |-----------|
           |-------------------|
    Implement the function find_left_par in the class Editor, test with editortest.cc.

  4. Classes can have static members, just like in Java. One example is seen in count_objs.cc, which uses a static member variable to count how many objects have been created of a class. Add the static member function
    void Foo::print_count(), so that the program compiles and running tit prints

    Created Foo objects = 1
    Created Foo objects = 2
    Created Foo objects = 3
    Created Foo objects = 3

    Add functionality to also keep track of how many objects are currently alive in the program. Change the class Foo so that it also counts the number of currently allocated objects. The output of the example program should be something like:

    Alive Foo objects: 1 / 1 created
    Alive Foo objects: 2 / 2 created
    Alive Foo objects: 1 / 3 created
    Alive Foo objects: 0 / 3 created

  5. Study the program integer_conversion.cc.
    Before running it, what do you think the output will be?
    Then run the program and see.
    Is the output what you expected?

    Then, compile with warnings for implicit conversions enabled (-Wconversion).
    Do you get a warning? Do you understand it?
    If you have access to both g++ and clang++ (the school computers do), or some other C++ compiler, compare the warnings they give.