c++学习-多态性
强制转换父类对象为子类
#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; class father{ public:
void smart(){}
virtual ~father(){}
}; class son : public father
{
public:
void say()
{
cout << "say" << endl;
}
}; void main()
{ father *p;
p= new son;
dynamic_cast<son*>(p)->say(); }
多重继承
#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; class father{ public:
father(){ cout << "father cons" << endl; }
virtual ~father(){ cout << "father des" << endl; }
virtual void say()
{
cout << "father say" << endl;
}
}; class mother{ public:
mother(){ cout << "mother cons" << endl; }
virtual ~mother(){ cout << "mother des" << endl; }
virtual void run()
{
cout << "mother run" << endl;
}
}; class son : public mother, public father
{
public:
void run()
{
cout << "son run" << endl;
} son(){ cout << "son cons" << endl; }
~son(){ cout << "son des" << endl; } }; void main()
{ mother *p; p = new son;
p->run(); delete p; //dynamic_cast<son*>(p)->say(); }
同时访问 父类和母类中的函数:
模拟抽象类
#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; class human{
public:
virtual void say(){}
virtual void run(){}
virtual ~human(){ cout << "human des" << endl; }
human(){ cout << "human cons" << endl; }
}; class father:virtual public human{ //虚基类 public:
father(){ cout << "father cons" << endl; }
~father(){ cout << "father des" << endl; }
void say()
{
cout << "father say" << endl;
}
}; class mother :virtual public human{ public:
mother(){ cout << "mother cons" << endl; }
~mother(){ cout << "mother des" << endl; }
void run()
{
cout << "mother run" << endl;
}
}; class son : public mother, public father
{
public: son(){ cout << "son cons" << endl; }
~son(){ cout << "son des" << endl; }
}; void main()
{ human *p; p = new son; delete p; }
抽象类:
#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; class human{
public:
virtual void say() = ;//纯虚函数
virtual void run() = ;
virtual ~human(){ cout << "human des" << endl; }
human(){ cout << "human cons" << endl; }
}; class father:virtual public human{ //虚基类 public:
father(){ cout << "father cons" << endl; }
~father(){ cout << "father des" << endl; }
void say()
{
cout << "father say" << endl;
}
}; class mother :virtual public human{ public:
mother(){ cout << "mother cons" << endl; }
~mother(){ cout << "mother des" << endl; }
void run()
{
cout << "mother run" << endl;
}
}; class son : public mother, public father
{
public: son(){ cout << "son cons" << endl; }
~son(){ cout << "son des" << endl; }
}; void main()
{
human *p; p = new son;
p->say();
delete p; }
抽象类实例:
#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; class alpha{
public:
virtual void ee(){};
virtual ~alpha(){ cout << "alpha des" << endl; };
alpha(){ cout << "alpha cons" << endl; };
}; class A: public alpha{
public:
void ee(){ cout <<"A"<< endl; }
A(){ cout <<"A cons"<< endl; };
~A(){ cout << "A des" << endl; }; }; class B : public A{
public:
void ee(){ cout << "B" << endl; }
~B(){}
}; void main()
{
alpha *p; p = new A;
p->ee(); p = new B;
p->ee(); delete p; }
c++学习-多态性的更多相关文章
- C++中多态性学习(上)
多态性学习(上) 什么是多态? 多态是指同样的消息被不同类型的对象接收时导致不同的行为.所谓消息是指对类的成员函数的调用,不同的行为是指不同的实现,也就是调用了不同的函数.虽然这看上去好像很高级的样子 ...
- C++学习之路—多态性与虚函数(二)纯虚函数与抽象类
(根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 1 纯虚函数 在前面的博客中已经提到:有时 ...
- C++学习之路—多态性与虚函数(一)利用虚函数实现动态多态性
(根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 多态性是面向对象程序设计的一个重要特征.顾名思义 ...
- 【Java学习笔记之二十四】对Java多态性的一点理解
面向对象编程有三大特性:封装.继承.多态. 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对外界而已它的内部细节是隐藏的,暴露给外界的只是它的访问方法. 继承 ...
- C++学习笔记45:多态性
运算符重载 运算符是针对新类型数据的实际需要,对原有运算符进行适当的改造 1.比如使复数类的对象可以使用+运算符实现加法: 2.比如使时钟类的对象可以用++运算符实现时间增加1秒: 注意:可以重载为类 ...
- C++学习7-面向对象编程基础(多态性与虚函数、 IO文件流操作)
多态 多态性是指对不同类的对象发出相同的消息将返回不同的行为,消息主要是指类的成员函数的调用,不同的行为是指不同的实现: 函数重载 函数重载是多态性的一种简单形式,它是指允许在相同的作用域内,相同的函 ...
- Java学习:面向对象三大特征:封装性、继承性、多态性之多态性。
面向对象三大特征:封装性.继承性.多态性之多态性. extends继承或者implemens实现,是多态性的前提. 例如:小菜是一个学生,但同时也是一个人.小菜是一个对象,这个对象既有学生形态,也有人 ...
- Java学习:面向对象的三大特征:封装性、继承性、多态性之继承性
面向对象的三大特征:封装性.继承性.多态性. 继承 继承是多态的前提 ,如果没有继承,就没有多态. 继承主要解决的问题就是:共性抽取. 继承关系当中的特点: 子类可以拥有父类的“内容” 子类还可以拥有 ...
- 第2课第4节_Java面向对象编程_多态性_P【学习笔记】
摘要:韦东山android视频学习笔记 面向对象程序的三大特性之继承性: 1.向上转换:只能定义被子类覆写的方法,不能调用在子类中定义的方法. class Father { private int ...
随机推荐
- hdu1025 dp(最长上升子序列LIS)
题意:有一些穷国和一些富国分别排在两条直线上,每个穷国和一个富国之间可以建道路,但是路不能交叉,给出每个穷国和富国的联系,求最多能建多少条路 我一开始在想有点像二分图匹配orz,很快就发现,当我把穷国 ...
- 复利计算- 结对2.0--复利计算WEB升级版
客户在大家的引导下,有了更多的想法: 这个数据我经常会填.....帮我预先填上呗?...... 把界面做得简单漂亮好操作一点呗? 能不能帮我转成个APP,我装到手机上就更方便了? 我觉得这个很有用,很 ...
- Python中strip()函数
在python API中这样解释strip()函数:
- nginx实现日志按天切割
背景 nginx的日志文件没有rotate功能.一段时间过后,日志将越发臃肿,一个accesslog很快就突破1G,因此有必要通过脚本实现按天切割日志. 解决思路 1 重命名日志文件,如更改为acc ...
- Linux-Hostname-details
转自:http://www.cnblogs.com/kerrycode/p/3595724.html 当我觉得对Linux系统下修改hostname已经非常熟悉的时候,今天碰到了几个个问题,这几个问题 ...
- elipse 调试 反射 invoke 子类
真实案例: 调试一个接口,子类invoke的,结果断点断不到: 查找两个项目间的关联.依赖,无果. 问人吧,结果是配置文件没改成本机: #============================# # ...
- Webproject 每次运行都停到workerDone(this); tomcat调试
那是因为你是Debug调试,你要将Dubug的时间设置长一些; 设置步骤: window-> preferences -> java -> debug -> commu ...
- mysqldump: Got error: 1556: You can't use locks with log tables. when using LOCK TABLES
mysqldump: Got error: 1556: You can't use locks with log tables. when using LOCK TABLES 我是把一些mysqldu ...
- HBase(六): HBase体系结构剖析(上)
HBase隶属于hadoop生态系统,它参考了谷歌的BigTable建模,实现的编程语言为 Java, 建立在hdfs之上,提供高可靠性.高性能.列存储.可伸缩.实时读写的数据库系统.它仅能通过主键( ...
- NAT,网络地址转换详解
这个技术,是一个非常成熟的技术了,但是,为了将其弄得清楚点,体系点,也为了备忘,还是有必要在这里梳理一下! NAT:Network Address Translation. 这个主要是用在网络地址(I ...