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 ...
随机推荐
- Android——多线程编程练习题
随便选择两个城市作为预选旅游目标.实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市.分别用Runnable接口和Thread类实 ...
- Java基础了解
今天刚开始学习Java,除了老师讲的之外,又进一步上网去了解了下Java的相关知识: Java语言的主要特点: 1. 跨平台性 所谓的跨平台性,是指软件可以不受计算机硬件和操作系统的约束而在任意计算机 ...
- hdu3231 拓扑序
题意:在空间内有多个长方体,有一系列关系,分别是 A 的所有点的 x 坐标都比 B 的所有点的 x 坐标小, A 的所有点的 y 坐标都比 B 的所有点的 y 坐标小, A 的所有点的 z 坐标都比 ...
- C# WinForm获取当前路径汇总
摘自:http://greatverve.cnblogs.com/archive/2011/12/15/winform-path.html Winform获取应用程序的当前路径的方法集合,具体如下,值 ...
- GDB调试器
/*this project used for gdb debug c programs*//*At first,using compile command turn out the executab ...
- js获取客户端操作系统
function detectOS() { var sUserAgent = navigator.userAgent; var isWin = (navigator.platform == " ...
- MySQL-python模块
1. Python 操作 Mysql 模块的安装 linux: pip install MySQL-python 或 yum -y install MySQL-python windows: exe ...
- ECLIPSE中添加TPTP插件
转自:http://blog.csdn.net/sinboy/article/details/1536625 程序在实际应用当中,大数据量时对系统本身的影响是一个不得不面对的问题. 最早在使用Jbui ...
- jQuery在IE浏览器上的html()报错 return !noData || noData !== true && elem.getAttribute("classid") === noData;
解决方法是,将Query 1.10.2版本的源码中的3791行: return !noData || noData !== true && elem.getAttribute(&quo ...
- 解决Oracle安装完成后,随便输入用户名密码只要选择DBA身份就能登陆进去的问题
以sysdba身份登录既采用的是本地(系统)认证方式, 将%ORACLE_HOME%\network\admin\sqlnet.ora中的SQLNET.AUTHENTICATION_SERVICES= ...