示例:

    1. #include<iostream>
    2. using namespace std;
    3. class AbstractDrinking
    4. {
    5. public:
    6. //煮水 boil煮水
    7. virtual void Boil()=0;
    8. //沏茶brew
    9. virtual void Brew()=0;
    10. //倒入杯中
    11. virtual void Incup()=0;
    12. // 加入佐料
    13. virtual void PutSomething()=0;
    14. void show()
    15. {
    16. Boil();
    17. Brew();
    18. Incup();
    19. PutSomething();
    20. }
    21. };
    22. class Coffee:public AbstractDrinking
    23. {
    24. public:
    25. void Boil()
    26. {
    27. cout<<"煮露水"<<endl;
    28. }
    29. void Brew()
    30. {
    31. cout<<"沏咖啡"<<endl;
    32. }
    33. void Incup()
    34. {
    35. cout<<"倒入杯中"<<endl;
    36. }
    37. void PutSomething()
    38. {
    39. cout<<"加入牛奶"<<endl;
    40. }
    41. };
    42. class Tea:public AbstractDrinking
    43. {
    44. public:
    45. void Boil()
    46. {
    47. cout<<"煮水"<<endl;
    48. }
    49. void Brew()
    50. {
    51. cout<<"沏茶"<<endl;
    52. }
    53. void Incup()
    54. {
    55. cout<<"倒入杯中"<<endl;
    56. }
    57. void PutSomething()
    58. {
    59. cout<<"加入柠檬"<<endl;
    60. }
    61. };
    62. void test(AbstractDrinking *pre)//这里相当于AbstractDrinking *per=new Coffee
    63. {
    64. pre->show();
    65. return ;
    66. }
    67. void test01()
    68. {
    69. test(new Coffee);
    70. cout<<"*****************"<<endl;
    71. test(new Tea);
    72. }
    73. int main()
    74. {
    75. test01();
    76. return 0;