- /* container.h */
-
#define DEFAULT_SIZE 100
-
-
class TObject;
-
typedef TObject * PTObject;
-
typedef PTObject * PPTObject;
-
-
class TObject {
-
public:
-
virtual ~TObject() { };
-
virtual int Compare(PTObject p)=0;
-
virtual void Display()=0;
-
};
-
-
class TContainer {
-
private:
-
int size;
-
int count;
-
PPTObject objects;
-
protected:
-
void QuickSort(int left, int right);
-
public:
-
TContainer(int n = DEFAULT_SIZE);
-
~TContainer();
-
//inline member fuctions
-
bool IsFull() { return (count >= size); }
-
int GetSize() { return size; }
-
int GetCount() { return count; }
-
//other public member functions
-
void PutObject(PTObject pto);
-
void ShowAllObjects(const char*msg);
-
void Sort();
- };
- /* container.cpp */
- #include
-
#include
- #include "container.h"
-
- using namespace std;
-
- TContainer::TContainer(int n)
- {
- if (n <= 0) n = 1;
- size = n;
- count = 0;
-
- objects = new PTObject[size];
- for (int i = 0; i < size; i++)
- objects[i] = NULL;
- }
-
- TContainer:: ~TContainer()
- {
- for (int i = 0; i < count; i++)
- delete objects[i];
- #ifdef DEBUG
- cout << "Deleting container" << endl;
- #endif
- delete objects;
- }
-
- void TContainer::PutObject(PTObject pto)
- {
- if (IsFull())
- {
- cout << "* * * Error: Container is Full" << endl;
- exit(1);
- }
- objects[count] = pto;
- count++;
- }
-
- void TContainer::ShowAllObjects(const char * msg)
- {
- cout << msg << endl;
- cout << "Number of objects == " << count << endl;
- for ( int i = 0; i < count; i++ )
- objects[i]->Display();
- cout << endl << endl;
- }
-
- void TContainer::QuickSort(int left,int right)
- {
- int i = left;
- int j = right;
- PTObject test = objects[(left + right)/2];
- PTObject swap;
- do {
- while(objects[i] -> Compare(test) < 0) i++;
- while (test->Compare(objects[j]) < 0) j--;
- if (i <= j) {
- swap = objects[i];
- objects[i] = objects[j];
- objects[j] = swap;
- i++;
- j--;
- }
- }while (i <= j);
- if (left < j) QuickSort(left,j);
- if (i < right) QuickSort(i,right);
- }
-
- void TContainer::Sort()
- {
- if(count > 1) QuickSort(0,count - 1);
-
}
- /* tcontain.cpp */
-
#include <iostream>
-
#include <string.h>
-
#include <stdlib.h>
-
#include "container.h"
-
-
using namespace std;
-
-
class TMyObject:public TObject {
-
private:
-
char *sp;
-
public:
-
TMyObject(const char *s) {
-
sp = new char(strlen(s) + 1);
-
strcpy(sp, s);
-
}
-
virtual ~TMyObject();
-
virtual int Compare(PTObject p);
-
virtual void Display();
-
};
-
-
TMyObject::~TMyObject()
-
{
-
#ifdef DEBUG
-
cout << "Inside destructor for" << sp << endl;
-
#endif
-
delete[]sp;
-
}
-
-
//compare two TMyObject objects
-
int TMyObject::Compare(PTObject p)
-
{
-
return strcmp(sp, ( (TMyObject *)p)->sp );
-
}
-
-
void TMyObject::Display()
-
{
-
cout << sp <<" ";
-
}
-
-
int main()
-
{
-
cout << endl << "Test TContainer class" << endl << endl;
-
TContainer * container = new TContainer(100);
-
container->PutObject(new TMyObject("Peach"));
-
container->PutObject(new TMyObject("Mango"));
-
container->PutObject(new TMyObject("Lime"));
-
container->PutObject(new TMyObject("Banana"));
-
container->PutObject(new TMyObject("Kiwi"));
-
container->PutObject(new TMyObject("Grapefruit"));
-
container->PutObject(new TMyObject("Orange"));
-
container->PutObject(new TMyObject("Lemon"));
-
container->PutObject(new TMyObject("Apple"));
-
-
container->ShowAllObjects("Before sorting");
-
container->Sort();
-
container->ShowAllObjects("After sorting");
-
-
delete container; //这一条语句可以删除所有对象
-
-
return 0;
- }
- /* tcontain.cpp */
类的多态的一个实例,实现类的动态多态。。。虚函数,纯虚函数的定义与使用
编译:
g++ -g --DEBUG -c container.cpp
g++ -g --DEBUG tcontain.cpp container.o
结果: