Final Examination

This is a take-home exam. You may work with other people in the class, AS LONG AS YOU INDICATE THEIR NAMES ON THIS PAPER. If you work with someone else without this indication, you may be given a zero for the exam.

1

Below is a fragment of meaningless C++ source code that contains 14 syntactic errors. Locate them (14 points), and correct them (7 points).

Class a0000 {
  int real;
public
  void a0000();
  long 32AA(int x, y) { return x ++ y }
  int& fff(int) { return real; }
}
static int & switch(float f; float g)
  cout << (Here is a message << endl;
  for (int i = 3, i < f; i++) {
      g += - [f + g) / i;
      a0000* _A_;
      return _A_->fff(3};
  }
}

2

What does the following program print? If a value is unknown, just write "??". Explain why the two calls to function g produces different results in each case. (20 points)

#include <iostream.h>

class X {
    int x;
public:
    X() { x = 10; }
    void f() { cout << "f called " << x <<  endl; }
};

class Y : public X {
    int y;
public:
    X() { y = 20; }
    void g() { cout << "g called" << y << endl; }
};

int main() {
   X* a = new X;
   Y* b = new Y;
   ((Y*)a)->g();
   a = b;
   ((Y*)a)->g();
}

3

In the program below, state whether each of the function calls in the main program are valid or invalid (i.e. will not compile) and, if it is invalid, state why. (8 points) If the invalid call is removed (or more if there are more invalid calls), what will the program print? Again, explain why the results are obtained. (13 points)

#include <iostream.h>

class X { int x; public: X() : x(0) {} };
class Y { int y; public: Y() : y(0) {} };

class base {
public:
  void f1(X) { cout << "f1(X) in base" << endl; }
  void f1(Y) { cout << "f1(Y) in base" << endl; }
  void f2(X) { cout << "f2(X) in base" << endl; }
  void f3(X) { cout << "f3(X) in base" << endl; }
  void f3(Y) { cout << "f3(Y) in base" << endl; }
  void f4(X) { cout << "f4(X) in base" << endl; }
};

class derived : public base {
public:
  void f1(X) { cout << "f1(X) in derived" << endl; }
  void f4(Y) { cout << "f4(Y) in derived" << endl; }
};

int main() {
  derived d;
  base b;

  d.f1(X());
  d.f1(Y());
  d.f2(X());
  d.f3(X());
  d.f3(Y());
  d.f4(Y());

  b.f1(X());
  b.f3(X());
}

4

What does the program below print? You need to provide an explanation for every assignment and function call in the main program by drawing diagrams of the objects involved and how functions are called. (16 points)

#include <iostream.h>

class MusicalInstrument {
protected:
  enum Method { BLOW, HIT, SCRAPE, PLUCK };
private:
  Method method;
public:
    MusicalInstrument(Method m) : method(m) {}
  virtual void Tune()=0;
  char* GetMethod() {
	 switch (method) {
		case BLOW: return "Blow"; break;
		case HIT: return "Hit"; break;
		case SCRAPE: return "Scrape"; break;
		case PLUCK: return "Pluck"; break;
		default : return "?????";
    }
  }
};
class Wind : public MusicalInstrument {
private:
public:
    Wind() : MusicalInstrument(BLOW) {}
  void Tune() { cout << GetMethod() << " a concert B flat" << endl; }
};
class Percussion : public MusicalInstrument {
private:
public:
    Percussion() : MusicalInstrument(HIT) {}
  void Tune() { cout << GetMethod() << " it!" << endl; }
};
class Tuned : public Percussion {
public:
  void Tune() { cout << GetMethod() << " the starting pitch" << endl; }
};
class Untuned : public Percussion {
public:
  void Tune() { cout << "Cannot tune untuned percussion" << endl; }
};
class String : public MusicalInstrument {
private:
public:
	 String(Method m) : MusicalInstrument(m) {}
	 void Tune() { cout << "Vibrate the string somehow" << endl; }
};
class Bowed : public String {
public:
    Bowed() : String(SCRAPE) {}
  void Tune() { cout << GetMethod() << " a concert A" << endl; }
};
class Plucked : public String {
public:
    Plucked() : String(PLUCK) {}
  void Tune() { cout << GetMethod() << " a concert A" << endl; }
};

int main() {
  MusicalInstrument* a;
  String *b;
  Percussion *c;
  Percussion d;
  Tuned e;
  a = new Wind;		// explain this assignment
  a->Tune();		// explain this call
  b = new Bowed;		// explain this assignment
  b->Tune();		// explain this call
  c = new Untuned;	// explain this assignment
  c->Tune();		// explain this call
  d.Tune();			// explain this call
  e.Tune();			// explain this call
}

5

The program below contains a serious error that can cause the program to crash during execution. What is it (12 points) and how can it be fixed (10 points)? Tracing the code in the main function should help you.

#include <iostream.h>

class AA {
private:
  int id;
  int* a;
public:
    AA(int size, int i) : a(new int[size]), id(i)
		{ cout << "Creating a" << endl; }
   ~AA() { cout << "Deleting AA " << id << endl; delete [] a; }
};
class BB {
private:
  int id;
  AA aa;
  AA *paa;
public:
    BB(int i) : aa(10, i), id(i) { paa = &aa; }
   ~BB() { cout << "Deleting aa " << id << endl; delete paa; }
};
int main () {
  BB* pbb = new BB(1);
  delete pbb;
  BB bb(2);
}