c++学习-虚函数
2015-06-21 15:21
381 查看
#include <iostream> using namespace std; class common{ public: virtual void hello(){cout<<"common hello"<<endl;} }; class a: public common{ public: //重写基类的虚函数后,本函数也是虚函数 可以省略 virtual virtual void hello(){cout<<"a hello"<<endl;} }; class b: public common{ public: void hello(){cout<<"b hello"<<endl;} }; int main() { //动态联编 在运行时才确定哪个指针确定哪个对象 //静态联编 在编译时就确定哪个指针确定哪个对象 速度快,浪费小 //虚函数实现了调用指向真实对象的方法 common *p = new a(); p->hello(); return 0; }
静态联编:在编译时就确定指针指向的类
#include <iostream> using namespace std; class common{ public: void hello(){cout<<"common hello"<<endl;} }; class a: public common{ public: void hello(){cout<<"a hello"<<endl;} }; class b: public common{ public: void hello(){cout<<"b hello"<<endl;} }; int main() { a i; common *p; //在编译时就确定指向 common 类,再改变也是无效的 p = &i; p->hello(); return 0; }
多态满足的条件:
#include <iostream> using namespace std; class father{ public: virtual void hello(){cout<<"father hello"<<endl;} }; class son: public father{ public: void hello(){cout<<"son hello"<<endl;} }; class doc: public father{ public: void hello(){cout<<"doc hello"<<endl;} }; void one(father one) { one.hello(); } void two(father *two) { two->hello(); } void three(father &three) { three.hello(); } int main() { /** 满足多态的条件: 1、继承 2、父类引用指向子类对象(指针或引用) 3、基类函数为虚函数 */ father *p=0; int choice; while(1){ bool quit = false; cout<<"0:退出,1:儿子, 2:女儿, 3:父亲"<<endl; cin>>choice; switch(choice) { case 0: quit=true; break; case 1: p = new son(); one(*p); break; case 2: p = new doc(); two(p); break; case 3: p=new father(); three(*p); } if(quit) { break; } } return 0; }
强制使用静态连接:
#include <iostream> using namespace std; class father{ public: virtual void hello(){cout<<"father hello"<<endl;} }; class son: public father{ public: void hello(){cout<<"son hello"<<endl;} }; class doc: public father{ public: void hello(){cout<<"doc hello"<<endl;} }; void one(father one) { one.hello(); } void two(father *two) { two->hello(); } void three(father &three) { three.hello(); } int main() { father *p=0; son a=son(); p=&a; p->father::hello();//强制使用静态连接 return 0; }
基类析构函数应声明为 virutal
#include <iostream> using namespace std; class father{ public: virtual void hello(){cout<<"father hello"<<endl;} father(){cout<<"father construct"<<endl;} //@warn virtual ~father(){cout<<"father destruct"<<endl;} //父类虚析构函数会自动调用子类虚析构函数 }; class son: public father{ public: void hello(){cout<<"son hello"<<endl;} son(){cout<<"son construct"<<endl;} ~son(){cout<<"son destruct"<<endl;} }; int main() { father *p = new son(); delete p; return 0; }
相关文章推荐
- 在C语言中的字符串
- 黑马程序员——C语言基础知识整理——printf函数与scanf函数
- 第14周项目4-处理C++源代码的程序
- 第十四周 【项目2-用文件保存的学生名单】若干名学生的学号 姓名和C++课、高数和英语成绩
- C++的4种类型转换关键字及其特点
- 注释转换 ——C注释转换为标准C++注释
- 从大学开始学C++到现在的一些感悟
- c++基础编程 之 string
- 【读书笔记:C++ primer plus 第六版 中文版】第4章 复合类型
- 双边滤波原理与C++实现
- c++学习-继承
- 第十五周oj刷题——Problem A: 长方柱类【C++ 类定义】
- 第十五周oj刷题——Problem B: 矩形类定义【C++】
- 第十五周oj刷题—— Problem C: 矩形类中运算符重载【C++】
- 第十五周oj刷题——Problem D: C++习题 对象数组输入与输出
- C/C++中关键字static的用法及作用
- 第十五周oj刷题——Problem E: C++习题 对象数组求最大值
- Uva1586
- 教你21天学会C++ (有图有真相)
- C++学习笔记之---中交换函数的几种写法与辨析