C++—多态与继承
一、基本概念
#include <iostream>
#include <time.h>
using namespace std;
class B1
{
public:
B1(int i)
{
cout<<"constructing B1 "<<i<<endl;
}
};
class B2
{
public:
B2(int j)
{
cout<<"constructing B2 "<<j<<endl;
}
};
class B3
{
public:
B3()
{
cout<<"constructing B3"<<endl;
}
};
class C: public B2, public B1, public B3
{
public:
C(int a, int b, int c, int d):B1(a), memberB2(d), memberB1(c),B2(b)
{
}
private:
B1 memberB1;
B2 memberB2;
B3 memberB3;
};
int main()
{
C obj(1,2,3,4);
return 0;
}
#include <iostream>
#include <time.h>
using namespace std;
class B1
{
public:
B1(int i)
{
cout<<"constructing B1 "<<i<<endl;
}
~B1()
{
cout<<"destructing B1"<<endl;
}
};
class B2
{
public:
B2(int j)
{
cout<<"constructing B2 "<<j<<endl;
}
~B2()
{
cout<<"destructing B2"<<endl;
}
};
class B3
{
public:
B3()
{
cout<<"constructing B3"<<endl;
}
~B3()
{
cout<<"destructing B3"<<endl;
}
};
class C: public B2, public B1, public B3
{
public:
C(int a, int b, int c, int d):B1(a), memberB2(d), memberB1(c),B2(b)
{
}
private:
B1 memberB1;
B2 memberB2;
B3 memberB3;
};
int main()
{
C obj(1,2,3,4);
return 0;
}
#include <iostream>
#include <time.h>
using namespace std;
class B1
{
public:
int nV;
void fun()
{
cout<<"member of B1 "<<nV<<endl;
}
};
class B2
{
public:
int nV;
void fun()
{
cout<<"member of B2 "<<nV<<endl;
}
};
class D1: public B1, public B2
{
public:
int nV;
void fun()
{
cout<<"member of D1 "<<nV<<endl;
}
};
int main()
{
D1 d1;
d1.nV = 1;
d1.fun();
d1.B1::nV = 2;
d1.B1::fun();
d1.B2::nV = 3;
d1.B2::fun();
return 0;
}
#include <iostream>
#include <time.h>
using namespace std;
class B0
{
public:
int nV;
void fun()
{
cout<<"member of B0 "<<nV<<endl;
}
};
class B1:public B0
{
public:
int nV1;
};
class B2:public B0
{
public:
int nV2;
};
class D1:public B1, public B2
{
public:
int nVd;
void fund()
{
cout<<"member of D1"<<endl;
}
};
int main()
{
D1 d1;
d1.B1::nV = 2;
d1.B1::fun();
d1.B2::nV = 3;
d1.B2::fun();
return 0;
}
#include <iostream>
#include <time.h>
using namespace std; class B0
{
public:
int nV;
void fun()
{
cout<<"member of B0 "<<nV<<endl;
}
}; class B1:virtual public B0
{
public:
int nV1;
}; class B2:virtual public B0
{
public:
int nV2;
}; class D1:public B1, public B2
{
public:
int nVd;
void fund()
{
cout<<"member of D1"<<endl;
}
}; int main()
{
D1 d1;
d1.nV = 2;
d1.fun(); return 0;
}
#include <iostream>
#include <time.h>
using namespace std; class B0
{
public:
B0(int n)
{
nV = n;
}
int nV;
void fun()
{
cout<<"member of B0 "<<nV<<endl;
}
}; class B1:virtual public B0
{
public:
B1(int a):B0(a)
{
}
int nV1;
}; class B2:virtual public B0
{
public:
B2(int a):B0(a)
{
}
int nV2;
}; class D1:public B1, public B2
{
public:
D1(int a):B0(a), B1(a), B2(a)
{
}
int nVd;
void fund()
{
cout<<"member of D1"<<endl;
}
}; int main()
{
D1 d1(1);
d1.nV = 2;
d1.fun(); return 0;
}
才是真正的调用了B0构造函数。
#include <iostream>
#include <time.h>
using namespace std; class B0
{
public:
void display()
{
cout<<"B0::display()"<<endl;
}
}; class B1:public B0
{
public:
void display()
{
cout<<"B1::display()"<<endl;
}
}; class B2:public B0
{
public:
void display()
{
cout<<"B2::display()"<<endl;
}
}; void fun(B0 *ptr)
{
ptr->display();
} int main()
{
B0 b0;
B1 b1;
B2 b2;
fun(&b0);
b0 = b1;
fun(&b0);
b0 = b2;
fun(&b0); return 0;
}
输出结果为:
B0::display()
B0::display()
B0::display()
通过这种赋值兼容后,每次调用的同名函数都是基类的同名函数,如果想调用派生类的,则需要使用虚函数。
五、总结

C++—多态与继承的更多相关文章
- 深入理解OOP(四): 多态和继承(抽象类)
在本文中,我们讨论OOP中的热点之一:抽象类.抽象类在各个编程语言中概念是一致的,但是C#稍微有些不一样.本文中我们会通过代码来实现抽象类,并一一进行解析. 深入理解OOP(一):多态和继承(初期绑定 ...
- 深入理解OOP(三):多态和继承(动态绑定和运行时多态)
在前面的文章中,我们介绍了编译期多态.params关键字.实例化.base关键字等.本节我们来关注另外一种多态:运行时多态, 运行时多态也叫迟绑定. 深入理解OOP(一):多态和继承(初期绑定和编译时 ...
- 深入理解OOP(二):多态和继承(继承)
本文是深入浅出OOP第二篇,主要说说继承的话题. 深入理解OOP(一):多态和继承(初期绑定和编译时多态) 深入理解OOP(二):多态和继承(继承) 深入理解OOP(三):多态和继承(动态绑定和运行时 ...
- 深入理解OOP(第一天):多态和继承(初期绑定和编译时多态)
在本系列中,我们以CodeProject上比较火的OOP系列博客为主,进行OOP深入浅出展现. 无论作为软件设计的高手.或者菜鸟,对于架构设计而言,均需要多次重构.取舍,以有利于整个软件项目的健康构建 ...
- Python入门之面向对象的多态和继承
本章内容 Python面向对象的多态和继承对比 ========================================= 在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的 ...
- C++基础学习教程(七)----类编写及类的两个特性解析--->多态&继承
类引入 到眼下为止我们所写的自己定义类型都是keywordstruct,从如今起我们将採用class方式定义类,这样的方式对于学习过其它高级语言包含脚本(Such as Python)的人来说再熟悉只 ...
- .NET Core CSharp初级篇 1-6 类的多态与继承
.NET Core CSharp初级篇 1-6 本节内容为类的多态与继承 简介 终于讲到了面向对象三大特性中的两大特性--继承与多态.通过继承与多态,我们能很好的将类的拓展性发挥到了极致.在下面的内容 ...
- python极简教程07:封装、多态和继承
测试奇谭,BUG不见. 这一场主讲python的面向对象部分--封装.多态和继承. 目的:掌握Python面向对象的三个核心概念. 封装 01 什么是封装? 封装的目的是,保护隐私.通俗的讲:不想让别 ...
- 深入浅出OOP(三): 多态和继承(动态绑定/运行时多态)
在前面的文章中,我们介绍了编译期多态.params关键字.实例化.base关键字等.本节我们来关注另外一种多态:运行时多态, 运行时多态也叫迟绑定. 运行时多态或迟绑定.动态绑定 在C#语音中,运行时 ...
- 深入浅出OOP(一): 多态和继承(早期绑定/编译时多态)
在本系列中,我们以CodeProject上比较火的OOP系列博客为主,进行OOP深入浅出展现. 无论作为软件设计的高手.或者菜鸟,对于架构设计而言,均需要多次重构.取舍,以有利于整个软件项目的健康构建 ...
随机推荐
- VSFTPD匿名用户上传文件
1.安装vsftpd yum -y install vsftpd yum -y install ftp 客户端 2.编写配置文件 vim /etc/vsftpd/vsftpd.conf anonymo ...
- learning scala akka actorySystem create and close
package com.example import akka.actor.ActorSystem import scala.util.{Failure, Success} import scala. ...
- Android Studio3.0的下载及其安装详解加eclipse下载安装配置jdk9
关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 本篇文章同步微信公众号 欢迎大家关注我的微信公众号:「醉翁猫咪」 今天我们来讲解如何下载android studio 3.0及其 ...
- 转载:scala中的:+\+:\::\:::
原文链接:https://segmentfault.com/a/1190000005083578 初学Scala的人都会被Seq的各种操作符所confuse.下面简单列举一下各个Seq操作符的区别. ...
- linux高性能服务器编程 (一) --Tcp/Ip协议族
前言: 在学习swoole入门基础的过程中,遇到了很多知识瓶颈,比方说多进程.多线程.以及进程池和线程池等都有诸多的疑惑.之前也有学习相关知识,但只是单纯的知识面了解.而没有真正的学习他们的来龙去脉. ...
- PHP Closure(闭包)类详解
Closure 面向对象变成语言代码的复用主要采用继承来实现,而函数的复用,就是通过闭包来实现.这就是闭包的设计初衷. 注:PHP里面闭包函数是为了复用函数而设计的语言特性,如果在闭包函数里面访问指定 ...
- centos7最小化安装准备工作
1.配置网络 [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eno16780032 HWADDR=00:0C:29:48:9 ...
- 【大数据】分布式文件系统HDFS 练习
作业要求来自于https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3292 利用Shell命令与HDFS进行交互 以”./bin/dfs d ...
- js十大排序算法收藏
十大经典算法排序总结对比 转载自五分钟学算法&https://www.cnblogs.com/AlbertP/p/10847627.html 一张图概括: 主流排序算法概览 名词解释: n: ...
- Maven中依赖的scope的依赖范围
在Maven中依赖的域有这几个:import.provided.runtime.compile.system.test 1compile 的范围 当依赖的scope为compile的时候,那么当前这个 ...