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 ...
随机推荐
- POJ1637 Sightseeing tour (混合图欧拉回路)(网络流)
Sightseeing tour Time Limit: 1000MS Me ...
- phpwind8.7升级9.0.1过程(一)本地和服务器数据同步的部署
在使用phpwind的过程中需要将整个网站论坛的模板从phpwind8.7升级到phpwind9.0.1 需要首先在本地搭建和服务器端一样的环境然后在本地尝试性升级之后,然后在服务器端进行升级. 以下 ...
- Android——复选按钮和开关按钮
复选按钮和开关按钮代码如下: <LinearLayout android:layout_width="match_parent" android:layout_height= ...
- 《剑指Offer》之替换空格
1.题目描述 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 2.代码实 ...
- memory dump and CLR Inside Out
MSDN Magazine: CLR Inside Out https://msdn.microsoft.com/en-us/magazine/cc501040.aspx CLR Inside Out ...
- MMU讲解
MMU是Memory Management Unit的缩写,中文名是内存管理单元,它是中央处理器(CPU)中用来管理虚拟存储器.物理存储器的控制线路,同时也负责虚拟地址映射为物理地址,以及提供硬件机制 ...
- 使用PHP+ajax打造聊天室应用
方法1. comet http://www.xiumu.org/technology/the-php-notes-comet-long-connection-instance.shtml 这篇文章 ...
- 001. 使用ssh连接不上centos 6.5的解决方法及其解决中文乱码
1. 使用ssh连接不上centos 6.5的解决方法: 错误显示: Connecting to 192.168.1.106:22... Could not connect to '192.168.1 ...
- Zend 安装 OpenExplorer插件
转自:http://blog.csdn.net/binyao02123202/article/details/8954249 OpenExplorer是一款打开导进来的项目文件或文件夹所在磁盘的位置的 ...
- 必须Mark!43个优秀的Swift开源项目推荐
摘要:拥有着苹果先天生态优势的Swift自发布以来,各种优秀的开源项目便层出不穷.本文作者站在个人的角度,将2014年Swift开源项目做了一个甄别.筛选,从工具.存储.网络.界面.框架到Demo以及 ...