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 ...
随机推荐
- js 获取地址栏参数
function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&] ...
- Android TextView换行问题
本文转载于:http://niufc.iteye.com/blog/1729792 ndroid的TextView在显示文字的时候有个问题就是一行还没显示满就跳到下一行,原因是: 1) TextVie ...
- Spring源码学习之:spring注解@Transactional
在分析深入分析@Transactional的使用之前,我们先回顾一下事务的一些基本内容. 事务的基本概念 先来回顾一下事务的基本概念和特性.数据库事务(Database Transaction) ,是 ...
- dir:一行代码,提取出所有视频文件名称及路径
某次,部门接到一个任务,要求对公司现有的视频文件资料做一个统计整理分类的工作. 领导召集开会,问:两周时间够用吗? 统计整理分类工作的第一步骤是把视频文件名称来源类别信息录入到 excel 表格中,才 ...
- pyzmq missing when running ipython notebook
Q: I can run iPython, but when I try to initiate a notebook I get the following error: ~ ipython not ...
- 转 -Linux 自检和 SystemTap (强大的内核调试工具)---包含下载地址
下载: http://www.oschina.net/p/systemtap/ https://sourceware.org/systemtap/ftp/releases/ Linux 自检和 S ...
- easyUI之layout
此组件与easyUI中的其它组件加载的方式相同,载为class方式和js方式,但此组件更多的是用class方式,其它的用js方式更为灵活.它继承panel组件及resize组件. 包括多个<di ...
- Entity Framework调用表值函数实现全文检索?
CREATE FUNCTION [dbo].[udf_BookContentSearch](@keywords NVARCHAR(1000))RETURNS @BookPageDetail TABLE ...
- MVC的异步,Entity Framework的异步,ADO.NET的异步,
MVC的异步化改造 无比轻松,只要把ActionResult改为async Task<AstionResult>: public async Task<ActionResult> ...
- python3生成标签云
标签云是现在大数据里面最喜欢使用的一种展现方式,其中在python3下也能实现标签云的效果,贴图如下: -------------------进入正文--------------------- 首先要 ...