强制转换父类对象为子类

#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. Oracle学习系列2

    SQL语法练习: 1,选择部门30中的所有员工 select * from emp where deptno=30; 2,列出办事员的姓名,编号和部门编号 select ename, empno, d ...

  2. spring beans源码解读之--Bean的定义及包装

    bean的定义,包装是java bean的基础.再怎么强调它的重要性都不为过,因此深入 了解这块的代码对以后的代码研究可以起到事半功倍的功效. 1. Bean的定义BeanDefinition 1.1 ...

  3. awesome-very-deep-learning

    awesome-very-deep-learning is a curated list for papers and code about implementing and training ver ...

  4. javascript输出图的简单路径

    <script> //图的构建 function vnode() { this.visited=0; this.vertex=0; this.arcs=new Array(); } fun ...

  5. set and Sequence theory

    https://en.wikipedia.org/wiki/Class_(set_theory) https://en.wikipedia.org/wiki/Zermelo%E2%80%93Fraen ...

  6. SQLServer2014内存优化表评测

    SQLServer2014内存优化表评测 分类: SQL内存表2014-06-20 11:49 1619人阅读 评论(11) 收藏 举报 目录(?)[-] SQLServer2014的使用基本要求 内 ...

  7. CSS中:display:none与visible:hidden的区别

    display:none视为不存在且不加载,即,不为被隐藏的对象保留其物理空间,即该对象在页面上彻底消失. visibility:hidden隐藏,但在浏览时保留位置,即,使对象在网页上不可见,但该对 ...

  8. Android 如何全局获取Context

    有时,在处理业务逻辑的时候,需要Context对象,但在某些情况下,并非容易获取,这时就需要一些巧妙的手段来管理Context. 在Android中,提供了一个类Application,当应用程序启动 ...

  9. js正则及常用方法函数总结

    正则表达式作为一种匹配处理字符串的利器在很多语言中都得到了广泛实现和应用,web开发本质上是处理字符串(服务端接受请求处理后拼接字符串作为响应,这在早期的CGI编程中最明显,然后客户端解析字符串进行渲 ...

  10. selenium+python自动化之CSS定位

    一.css:属性定位 1.css可以通过元素的id.class.标签这三个常规属性直接定位到 2.如下是百度输入框的的html代码: <input id="kw" class ...