=========================preview======================
(comp152H)[2010](s)midterm~1712^_10145.pdf
Back to COMP152H Login to download
======================================================
COMP152H Object Oriented Programming and Data Structures
Spring Semseter 2010
Midterm Exam
March 18, 2010, 3:00-4:20pm in Room 3598
Instructor: Chi Keung Tang

This is a CLOSED-BOOK-CLOSED-NOTES exam consisting of .ve (5) questions. Write your answer in the answer booklet provided.
1. OO concepts (5 points)
(a)
What is polymorphism in object-oriented programming?

(b)
Express in less than 30 words how the following concepts are related: dynamic polymorphism, dynamic binding, virtual functions, overriding, static polymor-phism, template.


2. Order of Construction and Destruction (10 points) What is the output of the following program?
#include <iostream>
using namespace std;
class Watch {
public:

Watch() { cout << "W" << endl;}
~Watch() { cout << "~W" << endl;}
};

class Father {

public:
Father() { cout << "F" << endl;}
~Father() { cout << "~F" << endl;}

private:
Watch w;
};

class Son : public Father {

public:
Son() { cout << "S" << endl;}
Son(const Watch& iw) : w(iw) { cout << "Sw" << endl; };
~Son() { cout << "~S" << endl;}

private:
Watch w;
};

int main ()
{

Watch watch;
cout << "son 1" << endl; Son s1;
cout << "son 2" << endl; Son s2(watch);
return 0;

}

3. Dynamic Polymorphism (10 points)
Code up square.h, circle.h, and polygon.h in order the following program can compile and run. The Polygon class should be made into an abstract base class and contain no data members. The output is also shown.
/** file: area.c **/
#include <stdio.h>
#include "polygon.h"
#include "circle.h"
#include "square.h"

int main() {
Polygon* p_array[2];
Circle c(3.0); // radius = 3
Square s(4.0); // width = 4
p_array[0] = &c;
p_array[1] = &s;

double sum_area = 0;

for (int i=0;i<2;i++) {
p_array[i]->print() ;
sum_area += p_array[i]->area();

}
printf("Total area = %f\n", sum_area);
return 0;

}

The output is shown below:
Circle(3.000000)
square(4.000000)
Total area = 44.274310

4. Template Container Class (10 points)
Complete the following template class de.nition. Adopt the array as the main data structure in the implementation. The output of the main program is also shown below.
#include <iostream>
using namespace std;

template <class T>
class Stack {
public:

Stack(int max_size = 10); // to be completed
bool is_empty () const { return (size == 0); }

bool is_full () const { return (size == MAX_SIZE); }
bool push (const T& item); // to be completed
T pop (); // to be completed
private:
const int MAX_SIZE;
T *array;
int size;
};

int main ()

{
Stack<int> my_stack(3);
cout << "pushing 1...\n"; my_stack.push(1);
cout << "pushing 2...\n"; my_stack.push(2);
cout << "pushing 3...\n"; my_stack.push(3);
cout << "pushing 4...\n"; my_stack.push(4);

cout << "popping " << my_stack.pop() << endl;

cout << "popping " << my_stack.pop() << endl;

cout << "popping " << my_stack.pop() << endl;