=========================preview======================
(COMP2012H)[2013](s)midterm~=v0vd57a^_74767.pdf
Back to COMP2012H Login to download
======================================================
COMP2012H Object Oriented Programming and Data Structures
Spring Semester 2013
Midterm Exam Solution
March 26, 2013, 10:30-11:50am 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 40 words how the following concepts are related: dynamic binding, virtual functions, overriding, static binding, operator overloading, STL.


2. Template and operator overloading (10 points)
You are given the following class template speci.cation and a main program where its sample output is also given. Your tasks are
(a)
implement the four member functions; use member initializer as much as possible when de.ning constructors

(b)
overload operator+ as a member function; operator<< and the pre.x operator++ as non-member function; you can assume the types to be instantiated T1 and T2 are primitive types (one example is shown in the main program) but no friend function is allowed.


You must not implement functions other than the above to make the main program run. You can de.ne all member functions within class template Tuple (which is probably easier with less syntax to bother), or outside of the class template.
template <typename T1, typename T2>
class Tuple {
public:

Tuple ();
Tuple (const T1& t1, const T2& t2);
T1 get_ele1() const;
T2 get_ele2() const;
void set_ele(T1 v1, T2 v2);

private:
T1 ele1;
T2 ele2;

};

int main()

{
Tuple<int,double> t1 (2, 4.5);
Tuple<int,double> t2 (4, 3.8);

Tuple<int,double> t3 = t1 + t2;
cout << t3 << endl;
++t3;
cout << t3 << endl;
return 0;

}
// output is
// (6, 8.3)
// (7, 9.3)

3. STL and iterators (5 points)
Complete the following program using STL algorithms copy and sort and iterators. A total of only three statements is needed.
#include <stdlib.h>
#include <vector.h>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

typedef istream_iterator_char; // complete the declaration

int main() {
vector<char> v;
istream_iterator_char start (cin);
istream_iterator_char end;
back_insert_iterator<vector<char> > dest (v);

// use stls copy to read characters from cin and copy them to v
// use stls sort to sort v
// use stls to copy the sorted v to cout, each character followed by a space

}

4. Order of Construction and Destruction (5 points)
What is the output of running the following program?
#include <iostream>
using namespace std;

class Bulb {

public:
Bulb() { cout << "B" << endl;}
~Bulb() { cout << "~B" << endl;}

};

class Lamp {
Bulb bulb;

public:
Lamp() { cout << "l" << endl;}
~Lamp() { cout << "~l" << endl;}

};

class Room {

public:
Room() { cout << "R" << endl;}
~Room() { cout << "~R" << endl;}

private:
Lamp l;
};

class Living_Room : p