Programming In C++
Spring 2000
CSCI 2200 CRN: 90349


Test #2 Topics


Sample Questions

(Sample answers are at the end of this page)

Expect questions related to the sample code we've gone over in class! Below are a few sample questions based on code very similar to the examples we discussed. If you completely understand the example code from class - you will do very well on the test!

  1. What is the output of the following program:
    #include <iostream.h>
    
    class blah {
    
    public:
      static int a;	
      int b;
    
    
      blah(int x) {
        b=x;
        a=b+1;
      }
    
    };
    
    
    int blah::a = 0;
    
    
    int main(void) {
    
      blah b1(5);
      blah b2(12);
      cout << "b1.a is " << b1.a << endl;
      cout << "b1.b is " << b1.b << endl;
      cout << "b2.a is " << b2.a << endl;
      cout << "b2.b is " << b2.b << endl;
    
    }
    
    

  2. What is the output of the following program, and justify (explain) each line of output:
    #include <iostream.h>
    
    // Class ol_int is like an int but not quite!
    
    class ol_int {
    private:
      int x;
    
    public:
    
    
      // default constructor - value is 0
      ol_int() {
        x=0;
      }
    
    
      // constructor from int
      ol_int(int v) {
        x=v;
      }
    
    
      // this is called when we assign an int to an ol_int
      ol_int& operator=( int i) {
        x=i;
        return(*this);
      }
    
    
      // overloaded plus operator
      // we return and int, but the compiler knows how to convert this
      // to an ol_int! (uses the constructor).
      // the return statement
    
      int operator+( ol_int &o) {
        return( x - o.x);
      }
    
      friend ostream &operator<<( ostream &, const ol_int& );
    
    
    
    };
    
    
    // Here is the definition of the overloaded << operator
    // used to send the value of an ol_int to an output stream
    
    ostream &operator<<(ostream &o, const ol_int &v) {
      o << v.x;
      return(o);
    }
    
    
    int main(void) {
    
      ol_int a=3;
      ol_int b=7;
      ol_int c,d;
    
      cout << "A is " << a << endl;
      cout << "B is " << b << endl;
    
      c = a + b;
      cout << "C is " << c << endl;
    
      d = 3 + 7;
      cout << "D is " << d << endl;
    
      ol_int e;
      cout << "E is " << e << endl;
      
    
    }
    
    
    
    

  3. Write the definition of a class named "computer" which includes the attribute "name" (the name of the computer) and a print method that prints out this name.

    Also write the definition of a class named "Macintosh" that is derived from the class "computer" (computer is the base class) and includes the attribute "color" (a string) and a method named print that prints out the computer name and color.

    Here is main function that will test these objects, it uses a pointer to treat a Macintosh object as a computer object, and calls the print method.

    int main() {
      computer *p;
      Macintosh imac("Joe's IMAC","Blue");
    
      p = &imac;
    
      p->print();
    }
    
    I want the printout of this test program to be something like:
    Name: Joe's IMAC
    Color: Blue
    
    that it, I want the color printed out even though the computer class does not have any knowledge of the color attribute. (in other words - make sure your system is polymorphic).


Answers

  1. The output would be:

    b1.a is 13
    b1.b is 5
    b2.a is 13
    b2.b is 12
    

    Since member a is a static member, all objects of type blah share a single a.

  2. The output would be:

    A is 3
    A is initialized from the integer 3, this calls the constructor that takes an int parameter.
    B is 7
    B is initialized from the integer 7, this calls the constructor that takes an int parameter.
    C is -4
    The ol_int + operator is overloaded to actually subtract!
    D is 10
    The int + operator doesn't change. The integer addition happens normally, the result is assigned to the ol_int object.
    E is 0
    The default constructor initializes the value to 0

  3. 
    
    class computer {
    private:
       char *name;
     public:
    
    
       computer(char *n) {
         name = new char[strlen(n)+1];
         strcpy(name,n);
       }
    
       ~computer() {
         delete[] name;
       }
    
    
       virtual void print() {
         cout << "Name: " << name << endl;
       }
    };
    
     
         
    
    
    
    class Macintosh: public computer {
    private:
       char *color;
     public:
    
    
       Macintosh(char *n, char *c) : computer(n) {
         color = new char[strlen(c)+1];
         strcpy(color,c);
       }
    
       ~Macintosh() {
         delete[] color;
       }
    
    
       void print() {
         computer::print();
         cout << "Color: " << color << endl;
       }
    };