#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++学习-虚函数的更多相关文章

  1. C++之多态性与虚函数

    面向对象程序设计中的多态性是指向不同的对象发送同一个消息,不同对象对应同一消息产生不同行为.在程序中消息就是调用函数,不同的行为就是指不同的实现方法,即执行不同的函数体.也可以这样说就是实现了&quo ...

  2. [转]C++之多态性与虚函数

    面向对象程序设计中的多态性是指向不同的对象发送同一个消息,不同对象对应同一消息产生不同行为.在程序中消息就是调用函数,不同的行为就是指不同的实现方法,即执行不同的函数体.也可以这样说就是实现了“一个接 ...

  3. C++虚函数与虚表

    有一定面向对象知识的朋友对继承与多态一定很熟悉,C++想实现继承的话就要使用虚函数,那么什么是虚函数,其原理是什么,下面尽量给大家分析一下C++中其运行机制: 首先,基础,什么是虚函数,什么是多态? ...

  4. C++学习 - 虚表,虚函数,虚函数表指针学习笔记

    http://blog.csdn.net/alps1992/article/details/45052403 虚函数 虚函数就是用virtual来修饰的函数.虚函数是实现C++多态的基础. 虚表 每个 ...

  5. (C/C++学习)5.C++中的虚继承-虚函数-多态解析

    说明:在C++学习的过程中,虚继承-虚函数经常是初学者容易产生误解的两个概念,它们与C++中多态形成的关系,也是很多初学者经常产生困惑的地方,这篇文章将依次分别对三者进行解析,并讲述其之间的联系与不同 ...

  6. C++学习基础十二——纯虚函数与抽象类

    一.C++中纯虚函数与抽象类: 1.含有一个或多个纯虚函数的类成为抽象类,注意此处是纯虚函数,而不是虚函数. 2.如果一个子类继承抽象类,则必须实现父类中的纯虚函数,否则该类也为抽象类. 3.如果一个 ...

  7. 【2016-10-13】【坚持学习】【Day4】【virtual 虚函数】

    定义一个基类,有一个虚函数 定义三个子类,分别继承,重写,New,这个虚函数   abstract class Test { public virtual void Prinf() { Console ...

  8. C++学习25 纯虚函数和抽象类

    在C++中,可以将成员函数声明为纯虚函数,语法格式为: ; 纯虚函数没有函数体,只有函数声明,在虚函数声明结尾加上=0,表明此函数为纯虚函数. 最后的=0并不表示函数返回值为0,它只起形式上的作用,告 ...

  9. C++学习23 虚函数详解

    虚函数对于多态具有决定性的作用,有虚函数才能构成多态.上节的例子中,你可能还未发现虚函数的用途,不妨来看下面的代码. #include <iostream> using namespace ...

随机推荐

  1. stl中双向队列用法

    双向队列的操作如下: d[i]:返回d中下标为I的元素的引用. d.front():返回的一个元素的引用. d.back():返回最后一个元素的引用. d.pop_back():删除尾部的元素.不返回 ...

  2. shell下的作业管理[转]

    作业管理 举例来说,我们在登陆 bash 后, 想要一边复制文件.一边进行数据搜寻.一边进行编译,还可以一边进行 vi 程序撰写! 当然我们可以重复登陆那六个文字介面的终端机环境中,不过,能不能在一个 ...

  3. 端口映射工具--socat

    需求 有些服务器没有公网IP, 在有公网IP的服务器上把端口映射出去. 这样的需求有很多实现, 我比较钟爱socat, 简单就是美. 示例脚本新建一个文件叫intershow.sh /usr/loca ...

  4. max_input_vars 的影响

    一同事,让帮忙解决问题:post了1020条数据,结果只显示250条. 判断可能是php的post设置问题,结果发现php.ini里关于post的设置没有问题. 通过 php://input 得到请求 ...

  5. line-hieght与vertical-align的区别与联系

    7.3 line-height 行高指的是文本行的基线间的距离,但是文本之间的空白距离不仅仅是行高决定的, 同时也受字号的影响. 7.3.1 语 法 line-height属性的具体定义列表如下: 语 ...

  6. 委托,C#本身的委托(Action Func)

    1.Action 分为带泛型的和不带泛型的,带泛型可传入任何类型的参数. 格式如下: using System; using System.Collections.Generic; using Sys ...

  7. asp.net之ajax

    ajax主要的作用是无刷新的情况进行提交. 常用于客户端组件信息的提交.服务器组件在asp.net中能够正常的提交,而html组件则不能正常提交,在此情况下,就可以采用jquery的方式进行数据的异步 ...

  8. sql字符串插入函数STUFF

    STUFF (Transact-SQL) SQL Server 2012 其他版本 此主题尚未评级 - 评价此主题 <?XML:NAMESPACE PREFIX = "[default ...

  9. CSS控制print打印样式

    来源:http://blog.csdn.net/pangni/article/details/6224533 一.添加打印样式 1. 为屏幕显示和打印分别准备一个css文件,如下所示:   用于屏幕显 ...

  10. Mac安装Appium

    通过淘宝镜像安装 npm --registry http://registry.cnpmjs.org install -g appium 如果提示权限不够的话,需要加sudo,并输入对应密码 sudo ...