=========================preview======================
(comp152h)[2011](s)midterm~2427^_60159.pdf
Back to COMP152H Login to download
======================================================
COMP152H Object Oriented Programming and Data Structures
Spring Semester 2011
Midterm Exam
March 22, 2011, 9:00-10:20am 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. Abstract Base Class (5 points)
The following shows the implementation of three classes derived from Animal. The main program and its output are also shown. Code up the Animal base class. Use member initializers whenever possible when you de.ne constructors.
class Cat: public Animal {
public:
Cat(std::string strName) : Animal(strName) { }
const char* Speak() { return "Meow"; }
};
class Dog: public Animal {
public:
Dog(std::string strName) : Animal(strName) { }
const char* Speak() { return "Woof"; }
};
class Cow: public Animal {
public:
Cow(std::string strName) : Animal(strName) { }
const char* Speak() { return "Moo"; }
};
int main()
{
// Animal animal; cannot declare Animal object; compilation error
Animal *animal_arr[3];
Cow cCow("Betsy"); Dog dDog("Lucky"); Cat cCat("Tom");
animal_arr[0] = &cCow; animal_arr[1] = &dDog; animal_arr[2] = &cCat;
for (int i=0; i<3; i++)
std::cout << animal_arr[i]->GetName() << " says "
<< animal_arr[i]->Speak() << "... ";
}
The output is
Betsy says Moo... Lucky says Woof... Tom says Meow...
3. Inheritance (10 points) You are given toy.h and a main program in the following. Your task is to code up toy.cpp so that the main program runs and produces the output on the orders of construction and destruction. Note that a dynamic array is allocated when an object is instantiated in both the base class and derived class. Your implementation must
nothave memoryleak ordanglingpointers, andyoumust usememberinitializer when de.ning constructors as much as possible.
// toy.h
class B {
public:
B(int n = 10); // initialize a dynamic array iarr of size n
virtual ~B(); // should deallocate memory
private:
static int bcnt;
const int MAX_iSIZE; // maximum size of iarr
int *iarr;
};
class D : public B {
public:
D(int n = 10); // initialize a dynamic array carr of size n
virtual ~D(); // should deallocate memory
private:
static int dcnt;
const int MAX_cSIZE; // maximum size of carr
char *carr;
};
// main program
int main()
{
B** b;
b = new B*[2];
for (int i=0;i<2;i++) b[i] = new D(10);
for (int i=0;i<2;i++) delete b[i];
delete b;
return 0;
}
The output is
B constructed: 1
D constructed: 1
B constructed: 2
D constructed: 2
D destructed: 2
B destructed: 2
D destructed: 1
B destructed: 1
4. Template and operator overloading (10 points)
You aregiventhefollowing classtempl