虚函数.pptVIP

  • 25
  • 0
  • 约1.15万字
  • 约 41页
  • 2017-02-24 发布于上海
  • 举报
虚函数

虚函数 思考:为什么要引入虚函数//test_virtual.cpp 【例4】没有使用虚函数的例题。 #includeiostream.h class base /定义基类base { public: void who() {coutthis is the class of base!endl;} }; class derive2 : public base //定义派生类derive2 { public: void who() {coutthis is the class of derive2!endl;} }; void main() { base obj,*ptr; derive1 obj1; derive2 obj2; ptr = obj; ptr-who(); ptr=obj1; //指向第一个子类 ptr-who(); ptr=obj2; //指向第二个子类 ptr-who(); obj1.who(); //分别指向子类 obj2.who(); 程序运行结果: This is the class of base! (a) This is the class of bas

文档评论(0)

1亿VIP精品文档

相关文档