[C++] c++ 调用多个构造函数 →→→→→进入此内容的聊天室

来自 , 2019-10-04, 写在 C++, 查看 99 次.
URL http://www.code666.cn/view/3c947bc2
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. typedef int HANDS;
  5. enum COLOR {
  6.         Red, Green, Blue, Yellow, White, Black, Brown
  7. };
  8.  
  9. class Horse {
  10. public:
  11.         Horse(COLOR color, HANDS height);
  12.         virtual ~Horse() {
  13.                 cout << "Horse destructor...\n";
  14.         }
  15.         virtual void Whinny() const {
  16.                 cout << "Whinny!... ";
  17.         }
  18.         virtual HANDS GetHeight() const {
  19.                 return itsHeight;
  20.         }
  21.         virtual COLOR GetColor() const {
  22.                 return itsColor;
  23.         }
  24. private:
  25.         HANDS itsHeight;
  26.         COLOR itsColor;
  27. };
  28.  
  29. Horse::Horse(COLOR color, HANDS height) :
  30.                 itsColor(color), itsHeight(height) {
  31.         cout << "Horse constructor...\n";
  32. }
  33.  
  34. class Bird {
  35. public:
  36.         Bird(COLOR color, bool migrates);
  37.         virtual ~Bird() {
  38.                 cout << "Bird destructor...\n";
  39.         }
  40.         virtual void Chirp() const {
  41.                 cout << "Chirp... ";
  42.         }
  43.         virtual void Fly() const {
  44.                 cout << "I can fly! I can fly! I can fly! ";
  45.         }
  46.         virtual COLOR GetColor() const {
  47.                 return itsColor;
  48.         }
  49.         virtual bool GetMigration() const {
  50.                 return itsMigration;
  51.         }
  52.  
  53. private:
  54.         COLOR itsColor;
  55.         bool itsMigration;
  56. };
  57.  
  58. Bird::Bird(COLOR color, bool migrates) :
  59.                 itsColor(color), itsMigration(migrates) {
  60.         cout << "Bird constructor...\n";
  61. }
  62.  
  63. class Pegasus: public Horse, public Bird {
  64. public:
  65.         void Chirp() const {
  66.                 Whinny();
  67.         }
  68.         Pegasus(COLOR, HANDS, bool, long);
  69.         ~Pegasus() {
  70.                 cout << "Pegasus destructor...\n";
  71.         }
  72.         virtual long GetNumberBelievers() const {
  73.                 return itsNumberBelievers;
  74.         }
  75.  
  76. private:
  77.         long itsNumberBelievers;
  78. };
  79.  
  80. Pegasus::Pegasus(COLOR aColor, HANDS height, bool migrates, long NumBelieve) :
  81.                 Horse(aColor, height), Bird(aColor, migrates), itsNumberBelievers(
  82.                                 NumBelieve) {
  83.         cout << "Pegasus constructor...\n";
  84. }
  85.  
  86. int main() {
  87.         Pegasus *pPeg = new Pegasus(Red, 5, true, 10);
  88.         pPeg->Fly();
  89.         pPeg->Whinny();
  90.         cout << "\nYour Pegasus is " << pPeg->GetHeight();
  91.         cout << " hands tall and ";
  92.         if (pPeg->GetMigration())
  93.                 cout << "it does migrate.";
  94.         else
  95.                 cout << "it does not migrate.";
  96.         cout << "\nA total of " << pPeg->GetNumberBelievers();
  97.         cout << " people believe it exists." << endl;
  98.         delete pPeg;
  99.         return 0;
  100. }
  101.  

回复 "c++ 调用多个构造函数"

这儿你可以回复上面这条便签

captcha