1 #include <iostream> 2 #include<iomanip> 3 using namespace std; 4 class Horse 5 { 6 public: 7 Horse() 8 { 9 cout << "Horse 申请了空间..." << endl; 10 } 11 virtual ~Horse() 12 { 13 cout << "Horse 释放了空间..." << endl; 14 } 15 virtual void Fly() 16 { 17 cout << "Just a horse." << endl; 18 } 19 }; 20 class Pegasus :public Horse 21 { 22 public: 23 Pegasus() 24 { 25 cout << "Pegasus 申请了空间..." << endl; 26 } 27 ~Pegasus() 28 { 29 cout << "Pegasus 释放了空间..." << endl; 30 } 31 void Fly() 32 { 33 cout << "I can fly!" << endl; 34 } 35 }; 36 int main() 37 { 38 Horse* p1 = new Horse; //输出:Horse 申请了空间... 39 Horse* p2 = new Pegasus; /* 输出两行: 40 Horse 申请了空间... 41 Pegasus 申请了空间...*/ 42 cout << endl; 43 44 p1->Fly(); //输出:Just a horse. 45 p2->Fly(); //输出:I can fly! 46 cout << endl; 47 48 delete p1; //输出:Horse 释放了空间... 49 delete p2; /* 输出两行: 50 Pegasus 释放了空间... 51 Horse 释放了空间...*/ 52 return 0; 53 }
标签:24,Fly,Horse,cout,2023.5,include From: https://www.cnblogs.com/muzhaodi/p/17429600.html