class Ireader { public: virtual string Getcontents() = 0; }; class Book : public Ireader { public: virtual string Getcontents() { return "读本书!"; } }; class NewsPaper : public Ireader { public: virtual string Getcontents() { return "读报纸!"; } }; class EBook : public Ireader { public: virtual string Getcontents() { return "读电子书!"; } }; class Mother { public: void tellStory(Ireader* pi) { cout << pi->Getcontents() << endl; } }; void TestDIP() { Mother m; Book b; NewsPaper n; EBook eb; m.tellStory(&b); m.tellStory(&n); m.tellStory(&eb); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)