=========================preview======================
(COMP2012H)[2013](s)midterm~=v0vd57a^_33724.pdf
Back to COMP2012H Login to download
======================================================
COMP 2012H: OOP and Data Structures (Honors Track)
Spring Semester 2013 Final Exam
Instructor: Chi KeungTang
Wednesday, 22 May 2013
8:30C11:30
Room 3006


This isa CLOSED-BOOK-CLOSED-NOTES exam consisting of ten problems. Calculator policy used by SATapplies. Follow the instructions carefully. Please write legibly in the space provided. Keep the exam booklet stapled.
KEY


Problem Points your score
1 INHERITANCE 8
2 OBJECT ORIENTED PROGRAMMING 8
3 FUNCTION OBJECTS 8
4 AVLTREE 10
5 SORTING 10
6 RECURRENCES 12
7 GRAPH TRAVERSALS 10
8 HASHING 12
9 ALGORITHM DESIGN 12
10 GRAPH ALGORITHM 10
Total 100

Often in a design, you want the base class to present only an interface for its derived classes. That is, you do not want anyone to actually create an object of the base class, only to upcast to it so that its interface can be used. This is accomplished by abstract base class. The following shows a class hierarchy where Instrumentis an abstract base class.
In this particular example, there are no private data member and no concrete implementation for the member functions for Instrument.


Implement the necessary classes in Instrument.hso that the following code compiles andrun.You should make thebase class Instrument abstract. The sample output is also shown below. To make your coding easier and more readable, feel free to use the enumerated types Note, and two macros note2textand int2note, which are de.ned below.
#include <iostream>
using namespace std;

enum Note { middleC, Csharp, Cflat };

#define note2text(note) \
((note == middleC) ? "middleC" : ((note == Csharp) ? "Csharp" : "Cflat"))

#define int2note(i) \
((i%3 == 0) ? middleC : ((i%3 == 1) ? Csharp : Cflat))

#include "Instrument.h"

int main() {


Instrument* A[] = {
new Wind,
new Percussion,
new Stringed,
new Brass,
new Woodwind

};


for (int i = 0; i < 5 ; i++) {
A[i]->what();
A[i]->play(int2note(i));

}
return 0;
}

Sample output:

Wind::play middleC
Percussion::play Csharp
Stringed::play Cflat
Brass::play middleC
Woodwind::play Csharp


Solution:
class Instrument {

public:
virtual void play (Note n) const = 0;
virtual char* what () const = 0;

};

class Wind : public Instrument {

public:
void play (Note n) const { cout << "Wind::play " << note2text(n) << endl; }
char* what() const { return "Wind"; }

};

class Percussion : public Instrument {

public:
void play (Note n) const { cout << "Percussion::play " << note2text(n) << endl; }
char* what() const { return "Percussion"; }

};

class Stringed : public Instrument {
public:
void play (Note n) const { cout << "Stringed::play " << note2text(n) << endl; }

class Brass : public Wind {

public:
void play (Note n) const { cout << "Brass::play " << note2text(n) << endl; }
char* what() const { return "Brass"; }

};

class Woodwind: public Wind {

public:
void play (Note n) const { cout <<