Chinaunix首页 | 论坛 | 博客
  • 博客访问: 857374
  • 博文数量: 254
  • 博客积分: 5350
  • 博客等级: 大校
  • 技术积分: 2045
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-27 13:27
文章分类

全部博文(254)

文章存档

2015年(1)

2014年(9)

2013年(17)

2012年(30)

2011年(150)

2010年(17)

2009年(28)

2008年(2)

分类: C/C++

2011-11-21 16:59:09

  1. /* container.h */
  2. #define DEFAULT_SIZE 100

  3. class TObject;
  4. typedef TObject * PTObject;
  5. typedef PTObject * PPTObject;

  6. class TObject {
  7. public:
  8.     virtual ~TObject() { };
  9.     virtual int Compare(PTObject p)=0;
  10.     virtual void Display()=0;
  11. };

  12. class TContainer {
  13. private:
  14.     int size;
  15.     int count;
  16.     PPTObject objects;
  17. protected:
  18.     void QuickSort(int left, int right);
  19. public:
  20.     TContainer(int n = DEFAULT_SIZE);
  21.     ~TContainer();
  22.     //inline member fuctions
  23.     bool IsFull() { return (count >= size); }
  24.     int GetSize() { return size; }
  25.     int GetCount() { return count; }
  26.     //other public member functions
  27.     void PutObject(PTObject pto);
  28.     void ShowAllObjects(const char*msg);
  29.     void Sort();
  30. };

  1. /* container.cpp */
  2. #include
  3. #include
  4. #include "container.h"

  5. using namespace std;

  6. TContainer::TContainer(int n)
  7. {
  8. if (n <= 0) n = 1;
  9. size = n;
  10. count = 0;

  11. objects = new PTObject[size];
  12. for (int i = 0; i < size; i++)
  13. objects[i] = NULL;
  14. }

  15. TContainer:: ~TContainer()
  16. {
  17. for (int i = 0; i < count; i++)
  18. delete objects[i];
  19. #ifdef DEBUG
  20. cout << "Deleting container" << endl;
  21. #endif
  22. delete objects;
  23. }

  24. void TContainer::PutObject(PTObject pto)
  25. {
  26. if (IsFull())
  27. {
  28. cout << "* * * Error: Container is Full" << endl;
  29. exit(1);
  30. }
  31. objects[count] = pto;
  32. count++;
  33. }

  34. void TContainer::ShowAllObjects(const char * msg)
  35. {
  36. cout << msg << endl;
  37. cout << "Number of objects == " << count << endl;
  38. for ( int i = 0; i < count; i++ )
  39. objects[i]->Display();
  40. cout << endl << endl;
  41. }

  42. void TContainer::QuickSort(int left,int right)
  43. {
  44. int i = left;
  45. int j = right;
  46. PTObject test = objects[(left + right)/2];
  47. PTObject swap;
  48. do {
  49. while(objects[i] -> Compare(test) < 0) i++;
  50. while (test->Compare(objects[j]) < 0) j--;
  51. if (i <= j) {
  52. swap = objects[i];
  53. objects[i] = objects[j];
  54. objects[j] = swap;
  55. i++;
  56. j--;
  57. }
  58. }while (i <= j);
  59. if (left < j) QuickSort(left,j);
  60. if (i < right) QuickSort(i,right);
  61. }

  62. void TContainer::Sort()
  63. {
  64. if(count > 1) QuickSort(0,count - 1);
  65. }
    1. /* tcontain.cpp */
    2. #include <iostream>
    3. #include <string.h>
    4. #include <stdlib.h>
    5. #include "container.h"

    6. using namespace std;

    7. class TMyObject:public TObject {
    8. private:
    9.     char *sp;
    10. public:
    11.     TMyObject(const char *s) {
    12.         sp = new char(strlen(s) + 1);
    13.         strcpy(sp, s);
    14.         }
    15.     virtual ~TMyObject();
    16.     virtual int Compare(PTObject p);
    17.     virtual void Display();
    18. };

    19. TMyObject::~TMyObject()
    20. {
    21. #ifdef DEBUG
    22.     cout << "Inside destructor for" << sp << endl;
    23. #endif
    24.     delete[]sp;
    25. }

    26. //compare two TMyObject objects
    27. int TMyObject::Compare(PTObject p)
    28. {
    29.     return strcmp(sp, ( (TMyObject *)p)->sp );
    30. }

    31. void TMyObject::Display()
    32. {
    33.     cout << sp <<" ";
    34. }

    35. int main()
    36. {
    37.     cout << endl << "Test TContainer class" << endl << endl;
    38.     TContainer * container = new TContainer(100);
    39.     container->PutObject(new TMyObject("Peach"));
    40.     container->PutObject(new TMyObject("Mango"));
    41.     container->PutObject(new TMyObject("Lime"));
    42.     container->PutObject(new TMyObject("Banana"));
    43.     container->PutObject(new TMyObject("Kiwi"));
    44.     container->PutObject(new TMyObject("Grapefruit"));
    45.     container->PutObject(new TMyObject("Orange"));
    46.     container->PutObject(new TMyObject("Lemon"));
    47.     container->PutObject(new TMyObject("Apple"));

    48.     container->ShowAllObjects("Before sorting");
    49.     container->Sort();
    50.     container->ShowAllObjects("After sorting");

    51.     delete container; //这一条语句可以删除所有对象

    52.     return 0;
    53. }

类的多态的一个实例,实现类的动态多态。。。虚函数,纯虚函数的定义与使用
编译:
g++ -g --DEBUG -c container.cpp
g++ -g --DEBUG tcontain.cpp container.o

结果:

阅读(618) | 评论(0) | 转发(0) |
0

上一篇:类的继承代码演示

下一篇:异常 处理

给主人留下些什么吧!~~