| Date Assigned: | Wed Nov-8 |
| Date Due: | Thu Nov-9 |
Note: This is an ungraded assignment.
Here is the code we reviewed in class. Your homework tonight is:
1) Review this code, walk through the debugger line by line, watch the constructors and destructors get called, etc. Also, put breaks in the constructors and destructors so you can see them get called at times when you can't otherwise step into them (consider why they get called then, too).
2) Write a list of issues you need to discuss in class tomorrow concerning this code. (Failure to produce a thoughtful list results in an F in the grade book.) We will review this tomorrow, plus introduce some new material. Friday, there is no class, but I will be around for review. Monday, you should be prepared for a quiz on this material.
Good luck.
DK
// This code is not properly commented and is only
// designed to explicitly demonstrate certain aspects
// of C++ classes and subclasses.
#include <stdio.h>
#include <iostream.h>
#include "apstring.h"
#include "apvector.h"
//////////////////////////
//// Car.h
//////////////////////////
class Car
{
public:
static Car* makeNewCar(apstring name);
static void printCarClassNames();
virtual void printCarName() = 0;
protected:
static int registerCarClass(apstring name,
Car*(*subclassGenFn)());
// ignores return value
private:
static apvector<apstring> s_subclassNames;
static apvector<void*> s_subclassGenFns;
};
apvector<apstring> Car::s_subclassNames; // actually allocate
space for
the variable
apvector<void*> Car::s_subclassGenFns;
//////////////////////////
//// Ford.h
//////////////////////////
class Ford : Car
{
public:
Ford();
virtual void printCarName();
static Car* genInstance();
private:
static int s_registerCarSubClass;
static int s_fordCount;
int m_number;
};
Ford::Ford()
{
m_number = ++s_fordCount;
}
void Ford::printCarName()
{
cout << "Ford #" << m_number << endl;
}
Car* Ford::genInstance()
{
return new Ford;
}
int Ford::s_registerCarSubClass =
Car::registerCarClass("Ford",Ford::genInstance);
int Ford::s_fordCount = 0;
//////////////////////////
//// AstonMartin.h
//////////////////////////
class AstonMartin : Car
{
public:
AstonMartin();
virtual void printCarName();
static Car* genInstance();
private:
static int s_registerCarSubClass;
static int s_astonMartinCount;
int m_number;
};
AstonMartin::AstonMartin()
{
m_number = ++s_astonMartinCount;
}
void AstonMartin::printCarName()
{
cout << "AstonMartin #" << m_number <<
endl;
}
Car* AstonMartin::genInstance()
{
return new AstonMartin;
}
int AstonMartin::s_registerCarSubClass =
Car::registerCarClass("AstonMartin",AstonMartin::genInstance);
int AstonMartin::s_astonMartinCount = 0;
//////////////////////////
//// Car.cpp
//////////////////////////
// Return value is a dummy placeholder
int Car::registerCarClass(apstring name,
Car*(*subclassGenFn)())
{
int len = s_subclassNames.length();
s_subclassNames.resize(len+1);
s_subclassNames[len] = name;
s_subclassGenFns.resize(len+1);
s_subclassGenFns[len] = subclassGenFn;
return NULL;
}
// static
Car* Car::makeNewCar(apstring name)
{
// 1. find the car in the car class names
int i, len = s_subclassNames.length();
for (i=0; i<len; i++)
if (s_subclassNames[i] == name)
break;
if (i == len)
// darn, didn't find the name of the car class
// in the subclassNames array
return NULL;
// 2. We have the car name, create an instance of it
// (which just means to call its instanceGenFn...)
Car* (*instanceGenFn)() = (Car* (*) ()) s_subclassGenFns[i];
return instanceGenFn();
}
void Car::printCarClassNames()
{
int len = s_subclassNames.length();
cout << "CarClassNames:" << endl;
for (int i=0; i<len; i++)
cout << " " << i << ": " <<
s_subclassNames[i] << endl;
cout << "<end>" << endl;
}
Car* genCar(apstring subclassName)
{
cout << "Gencar[" << subclassName << "]:"
<< endl;
Car *pc;
pc = Car::makeNewCar(subclassName);
if (pc == NULL)
cout << "Dang, it failed!" << endl;
else
{
printf("Just created: ");
pc->printCarName();
printf("\n");
}
return pc;
}
void main()
{
Car::printCarClassNames();
Ford f1,f2,f3;
AstonMartin a1,a2;
f1.printCarName();
f2.printCarName();
f3.printCarName();
a1.printCarName();
a2.printCarName();
Car *pc1 = genCar("Ford");
Car *pc2 = genCar("AstonMartin");
Car *pc3 = genCar("Ford");
Car *pc4 = genCar("Chevy");
if (pc1) delete pc1;
if (pc2) delete pc2;
if (pc3) delete pc3;
if (pc4) delete pc4;
getchar();
}