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 ...
随机推荐
- poj2912 带权并查集
题意:多个人玩石头剪刀布,每个人提前选定了自己出哪个手势,而其中有一种特殊的人他可以随意出什么手势,问是否能够从给出的一系列石头剪刀布游戏中判断出哪个是特殊的,可以从第几局游戏中判断出来. 首先按照食 ...
- 什么是HTML、XML和XHTML
(1)XMLXML是The Extensible Markup Language(可扩展标识语言)的简写.目前推荐遵循的是W3C于2000年10月6日发布的XML1.0,参考(www.w3.org/T ...
- 【LOI2005】【P1306】河流
树归题,本来比较简单,但是因为几个思想搞错了,所以卡了两天 原题: 几乎整个Byteland 王国都被森林和河流所覆盖.小点的河汇聚到一起,形成了稍大点的河.就这样,所有的河水都汇聚并流进了一条大河, ...
- 自我提升mysql
1.某字段更新 自增 1 update table set a=a+1 2.修改某一字段的数据类型 alter table "tablename" modify "co ...
- EXT2 文件系统
转自:http://www.cnblogs.com/ggjucheng/archive/2012/08/22/2651641.html#ext2_filesystem 认识ext文件系统 硬盘组成与分 ...
- socket模块
1 1.1 server: #!/use/local/env python# -*- coding:utf-8 -*- import socket ip_port = ('127.0.0.1', 99 ...
- IIS_PUT
[*] Put file success http://58.16.95.114:80/1470546504.01.txt [*] Put file success http://58.17.121. ...
- css设置透明度
使用新的CSS3的"RGBA"声明,不仅仅让我们像通常一样设置RGB颜色,而且还可以设置其透明度. RGBA像RGB一样设置颜色,而这个"A"--RGBA中的最 ...
- 虚拟化之kvm与xen对比
xen XenServer is the leading open source virtualization platform, powered by the Xen Project hypervi ...
- caller 属性和callee属性
1.caller 属性 返回一个对函数的引用,即调用了当前函数的函数体. functionName.caller :functionName 对象是所执行函数的名称. 说明: 对于函数来说,calle ...