CMPT 212
Object-Oriented Applications Design in C++

Spring 1998

Midterm Answers


  1. What are four advantages and four disadvantages of separate compilation?

    • Advantages:

      1. Parts of the program can be written in different languages.

      2. If changes are made to the program, we only need to recompile the source files which contain the changes, not the whole program.

      3. Different programmers can be working on different parts of the program at the same time.

      4. It is easier to write commonly used modules and use them in different programs.

    • Disadvantages:

      1. Difficulty in deciding how to split up the program into modules.

      2. If a subroutine X in one source file calls a subroutine Y contained in another source file, how do we ensure that
        1. Y exists,
        2. X is calling Y with the correct number of parameters,
        3. X is calling Y with the correct parameter types,
        4. X is expecting the correct type of value returned by Y.

      3. If a subroutine X in one source file uses a variable Y contained in another source file, how do we ensure that
        1. Y exists,
        2. X knows the correct type of Y.

      4. How do we prevent the same subroutine or variable from being defined in different source files? Or should we prevent this?

      5. How can a subroutine in one source file call a subroutine in another source file if they are written in different programming languages?


  2. What are the differences between passing a parameter by value and passing a parameter by reference?

    • When a parameter is passed by value, the function gets its own copy of the parameter value. If the function makes changes to its copy of the parameter, the original parameter is unchanged.

    • When a parameter is passed by reference, the function has access to the actual parameter that was passed. If the function makes changes to the parameter, the original parameter is changed, since the function is accessing the original parameter.


  3. What are two reasons why a parameter would be passed by reference?

    • We want the function to be able to change the value of a parameter.

    • It the parameter is a large item (such as a struct), passing by reference is much faster.


  4. Suppose we have this class definition:
        class Person {
          public:
             Person();
             ~Person();
             friend ostream & operator<<(ostream &, Person &);
          private:
             char name[20];
             int  age;
             int  weight;
       };
              
    Write a definition of the output operator << for the class Person that will produce output like that shown in the sample output below.
       Name: Henry Ford
       Age: 32
       Weight: 185
       

                      
       ostream & operator<<(ostream & os, Person & p) {
          os << "Name: "   << p.name   << endl
             << "Age: "    << p.age    << endl
             << "Weight: " << p.weight << endl;
          return os;
       }                
                      


  5. What is the purpose of function prototypes?

    To allow the compiler to check, in one pass, that each function is being called with the correct number of parameters, with the correct parameter types, and with the correct return value type being expected.


  6. What is the output of this program?
         #include <iostream.h>
         
         class elephant {
            public:
               elephant();
               elephant(const int i);
               elephant(const elephant & c);
               ~elephant();
               elephant & operator=(const elephant & c);
            private:
               int size;
            };
         
         elephant::elephant() {
            size = 3;
            cout << "default constructor: size = " << size << endl;
         }
         
         elephant::elephant(const int i) {
            size = i;
            cout << "int constructor: size = " << size << endl;
         }
         
         elephant::elephant(const elephant & c) {
            size = c.size;
            cout << "elephant constructor: size = " << size << endl;
         }
         
         elephant::~elephant() {
            cout << "destructor: size = " << size << endl;
         }
         
         elephant & elephant::operator=(const elephant & c) {
            size = c.size;
            cout << "assignment operator: size = " << size << endl;
            return *this;
         }
         
         int main() {
           elephant a;
           elephant b(7);
           elephant c = b;
           elephant * d = new elephant(4);
         
           delete d;
           c = a;
         
           return 0;
         }
             

                      
       default constructor: size = 3
       int constructor: size = 7
       elephant constructor: size = 7
       int constructor: size = 4
       destructor: size = 4
       assignment operator: size = 3
       destructor: size = 3
       destructor: size = 7
       destructor: size = 3
                      


  7. What is the output of this program?
       #include <iostream.h>
         
       class myObject {
          public:
             myObject(int i, int j) { x = i; y = j; }
             void SetX(int i) { x = i; }
             void SetY(int i) { y = i; }
             static void ShowX() { cout << "x = " << x << endl; }
             void ShowY() { cout << "y = " << y << endl; }
             static void increment() { x++; }
          private:
             static int x;
             int y;
          };
    
       int myObject::x = 5;
    
       int main() {
          static myObject a(3, 4);
          myObject b(7, 8);
    
          a.ShowX();
          a.ShowY();
          b.ShowX();
          b.ShowY();
    
          a.SetX(1);
          b.increment();
          myObject::increment();
    
          a.ShowX();
          a.ShowY();
          b.ShowX();
          b.ShowY();
    
          return 0;
       }        
       

                      
       x = 7
       y = 4
       x = 7
       y = 8
       x = 3
       y = 4
       x = 3
       y = 8
    
                      


  8. Given that you need to do this assignment:
    myPtr->field = 16;
    which const in the following declaration of myPtr would have to be removed, the first one or the second one?
    const MyType * const myPtr;

    the first one


  9. What is the scope and lifetime these items: a, b, c, d, e, and the char pointed to by f?
         #include <stdlib.h>
         #include <iostream.h>
         
         int a = 4;
         static float b = 2.73;
         
         int evaluate(char c = 'X') {
            static bool d = true;
            int e = 0;
         
            if (c != 'X') {
               e++;
               d = (e < 5);
               }
         
            return e;
         }
         
         int main() {
            char * f = NULL;
         
            f = new char('H');
            a = evaluate(*f);
            delete f;
         
            return 0;
         }
       

    Scope Lifetime
    a global static
    b module static
    c local automatic
    d local static
    e local automatic
    f local dynamic


  10. What is the output of this program?
       #include <iostream.h>
         
       int main() {
          int   a = 3;
          int & b = a;
          int * c = &a;
          int   d = 10;
    
          cout << b << endl;
          *c = 5;
          cout << b << endl;
          c = &d;
          a++;
          b++;
          (*c)++;
          d++;
          cout << a    << endl
               << b    << endl
               << (*c) << endl
               << d;
    
          return 0;
       }
       

                      
       3
       5
       7
       7
       12
       12
    
                      


  11. What is the output of this program?
       #include <iostream.h>
         
         char test(int i) {
            cout << "one" << endl;
            return 'B';
         }
         
         int test(int i, float f) {
            cout << "two" << endl;
            return 8;
         }
         
         float test(char c, int i = 3) {
            cout << "three" << endl;
            return 3.14;
         }
         
         int test(float f) {
            cout << "four" << endl;
            return 7;
         }
         
         
         int main() {
           int   x = 1;
           float y = 2.0;
           char  z = 'A';
         
           cout << test(test(y))    << endl;
           cout << test(test(x))    << endl;
           cout << test(test(x, y)) << endl;
           cout << test(test(z, x)) << endl;
           cout << test(test(z))    << endl;
         
           return 0;
         }
       

                      
       four
       one
       B
       one
       three
       3.14
       two
       one
       B
       three
       four
       7
       three
       four
       7
    
                      




Return to CMPT 212 home page



This page is maintained by simpson@cs.sfu.ca. Last updated on 4 Mar 1998.