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 ...
随机推荐
- SWIFT语言的概览
Swift用来写iOS和OS X程序.(估计也不会支持其它屌丝系统) Swift吸取了C和Objective-C的优点,且更加强大易用. Swift可以使用现有的Cocoa和Cocoa Touch框架 ...
- poj1420 拓扑序
题意:给出一个表格,一部分单元格是给定的数字,而另一部分单元格则是一个式子,表示是其他一些单元格的和,让你输出最后计算出的所有格子的数. 因为有些格子需要其他格子先计算出来,所以计算顺序是按照拓扑序的 ...
- c3p0操作MySQL数据库
使用c3p0连接MySQL数据库并对MySQL数据库进行基本操作. 1. [文件] 数据库准备 ~ 226B 下载(64) ? 1 2 3 4 5 6 7 8 9 10 ##创建数据库 ...
- Java 学习
effective+java第三版 2016-09-23 15:25 effective+java第三版 相关问答 EffectiveJava第28条中关于泛型的一个疑问? @又名耶稣谢邀 一.先说说 ...
- Redis GEO ,GEOHASH,Spatial_index
https://matt.sh/redis-geo http://antirez.com/latest/0 http://invece.org/ https://github.com/davidmot ...
- memcache中的add和set方法区别
相信大家对memcache都不陌生,在项目中也经常使用memcache作为缓存方案,那么在使用过程中有没有发现为什么memcahce有两个添加缓存的方法:一个是add,一个是set,那么你知道这2个方 ...
- [转]easyui常用控件及样式API中文收藏
[转]easyui常用控件及样式收藏 2013-05-06 23:01 30612人阅读 评论(0) 收藏 举报 分类: java ee(5) 目录(?)[+] CSS类定义: div easyu ...
- Linux静态库和共享库
1.什么是静态库静态库类似windows中的静态lib 关于windows中的静态lib,可参考 Windows动态链接库DLL 特点:包含函数代码声明和实现,链接后所有代码都嵌入到宿主程序中. 只在 ...
- OpenJudge计算概论-矩阵归零消减序列和
矩阵归零消减序列和 总时间限制: 1000ms 内存限制: 65536kB 描述 给定一个n*n的矩阵( <= n <= ,元素的值都是非负整数).通过n-1次实施下述过程,可把这个矩阵转 ...
- no ocijdbc11 in java.library.path linux
no ocijdbc11 in java.library.path linux vi /etc/profile export ORACLE_HOME=/oracle/database/oracle/p ...