虚函数专题
来源:互联网 发布:swmm软件下载 编辑:程序博客网 时间:2024/06/18 15:47
结合笔记本
程序:
#include <iostream> using namespace std;class Base1 {public: virtual void display() const; //虚函数 };void Base1::display() const { cout << "Base1::display()" << endl;}class Base2:public Base1 {public: virtual void display() const;};void Base2::display() const { cout << "Base2::display()" << endl;}class Derived : public Base2 {public: virtual void display() const;};void Derived::display() const { cout << "Derived::display()" << endl;}void fun(Base1 *ptr) { ptr->display();}int main() { Base1 base1; Base2 base2; base1.display(); base2.display(); Derived derived; fun(&base1); fun(&base2); fun(&derived); return 0;}
#include <iostream>using namespace std;class Base{public: virtual void fun1() = 0; virtual void fun1(int) { } virtual void fun2() { } void fun2(int) { } void fun3(float) { }};class Derived :private Base{public: virtual void fun1() { cout << "This function is implemented to avoid to be a abstract class"; } void CallBaseFun2(int x) { Base::fun2(x); }};int main(){ Derived a; a.CallBaseFun2(3); a.fun2(); //incorrect cout << "Hello world!" << endl; return 0;}
#include <iostream>using namespace std;class Base{public:/* virtual void fun1() = 0;*/ void fun1() { cout << "爸爸1" << endl; } //virtual void fun2() //{ // cout << "爸爸2" << endl; //} //void fun2(int) //{ // cout << "爸爸2整形" << endl; //} //void fun3() //{ // cout << "爸爸3" << endl; //}};class Derived :public Base{public: void fun1() { cout << "女儿1" << endl; } //void fun3() //{ // cout << "女儿3" << endl; //} //void fun4() //{ // cout << "女儿4" << endl; //}};class DDerived :public Base{//public:public: // void fun1() //{ // cout << "二女儿1" << endl; //}};int main(){ //Derived a; //a.fun1(); //correct Derived:fun1(); ////a.fun1(8); //incorrect //a.fun2(); //correct Base:fun2() //a.fun2(3); //correct Base:fun2(int); //a.fun3(); //correct Derived:fun3() ////a.fun3(3.0); //incorrect //cout << "Hello world!" << endl; Base a; Derived nver; DDerived ernver; Base *ptr; ////ptr->fun3(); ////cout << ptr << endl; ptr = &nver; //cout << ptr << endl; ptr->fun1(); nver.fun1(); ptr = &ernver; //cout << ptr << endl; ptr->fun1(); nver.fun1(); //ptr = &er //ptr = &ernver; //cout << ptr << endl; //ptr->fun1(); return 0;}
阅读全文
0 0
- 虚函数专题
- 虚函数专题
- C++ 继承,虚函数与多态性专题
- 分析函数开发专题
- 函数专题:CASE WHEN
- Oracle 函数专题:TO_CHAR
- python 专题十六 函数
- 函数专题(简介)
- 母函数入门 专题
- 【专题】欧拉函数
- 【Python专题】使用函数
- PHP专题解剖--函数
- TensorFlow损失函数专题
- JavaScript函数专题
- 函数专题:分析函数 over
- DLL专题之DllMain函数
- 博弈专题·sg函数
- 专题:rand();函数的使用
- dubbo服务集群配置及负载均衡策略
- 面向对象编程进阶
- NoSQL数据库数据模型(笔记)
- 查询两个数组的中位数
- 求一棵二叉树的镜像
- 虚函数专题
- jQuery
- c++11 多线程基础
- React学习——JSX的使用
- EditPlus、Notepad++等编辑器选中列(块)的方法
- Ajax和JQuery的$.ajax
- (个人)AR电子书系统创新实训第四周(2)
- Ubuntu16.04搭建ssh 和 samba
- 数据库会自动清除掉超时的空闲连接造成中间件连接池中连接断开的问题