=========================preview======================
(COMP151)mid2002.pdf
Back to COMP151 Login to download
======================================================
Hong Kong University of Science and Technology
COMP151: Object Oriented Programming

Spring 2002, Midterm Examination

Monday 11 March, 7:15 C9.00 PM




Student Name:

Student ID: Lab Section/TA Name:





Instructions:
1. This is a closed-book, closed-notes examination.

2. Check that you have all 13 pages (including this cover page).

3. Write your name, student ID, lab section/TA name on this page.

4. Answer all questions in the space provided. Rough work should be done on the back pages.




Question
Score

1


2


3


4


5


6


7


Total




Question 1 : You are given the definition of the class Weekend. Write the implementation of the default constructor and the destructor of class Weekend. The default constructor initializes the 2-dimentional array days to the values: Saturday and Sunday. The destructors deallocates the memory allocated for days.
class Weekend
{
private:

const char** days; // Dynamically allocated array
int num;
public:
Weekend(){ //Default constructor
------------//to be filled-----------------
~Weekend(){ //Destructor
------------//to be filled-----------------

void print() const {for (int i=0;i<2;++i) cout<<days[i]<<endl;}
};

The following program demonstrates the use of class Weekend.

void main() {
Weekend x;
x.print();
}

The output of the main program should be:

OUTPUT
Saturday
Sunday

Write the implementation of Weekend::Weekend()


Write the implementation of Weekend::~Weekend()
Question 2 : You are given the class Date and the class Person:
class Date{
int y,m,d;
public:
Date(int x, int y, int z) {y=x; m=y; d=z;}
};

class Person{
const Date birthday;
public:
Person(int x, int y,int z){
------------//to be filled-----------------
};



Write the implementation of Person::Person(int x, int y, int z):



Question 3 (20 points) : You are given the class Movie and two functions f1() and f2 ().

class Movie {

char * name; //the title of the movie
int size; //the number of characters of the title

public:
//default constructor
Movie() {
cout<<"Default Constructor"<<endl;
------------//to be filled-----------------

//copy constructor
Movie(const Movie &c) {
cout<<"Copy Constructor"<<endl;
------------//to be filled-----------------


//conversion constructor
Movie(const char * x) {
cout<<"Conversion Constructor"<<endl;
------------//to be filled-----------------

~Movie() {delete [ ] name;}
void print() {cout<<name<<endl;}
//transforms to uppercase letters the name of the movie
void cap() {for(int i=0; i<size; i++) name[i]=toupper(name[i]);}
//transforms to lowercase letters the name of the movie
void low