[ answers in web view ]
Name: _______________________________
Student ID: _______________________________

Total points: 60
  1. What do public, private, and protected mean as C++ access modifiers? [3]
    private
    only methods in this class can access
    protected
    also subclasses can access
    public
    anyone can access



  2. Explain and contrast method overloading with method overriding. [3]
    overloading
    multiple versions of the same method (same name) but with different number/types of parameters.
    overriding
    a method defined in a subclass overrides a method of the same name in the superclass.




  3. Describe and contrast eight of the C++ primitive types. [5]
    bool
    boolean type, just true and false
    char
    a (ASCII) character, generally 1 byte
    short
    an integer with shorter range (usually 2 bytes)
    int
    an integer (usually 4 bytes)
    long int
    an integer with greater range (usually 8 bytes)
    float
    a real value (usually 4 bytes)
    double
    a real value with greater precision (usually 8 bytes)
    long double
    a real value with even greater precision (usually 16 bytes)
    There are also signed and unsigned versions of these.






  4. What is an abstract superclass? What would you use one for? Give an example of such a situation (no code required, just describe the situation). How do you declare a class to be abstract? [4]
    An abstract class is not meant to be instantiated directly. An abstract superclass can be used as a way of organizing its subclasses under one category. For example, Dog, Cat, and Cow all are within the Mammal category. It makes sense to have specific instances of Dog, Cat, and Cow, but not of Mammal. Mammal is the abstract superclass. In C++, a class is abstract if it has at least one pure virtual function: this is a virtual method that has no body, only = 0. Concrete subclasses must override all pure virtual functions and provide bodies to them.






  5. What is the role of the C preprocessor? (What do we most commonly use it for?) Describe at least three preprocessor directives we know about. [4]
    The preprocessor does text manipulation on source files; its output gets sent to the compiler. The preprocessor can read in contents of other files into the current file (#include), or define text macros to be used in the code (#define, handy for constants), or optionally skip over lines in the source file (#if and #ifdef) (conditional compilation).





  6. Tell me everything you know about namespaces in C++. [4]
    Namespaces are containers for names like classes, variables, and functions. You can create a namespace just like you would a class: namespace MyNameSpace { } creates a namespace called MyNameSpace, and everything declared inside the block belongs to that namespace. Items inside a namespace may be accessed by prepending the namespace and '::'. Items may also be brought into the current scope by using the item. The whole namespace may be brought into the current scope by using namespace. Every file has a default, anonymous/unnamed namespace. Items declared in this namespace are accessible only within that file.






  7. Sketch a UML class diagram and code simple C++ classes that formalize the following set of statements: [8]

    class Date { int year, month, day; }
    class Person { Date birthDate; string name; }
    class Student : public Person { float GPA; }
    class Employee : public Person { }
    







    On a separate sheet of paper:

  8. Define your own exception class and write a short but complete C++ program that throws and catches an instance of that class.[5]
    #include 
    using namespace std;
    
    class Badness {
      private:
        int level;
      public:
        Badness(int l=0) : level(l) {};
        int howbad() { return level; }
    };
    
    int main() {
      try {
        throw Badness(100);
      } catch( Badness& b ) {
        cout << "caught Badness of level " << b.howbad() << endl;
      }
    }
    
  9. A complex number consists of a real part and an imag (imaginary) part (both floats). Write a C++ class Complex for complex numbers, including a constructor that takes 0, 1, or 2 arguments (use a constructor initializer list), and overloading the operators + and -. [6]
    (See file complex.cpp)

  10. Consider a student enrolment database like what we have at TWU: it holds students' personal information, transcripts, current enrolment, advising notes, etc. Design a use-case chart for such a system. (You have a fair amount of freedom in how to design the system.) [6]

  11. Write a complete C++ program that reads in lines of text from the file 'input.txt' and outputs the text both on the console and to the file 'output.txt'. There is no user interactivity in this program. (This is the basis of the Unix utility, tee.) [6]
    (See file tee.cpp)
  12. Write a C++ function that creates and returns a C++ string consisting of the 127 ASCII characters in order (skip the null character). Pay attention to efficiency -- don't force the C++ string library to keep reallocating space for the string.[6]
    (See file ascii.cpp)