c++学习-虚函数
#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 ;
}
静态联编:在编译时就确定指针指向的类
#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 ;
}
多态满足的条件:
#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=;
int choice; while(){
bool quit = false; cout<<"0:退出,1:儿子, 2:女儿, 3:父亲"<<endl;
cin>>choice; switch(choice)
{
case :
quit=true;
break;
case :
p = new son();
one(*p);
break;
case :
p = new doc();
two(p);
break;
case :
p=new father();
three(*p);
} if(quit)
{
break;
} } return ;
}
强制使用静态连接:
#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=;
son a=son();
p=&a;
p->father::hello();//强制使用静态连接 return ;
}
基类析构函数应声明为 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 ;
}
c++学习-虚函数的更多相关文章
- C++之多态性与虚函数
面向对象程序设计中的多态性是指向不同的对象发送同一个消息,不同对象对应同一消息产生不同行为.在程序中消息就是调用函数,不同的行为就是指不同的实现方法,即执行不同的函数体.也可以这样说就是实现了&quo ...
- [转]C++之多态性与虚函数
面向对象程序设计中的多态性是指向不同的对象发送同一个消息,不同对象对应同一消息产生不同行为.在程序中消息就是调用函数,不同的行为就是指不同的实现方法,即执行不同的函数体.也可以这样说就是实现了“一个接 ...
- C++虚函数与虚表
有一定面向对象知识的朋友对继承与多态一定很熟悉,C++想实现继承的话就要使用虚函数,那么什么是虚函数,其原理是什么,下面尽量给大家分析一下C++中其运行机制: 首先,基础,什么是虚函数,什么是多态? ...
- C++学习 - 虚表,虚函数,虚函数表指针学习笔记
http://blog.csdn.net/alps1992/article/details/45052403 虚函数 虚函数就是用virtual来修饰的函数.虚函数是实现C++多态的基础. 虚表 每个 ...
- (C/C++学习)5.C++中的虚继承-虚函数-多态解析
说明:在C++学习的过程中,虚继承-虚函数经常是初学者容易产生误解的两个概念,它们与C++中多态形成的关系,也是很多初学者经常产生困惑的地方,这篇文章将依次分别对三者进行解析,并讲述其之间的联系与不同 ...
- C++学习基础十二——纯虚函数与抽象类
一.C++中纯虚函数与抽象类: 1.含有一个或多个纯虚函数的类成为抽象类,注意此处是纯虚函数,而不是虚函数. 2.如果一个子类继承抽象类,则必须实现父类中的纯虚函数,否则该类也为抽象类. 3.如果一个 ...
- 【2016-10-13】【坚持学习】【Day4】【virtual 虚函数】
定义一个基类,有一个虚函数 定义三个子类,分别继承,重写,New,这个虚函数 abstract class Test { public virtual void Prinf() { Console ...
- C++学习25 纯虚函数和抽象类
在C++中,可以将成员函数声明为纯虚函数,语法格式为: ; 纯虚函数没有函数体,只有函数声明,在虚函数声明结尾加上=0,表明此函数为纯虚函数. 最后的=0并不表示函数返回值为0,它只起形式上的作用,告 ...
- C++学习23 虚函数详解
虚函数对于多态具有决定性的作用,有虚函数才能构成多态.上节的例子中,你可能还未发现虚函数的用途,不妨来看下面的代码. #include <iostream> using namespace ...
随机推荐
- 归并排序 空间复杂度为O(1)的做法
#include <iostream> #include <cstdlib> using namespace std; void print(int *arr, int sta ...
- 简单三步-实现dede站内搜索功能
第一步:找到对应的搜索模板的代码 我们都知道,dede有自带的搜索功能,我们只要找到对应的模板,然后把我们想要的代码拿出来就行了.具体如下: 首先进入templets-->default--&g ...
- curl网站开发指南
我一向以为,curl只是一个编程用的函数库. 最近才发现,这个命令本身,就是一个无比有用的网站开发工具,请看我整理的它的用法. =================================== ...
- APACHE 多站点配置方法
例如你的主机IP:192.168.1.8 而你有三个站点,域名为:www.111.com,www.222.com,www.333.com 相应的网站文件放在主机的:\website\111;D:\we ...
- $.ajax提交,后台接受到的值总是乱码?明天再总结
//首先说明,我的服务器和页面编码都是GBK,所以尝试了很多种GBK的方式前台:function printFunction(){ window.print(); $.ajax({ url : '/t ...
- JavaScript 札记(数据类型和变量、)
1. 变量名由:字母.下划线.$.数字组成,只可以字母.下划线.$开头. 2.JavaScript区分大小写! 3.命名规范:匈牙利命名法(不论是变量名还是函数名). 4.基本类型(3种):字符串.数 ...
- unity, sceneview 中拾取球体gizmos
http://answers.unity3d.com/questions/745560/handle-for-clickable-scene-objects.html http://www.jians ...
- OpenJudge计算概论-扩号匹配问题【这个用到了栈的思想】
/*====================================================================== 扩号匹配问题 总时间限制: 1000ms 内存限制: ...
- Python画图形界面
使用QTdesigner 生成.ui文件,输入命令行pyuic4 -o test.py test.ui 在生成的Python文件后面输入下面代码 if __name__=="__main__ ...
- Amoeba:开源的分布式数据库Porxy解决方案
http://www.biaodianfu.com/amoeba.html 什么是Amoeba? Amoeba(变形虫)项目,该开源框架于2008年 开始发布一款 Amoeba for Mysql软件 ...