强制转换父类对象为子类

#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++学习-多态性的更多相关文章

  1. C++中多态性学习(上)

    多态性学习(上) 什么是多态? 多态是指同样的消息被不同类型的对象接收时导致不同的行为.所谓消息是指对类的成员函数的调用,不同的行为是指不同的实现,也就是调用了不同的函数.虽然这看上去好像很高级的样子 ...

  2. C++学习之路—多态性与虚函数(二)纯虚函数与抽象类

    (根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 1    纯虚函数 在前面的博客中已经提到:有时 ...

  3. C++学习之路—多态性与虚函数(一)利用虚函数实现动态多态性

    (根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 多态性是面向对象程序设计的一个重要特征.顾名思义 ...

  4. 【Java学习笔记之二十四】对Java多态性的一点理解

    面向对象编程有三大特性:封装.继承.多态. 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对外界而已它的内部细节是隐藏的,暴露给外界的只是它的访问方法. 继承 ...

  5. C++学习笔记45:多态性

    运算符重载 运算符是针对新类型数据的实际需要,对原有运算符进行适当的改造 1.比如使复数类的对象可以使用+运算符实现加法: 2.比如使时钟类的对象可以用++运算符实现时间增加1秒: 注意:可以重载为类 ...

  6. C++学习7-面向对象编程基础(多态性与虚函数、 IO文件流操作)

    多态 多态性是指对不同类的对象发出相同的消息将返回不同的行为,消息主要是指类的成员函数的调用,不同的行为是指不同的实现: 函数重载 函数重载是多态性的一种简单形式,它是指允许在相同的作用域内,相同的函 ...

  7. Java学习:面向对象三大特征:封装性、继承性、多态性之多态性。

    面向对象三大特征:封装性.继承性.多态性之多态性. extends继承或者implemens实现,是多态性的前提. 例如:小菜是一个学生,但同时也是一个人.小菜是一个对象,这个对象既有学生形态,也有人 ...

  8. Java学习:面向对象的三大特征:封装性、继承性、多态性之继承性

    面向对象的三大特征:封装性.继承性.多态性. 继承 继承是多态的前提 ,如果没有继承,就没有多态. 继承主要解决的问题就是:共性抽取. 继承关系当中的特点: 子类可以拥有父类的“内容” 子类还可以拥有 ...

  9. 第2课第4节_Java面向对象编程_多态性_P【学习笔记】

    摘要:韦东山android视频学习笔记  面向对象程序的三大特性之继承性: 1.向上转换:只能定义被子类覆写的方法,不能调用在子类中定义的方法. class Father { private int ...

随机推荐

  1. Linux驱动设计——阻塞和同步

    阻塞和非阻塞是设备访问的两种基本方式,阻塞和非阻塞驱动程序使用时,经常会用到等待队列. 阻塞和非阻塞 阻塞操作是指在执行设备操作时,若不能获得资源,则挂起进程直到满足可操作的条件后再进行操作.被挂起的 ...

  2. 自定义android程序一段时间无操作后的功能

    项目中遇见一个这样的需求,就是当软件在一定时间没有操作时候需要弹出广告页面,当点击广告页面时又进行软件操作,也就是广告要在软件打开并且处于未操作状态才会出来. 方法一:用handler+onTouch ...

  3. why does txid_current() assign new transaction-id?

    Naoya: Hi,hackers! I have a question about txid_current(). it is "Why does txid_current() assig ...

  4. syslog-ng 安装

    下载 Syslog-NG的rpm包,  地址 http://www.kevindeng.org/wp-content/uploads/2010/10/Syslog-NG.zip unzip解压 [ro ...

  5. SVG 箭头线绘制

    SVG并没有提供原生的Arrow标签,这就需要自己的组合了,通过marker标签和path标签可以完美的模仿出箭头线,无论需要多少个箭头线,只需引用同一个marker即可: <svg id=&q ...

  6. sql复制表

    SELECT INTO 和 INSERT INTO SELECT 两种表复制语句       Insert是T-sql中常用语句,Insert INTO table(field1,field2,... ...

  7. 【转】PHP简单拦截器的实现

    最近在看Yii的源代码,收获了不少,这里就是从中得到的启发,而写的一个简单拦截器的实现下面看例子: <?phpclass A{    private $_e = array();       p ...

  8. JQuery Uplodify上传附件(同一个页面多个uplodify控件解决方案)

    功能描述:实现同一页面中多个不同附件的上传保存,且做到最大程度的减少代码修改量(最大程度的公用),为了方便实现垃圾数据和垃圾文件的处理,项目采用临时文件夹的方式:即:文件自动上传先保存到临时文件夹下, ...

  9. mysql将字符转换成数字

    在操作mysql时,经常需要将字符转换成数字,这一步虽然简单,但不常用的话也很容易忘记,现将在网上找到的方法记录如下: 1.将字符的数字转成数字,比如'0'转成0可以直接用加法来实现例如:将pony表 ...

  10. mysql sys table

    本文详细地介绍了MySQL 5.7新引入的sys schema.首先,本文概要地介绍了sys schema的作用和定位:其次,分别介绍了sys schema中的视图.函数和存储过程:接下来,通过两个例 ...