虚函数——《C++编程风格》读书笔记(二)

来源:互联网 发布:大数据修炼系统醉寒 编辑:程序博客网 时间:2024/06/17 20:18
 

虚函数——《C++编程风格》读书笔记(二)

分类: 读书笔记 153人阅读 评论(0) 收藏 举报
读书c++编程deleteclassnull

1.例子

    设计一个车辆管理程序,程序对车辆进入车库与离开车库进行记录。

2.代码

 

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. class Vehicle  
  5. {  
  6. protected:  
  7.     char* plate;  
  8. public:  
  9.     Vehicle() { plate = NULL;}  
  10.     Vehicle(char* p)  
  11.     {  
  12.         plate = new char[strlen(p) + 1];  
  13.         strcpy(plate,p);  
  14.     }  
  15.     ~Vehicle() {delete [] plate;}  
  16.     virtual void identfy()  
  17.     {  
  18.         printf("generic vehicle/n");  
  19.     }  
  20. };  
  21.   
  22. class Car : public Vehicle  
  23. {  
  24. public:  
  25.     Car() : Vehicle() {}  
  26.     Car(char* p) : Vehicle(p) {}  
  27.     void identfy()  
  28.     {  
  29.         char *p = plate ? plate : "<none>";  
  30.         printf("car with plate %s/n",p);  
  31.     }  
  32. };  
  33.   
  34. class Truck : public Vehicle  
  35. {  
  36. public:  
  37.     Truck() : Vehicle() {}  
  38.     Truck(char *p):Vehicle(p){}  
  39.     void identfy()  
  40.     {  
  41.         char *p = plate ? plate : "<none>";  
  42.         printf("car with plate %s/n",p);  
  43.     }  
  44. };  
  45.   
  46. class Garage  
  47. {  
  48.     int maxVehicles;  
  49.     Vehicle **parked;  
  50. public:  
  51.     Garage(int max);  
  52.     ~Garage();  
  53.     int accept(Vehicle*);  
  54.     Vehicle *release(int bay);  
  55.     void listVehicles();  
  56.   
  57. };  
  58.   
  59. Garage::Garage(int max)  
  60. {  
  61.     maxVehicles = max;  
  62.     parked = new Vehicle*[maxVehicles];  
  63.     for(int bay = 0;bay < maxVehicles;++bay)  
  64.         parked[bay] = NULL;  
  65. }  
  66.   
  67. Garage::~Garage()  
  68. {  
  69.     delete [] parked;  
  70. }  
  71.   
  72. int Garage::accept(Vehicle *veh)  
  73. {  
  74.     for(int bay = 0;bay < maxVehicles;++bay)  
  75.         if(!parked[bay])  
  76.         {  
  77.             parked[bay] = veh;  
  78.             return bay;  
  79.         }  
  80.     return -1;  
  81. }  
  82.   
  83. Vehicle *Garage::release(int bay)  
  84. {  
  85.     if(bay < 0 || bay >= maxVehicles)  
  86.         return NULL;  
  87.     Vehicle *veh = parked[bay];  
  88.     parked[bay] = NULL;  
  89.     return veh;  
  90. }  
  91.   
  92. void Garage::listVehicles()  
  93. {  
  94.     for(int bay = 0;bay < maxVehicles;++bay)  
  95.         if(parked[bay])  
  96.         {  
  97.             printf("Vehicle in bay %d is: ",bay);  
  98.             parked[bay]->identfy();  
  99.         }  
  100. }  
  101.   
  102. Car c1("RVR 101");  
  103. Car c2("SPT 202");  
  104. Car c3("CHP 303");  
  105. Car c4("BDY 404");  
  106. Car c5("BCH 505");  
  107.   
  108. Truck t1("TBL 606");  
  109. Truck t2("TBL 707");  
  110. Truck t3("TBL 808");  
  111. Truck t4("TBL 909");  
  112. Truck t5("TBL 000");  
  113.   
  114. int main()  
  115. {  
  116.     Garage Park(15);  
  117.   
  118.     Park.accept(&c1);  
  119.   
  120.     int t2bay = Park.accept(&t2);  
  121.   
  122.     Park.accept(&c3);  
  123.     Park.accept(&t1);  
  124.   
  125.     int c4bay = Park.accept(&c4);  
  126.   
  127.     Park.accept(&c5);  
  128.     Park.accept(&t5);  
  129.   
  130.     Park.release(t2bay);  
  131.   
  132.     Park.accept(&t4);  
  133.     Park.accept(&t3);  
  134.   
  135.     Park.release(c4bay);  
  136.   
  137.     Park.accept(&c2);  
  138.   
  139.     Park.listVehicles();  
  140.   
  141.     return 0;  
  142. }  

3.代码分析

    3.1 析构函数

 

如果定义一个

[cpp] view plaincopy
  1. Vehicle *p = new car  
  2. //....  
  3. delete p  

程序将在编译器更具delete表达式中指针的类型来决定调用哪个析构函数,由于p是基类型指针,并且基类的析构函数是非虚的,因此只有基类的析构函数被执行,如果有涉及到派生类对象的析构函数的内容,程序将会出错。

原则:

    a.如果在公有基类中没有定义虚析构函数,那么在所有的派生类或者派生类的数据成员中都应该没有定义析构函数。

    b.通常情况下,公有基类的析构函数应该被声明为虚函数。

   3.2 继承

    由于Car 和Truck的唯一区别就是属性值,一个字符串的名称不同而已,二者在行为上没有区别;考虑到这一点,可以将共同行为identfy()迁移到基类中去。由于这一迁移,基中原来的protected的plate数据成员无需在其派生类中被访问到,因而可以将其改为private.

原则:

    c.将共同行为迁移到基类中。

经过这两步后的程序为:

 

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. class Vehicle  
  5. {  
  6. private:  
  7.     char* plate;  
  8.     virtual char *group() = 0;  
  9. public:  
  10.     Vehicle() { plate = NULL;}  
  11.     Vehicle(char* p)  
  12.     {  
  13.         plate = new char[strlen(p) + 1];  
  14.         strcpy(plate,p);  
  15.     }  
  16.     virtual ~Vehicle() {delete [] plate;}  
  17.     void identfy()  
  18.     {  
  19.         char *p = plate ? plate : "<none>";  
  20.         printf("%s with plate %s/n",group(),p);  
  21.     }  
  22. };  
  23.   
  24. class Car : public Vehicle  
  25. {  
  26. private:  
  27.     char *group() {return "car";}  
  28. public:  
  29.     Car() : Vehicle() {}  
  30.     Car(char* p) : Vehicle(p) {}  
  31. };  
  32.   
  33. class Truck : public Vehicle  
  34. {  
  35. private:  
  36.     char *group() {return "truck";}//成员函数的作用:作为类的实现  
  37. public:  
  38.     Truck() : Vehicle() {}  
  39.     Truck(char *p):Vehicle(p){}   
  40. };  
  41.   
  42. class Garage  
  43. {  
  44.     int maxVehicles;  
  45.     Vehicle **parked;  
  46. public:  
  47.     Garage(int max);  
  48.     ~Garage();  
  49.     int accept(Vehicle*);  
  50.     Vehicle *release(int bay);  
  51.     void listVehicles();  
  52.   
  53. };  
  54.   
  55. Garage::Garage(int max)  
  56. {  
  57.     maxVehicles = max;  
  58.     parked = new Vehicle*[maxVehicles];  
  59.     for(int bay = 0;bay < maxVehicles;++bay)  
  60.         parked[bay] = NULL;  
  61. }  
  62.   
  63. Garage::~Garage()  
  64. {  
  65.     delete [] parked;  
  66. }  
  67.   
  68. int Garage::accept(Vehicle *veh)  
  69. {  
  70.     for(int bay = 0;bay < maxVehicles;++bay)  
  71.         if(!parked[bay])  
  72.         {  
  73.             parked[bay] = veh;  
  74.             return bay;  
  75.         }  
  76.     return -1;  
  77. }  
  78.   
  79. Vehicle *Garage::release(int bay)  
  80. {  
  81.     if(bay < 0 || bay >= maxVehicles)  
  82.         return NULL;  
  83.     Vehicle *veh = parked[bay];  
  84.     parked[bay] = NULL;  
  85.     return veh;  
  86. }  
  87.   
  88. void Garage::listVehicles()  
  89. {  
  90.     for(int bay = 0;bay < maxVehicles;++bay)  
  91.         if(parked[bay])  
  92.         {  
  93.             printf("Vehicle in bay %d is: ",bay);  
  94.             parked[bay]->identfy();  
  95.         }  
  96. }  
  97.   
  98. Car c1("RVR 101");  
  99. Car c2("SPT 202");  
  100. Car c3("CHP 303");  
  101. Car c4("BDY 404");  
  102. Car c5("BCH 505");  
  103.   
  104. Truck t1("TBL 606");  
  105. Truck t2("TBL 707");  
  106. Truck t3("TBL 808");  
  107. Truck t4("TBL 909");  
  108. Truck t5("TBL 000");  
  109.   
  110. int main()  
  111. {  
  112.     Garage Park(15);  
  113.   
  114.     Park.accept(&c1);  
  115.   
  116.     int t2bay = Park.accept(&t2);  
  117.   
  118.     Park.accept(&c3);  
  119.     Park.accept(&t1);  
  120.   
  121.     int c4bay = Park.accept(&c4);  
  122.   
  123.     Park.accept(&c5);  
  124.     Park.accept(&t5);  
  125.   
  126.     Park.release(t2bay);  
  127.   
  128.     Park.accept(&t4);  
  129.     Park.accept(&t3);  
  130.   
  131.     Park.release(c4bay);  
  132.   
  133.     Park.accept(&c2);  
  134.   
  135.     Park.listVehicles();  
  136.   
  137.     return 0;  
  138. }  

  3.3耦合(coupling)

      group()的使用减少了Vehicle与派生类之间的耦合。所谓耦合就是指组件之间的相互作用程度。最初的程序中派生类通过三种方式与基类进行交互:首先,派生类的构造函数需要为基类的构造函数提供一个参数。其次,派生类中需要定义自己的identfy()函数。最后,派生类的identfy()需要访问基类的plate成员值。修改后,通过group()这种方式代替了后两种交互。降低了耦合。

     现在考虑第一种:数据成员的值可以通过Vehicle的构造函数增加一个参数来获得,并且这个参数值将有相应的派生类的构造函数获得。

原则:

     d.降低耦合性,将类之间的交互最小化。

     e.如果派生类之间的区别在于属性,则用数据成员来表示;如果在于行为,则用虚函数来表示。(group表示的其实是一种属性)

经过这步后:

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. class Vehicle  
  5. {  
  6. private:  
  7.     char* plate;  
  8.     char *group;  
  9. public:  
  10.     Vehicle(char *g) { group = g;plate = NULL;}  
  11.     Vehicle(char* g,char *p)  
  12.     {  
  13.         group = g;  
  14.         plate = new char[strlen(p) + 1];  
  15.         strcpy(plate,p);  
  16.     }  
  17.     virtual ~Vehicle() {delete [] plate;}  
  18.     void identfy()  
  19.     {  
  20.         char *p = plate ? plate : "<none>";  
  21.         printf("%s with plate %s/n",group,p);  
  22.     }  
  23. };  
  24.   
  25. class Car : public Vehicle  
  26. {  
  27.   
  28. public:  
  29.     Car() : Vehicle("car") {}  
  30.     Car(char* p) : Vehicle("car",p) {}  
  31. };  
  32.   
  33. class Truck : public Vehicle  
  34. {  
  35. public:  
  36.     Truck() : Vehicle("truck") {}  
  37.     Truck(char *p):Vehicle("truck",p){}   
  38. };  
  39.   
  40. class Garage  
  41. {  
  42.     int maxVehicles;  
  43.     Vehicle **parked;  
  44. public:  
  45.     Garage(int max);  
  46.     ~Garage();  
  47.     int accept(Vehicle*);  
  48.     Vehicle *release(int bay);  
  49.     void listVehicles();  
  50.   
  51. };  
  52.   
  53. Garage::Garage(int max)  
  54. {  
  55.     maxVehicles = max;  
  56.     parked = new Vehicle*[maxVehicles];  
  57.     for(int bay = 0;bay < maxVehicles;++bay)  
  58.         parked[bay] = NULL;  
  59. }  
  60.   
  61. Garage::~Garage()  
  62. {  
  63.     delete [] parked;  
  64. }  
  65.   
  66. int Garage::accept(Vehicle *veh)  
  67. {  
  68.     for(int bay = 0;bay < maxVehicles;++bay)  
  69.         if(!parked[bay])  
  70.         {  
  71.             parked[bay] = veh;  
  72.             return bay;  
  73.         }  
  74.     return -1;  
  75. }  
  76.   
  77. Vehicle *Garage::release(int bay)  
  78. {  
  79.     if(bay < 0 || bay >= maxVehicles)  
  80.         return NULL;  
  81.     Vehicle *veh = parked[bay];  
  82.     parked[bay] = NULL;  
  83.     return veh;  
  84. }  
  85.   
  86. void Garage::listVehicles()  
  87. {  
  88.     for(int bay = 0;bay < maxVehicles;++bay)  
  89.         if(parked[bay])  
  90.         {  
  91.             printf("Vehicle in bay %d is: ",bay);  
  92.             parked[bay]->identfy();  
  93.         }  
  94. }  
  95.   
  96. Car c1("RVR 101");  
  97. Car c2("SPT 202");  
  98. Car c3("CHP 303");  
  99. Car c4("BDY 404");  
  100. Car c5("BCH 505");  
  101.   
  102. Truck t1("TBL 606");  
  103. Truck t2("TBL 707");  
  104. Truck t3("TBL 808");  
  105. Truck t4("TBL 909");  
  106. Truck t5("TBL 000");  
  107.   
  108. int main()  
  109. {  
  110.     Garage Park(15);  
  111.   
  112.     Park.accept(&c1);  
  113.   
  114.     int t2bay = Park.accept(&t2);  
  115.   
  116.     Park.accept(&c3);  
  117.     Park.accept(&t1);  
  118.   
  119.     int c4bay = Park.accept(&c4);  
  120.   
  121.     Park.accept(&c5);  
  122.     Park.accept(&t5);  
  123.   
  124.     Park.release(t2bay);  
  125.   
  126.     Park.accept(&t4);  
  127.     Park.accept(&t3);  
  128.   
  129.     Park.release(c4bay);  
  130.   
  131.     Park.accept(&c2);  
  132.   
  133.     Park.listVehicles();  
  134.   
  135.     return 0;  
  136. }  

3.4 默认参数

    在基类和派生类中各有两个构造函数,区别在于第二个参数,在只有一个参数的构造函数中,第二个参数被视为空的。因而我们考虑使用默认参数的形式来代替函数重载,这能够减少一半的构造函数,付出的代价是提高了基类与派生类之间的耦合性。

    写在基类内部的成员函数被默认为内联的,在没有明显好处的情形下,应该放在基外。

 

原则:

f.考虑使用默认参数的形式来代替函数重载。

修改后的函数代码为:

[c-sharp] view plaincopy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. class Vehicle  
  5. {  
  6. private:  
  7.     char* plate;  
  8.     char *group;  
  9. public:  
  10.     Vehicle::Vehicle(char* g,char *p);  
  11.     virtual ~Vehicle() = 0;  
  12.     void identfy()  
  13.     {  
  14.         char *p = plate ? plate : "<none>";  
  15.         printf("%s with plate %s/n",group,p);  
  16.     }  
  17. };  
  18. Vehicle::Vehicle(char* g,char *p)  
  19. {  
  20.     group = g;  
  21.     if(p)  
  22.     {  
  23.         plate = new char[strlen(p) + 1];  
  24.         strcpy(plate,p);  
  25.     }  
  26.     else  
  27.         plate = NULL;  
  28. }  
  29. Vehicle::~Vehicle()  
  30. {  
  31.     delete [] plate;  
  32. }  
  33. class Car : public Vehicle  
  34. {  
  35. public:  
  36.     Car(char* p = NULL) : Vehicle("car",p) {}  
  37. };  
  38.   
  39. class Truck : public Vehicle  
  40. {  
  41. public:  
  42.     Truck(char *p = NULL):Vehicle("truck",p){}  
  43. };  
  44.   
  45. class Garage  
  46. {  
  47.     int maxVehicles;  
  48.     Vehicle **parked;  
  49. public:  
  50.     Garage(int max);  
  51.     ~Garage();  
  52.     int accept(Vehicle*);  
  53.     Vehicle *release(int bay);  
  54.     void listVehicles();  
  55.   
  56. };  
  57.   
  58. Garage::Garage(int max)  
  59. {  
  60.     maxVehicles = max;  
  61.     parked = new Vehicle*[maxVehicles];  
  62.     for(int bay = 0;bay < maxVehicles;++bay)  
  63.         parked[bay] = NULL;  
  64. }  
  65.   
  66. Garage::~Garage()  
  67. {  
  68.     delete [] parked;  
  69. }  
  70.   
  71. int Garage::accept(Vehicle *veh)  
  72. {  
  73.     for(int bay = 0;bay < maxVehicles;++bay)  
  74.         if(!parked[bay])  
  75.         {  
  76.             parked[bay] = veh;  
  77.             return bay;  
  78.         }  
  79.     return -1;  
  80. }  
  81.   
  82. Vehicle *Garage::release(int bay)  
  83. {  
  84.     if(bay < 0 || bay >= maxVehicles)  
  85.         return NULL;  
  86.     Vehicle *veh = parked[bay];  
  87.     parked[bay] = NULL;  
  88.     return veh;  
  89. }  
  90.   
  91. void Garage::listVehicles()  
  92. {  
  93.     for(int bay = 0;bay < maxVehicles;++bay)  
  94.         if(parked[bay])  
  95.         {  
  96.             printf("Vehicle in bay %d is: ",bay);  
  97.             parked[bay]->identfy();  
  98.         }  
  99. }  
  100.   
  101. Car c1("RVR 101");  
  102. Car c2("SPT 202");  
  103. Car c3("CHP 303");  
  104. Car c4("BDY 404");  
  105. Car c5("BCH 505");  
  106.   
  107. Truck t1("TBL 606");  
  108. Truck t2("TBL 707");  
  109. Truck t3("TBL 808");  
  110. Truck t4("TBL 909");  
  111. Truck t5("TBL 000");  
  112.   
  113. int main()  
  114. {  
  115.     Garage Park(15);  
  116.   
  117.     Park.accept(&c1);  
  118.   
  119.     int t2bay = Park.accept(&t2);  
  120.   
  121.     Park.accept(&c3);  
  122.     Park.accept(&t1);  
  123.   
  124.     int c4bay = Park.accept(&c4);  
  125.   
  126.     Park.accept(&c5);  
  127.     Park.accept(&t5);  
  128.   
  129.     Park.release(t2bay);  
  130.   
  131.     Park.accept(&t4);  
  132.     Park.accept(&t3);  
  133.   
  134.     Park.release(c4bay);  
  135.   
  136.     Park.accept(&c2);  
  137.   
  138.     Park.listVehicles();  
  139.   
  140.     return 0;  
  141. }  

原创粉丝点击