CS330 Spring 2004 Lecture 32
George Tzanetakis
Review for Final
Here are some questons. I will try to go over them in class.
1) Show the calling sequence of
constructors/destructors when foo is called (for class A use A to
signify the constructor and ~A the destructor)
for the following C++ code:
class A
{
public:
A() { cout << "A" << endl; }
~A() { cout << "~A" << endl; }
};
class B
{
private:
A a;
public:
B() { cout << "B" << endl;}
~B(){ cout << "~B" << endl;}
};
class C: public A
{
public :
C() { cout << "C" << endl;}
~C() { cout << "~C" << endl;}
};
void foo()
{
A a;
B b;
C c;
}
...
....
...
foo();
...
...
2) A CSC330 student emails
you the following code complaining that it core dumps
when printRooms is called. What's wrong with the following code ?
Explain in detail
and show how the problem can be solved:
class Game{
private:
map<string, Room *> rooms;
....
....
};
void Game::addRoom(string name)
{
Room newRoom(name);
rooms[name] = &newroom;
}
void Game::printRooms()
{
map<string, Room*>::iterator ri;
for (ri = rooms.begin(); ri != rooms.end(); ++ri)
cout << rooms->second
<< endl;
}
3) A print procedure is a good
candidate for a dynamically bound method why ?
4) (Louden pg. 442-444) What
will the output of main be for version 1 and version 2.
class A
{
public:
void p() {cout << "A::p\n";}
virtual void q() { cout << "A::q\n";}
void f()
{
p();
q();
}
};
class B: public A
{
public:
void p() { cout << "B::p\n";}
void q() { cout << "B::q\n";}
};
int main() // version 1
{
A a;
B b;
a.f();
b.f();
a = b;
a.f();
}
int main() // version 2
{
A* a = new A;
B* b = new B;
a->f();
b->f();
a = b;
a->f();
}