c++ --> 父类与子类间的继承关系
父类与子类间的继承关系
一、父类与子类
父类与子类的相互转换
class father{
//
};
class son : public father{
//
};
int main()
{
father f;
son s;
f = s;//正确
s = f;//错误
father *pf = new son;//正确
son *ps = new father;//错误
father &rf = s;//正确
father &rs = f;//错误
return ;
}
继承关系对基类成员的影响
| 公有成员 | 保护成员 | 私有成员 | |
| 公有继承 | 公有 | 保护 | 不可访问 |
| 保护继承 | 保护 | 保护 | 不可访问 |
| 私有继承 | 私有 | 私有 | 不可访问 |
| 成员函数 | 1 | 1 | 1 |
| 对象 | 1 | 0 | 0 |
| 子类 | 1 | 1 | 0 |
class father
{
public:
father(){cout<<"father construct"<<endl;}
~father(){cout<<"father delete"<<endl;}
};
class son : public father
{
public:
son(){cout<<"son construct"<<endl;}
~son(){cout<<"son delete"<<endl;}
};
int main()
{
son s;
return ;
}
输出:
father construct
son construct
son delete
father delete
2.如果是多重继承,基类的构造顺序按给定的顺序,析构反之
class father
{
public:
father(){cout<<"father construct"<<endl;}
~father(){cout<<"father delete"<<endl;}
};
class mother
{
public:
mother(){cout<<"mother construct"<<endl;}
~mother(){cout<<"mother delete"<<endl;}
};
class son : public father, public mother
{
public:
son(){cout<<"son construct"<<endl;}
~son(){cout<<"son delete"<<endl;}
};
int main()
{
son s;
return ;
}
输出:
father construct
mother construct
son construct
son delete
mother delete
father delete
3.利用基类的构造函数构造子类,效率更高
class father
{
int x;
public:
father(int a):x(a){cout<<"father construct:"<<x<<endl;}
};
class son : public father
{
int y;
public:
son(int a, int b):father(a), y(b){cout<<"son construct:"<<y<<endl;}
};
int main()
{
son s(, );
return ;
}
输出:
father construct:
son construct:
三、多重继承
1.多重继续的二义性,根本原因是

假如A有Test(),则B和C都有Test(),于是D产生了二义性
class A
{
public:
void Test(){cout<<"A"<<endl;}
};
class B
{
public:
void Test(){cout<<"B"<<endl;}
};
class C : public A, public B
{
};
int main()
{
C c;
c.Test(); //错误
c.A::Test(); //正确,输出:A
c.B::Test(); //正确,输出:B
return ;
}
2.编译器通常都是从离自己最近的目录树向上搜索的
子类的Test()覆盖了基类的Test(),并不代表基类的Test()消失,只是不能直接访问
class A
{
public:
void Test(){cout<<"A"<<endl;}
};
class B
{
public:
void Test(){cout<<"B"<<endl;}
};
class C : public A, public B
{
void Test(){cout<<"C"<<endl;}
};
int main()
{
C c;
c.Test(); //正确,输出:C
c.A::Test(); //正确,输出:A
c.B::Test(); //正确,输出:B
return ;
}
c++ --> 父类与子类间的继承关系的更多相关文章
- python的父类和子类中关于继承的不同版本的写法
Python 2.7中的继承 在Python 2.7中,继承语法稍有不同,ElectricCar 类的定义类似于下面这样: class Car(object): def __init__(self, ...
- Java awt组件间的继承关系
Container的继承关系: Window是可独立存在的容器,其他则不行.
- PythonI/O进阶学习笔记_4.自定义序列类(序列基类继承关系/可切片对象/推导式)
前言: 本文代码基于python3 Content: 1.python中的序列类分类 2. python序列中abc基类继承关系 3. 由list的extend等方法来看序列类的一些特定方法 4. l ...
- C++反汇编第四讲,反汇编中识别继承关系,父类,子类,成员对象
C++反汇编第四讲,反汇编中识别继承关系,父类,子类,成员对象 讲解目录: 1.各类在内存中的表现形式 备注: 主要复习开发知识,和反汇编没有关系,但是是理解反汇编的前提. 2.子类继承父 ...
- C++反汇编第三讲,反汇编中识别继承关系,父类,子类,成员对象
讲解目录: 1.各类在内存中的表现形式 备注: 主要复习开发知识,和反汇编没有关系,但是是理解反汇编的前提. 2.子类继承父类 2.1 子类中有虚函数,父类中有虚函数 : 都有的情况下 ...
- Python父类和子类关系/继承
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @File:继承_子类和父类的关系.py @E-mail:364942 ...
- java子类继承关系
1.java的类按照继承关系的树形结构所有的类其根节点都是object类,一个类有两种重要的成员,一是变量 .二是方法.子类继承不能继承父类中被声明为private的变量和方法. public cla ...
- 转:Java中子类是否可以继承父类的static变量和方法而呈现多态特性
原文地址:Java中子类是否可以继承父类的static变量和方法而呈现多态特性 静态方法 通常,在一个类中定义一个方法为static,那就是说,无需本类的对象即可调用此方法,关于static方法,声明 ...
- Java中子类是否可以继承父类的static变量和方法而呈现多态特性
静态方法 通常,在一个类中定义一个方法为static,那就是说,无需本类的对象即可调用此方法,关于static方法,声明为static的方法有以下几条限制: 它们仅能调用其他的static 方法. 它 ...
随机推荐
- Ubuntu 11.10下GRUB 2 1.99版编译安装笔记
Ubuntu 11.10下GRUB 2 1.99版编译安装笔记 以下的安装笔记,都是QLi自己学习grub2 时,所整理的,还是新手,有错误的话,请大家帮忙就别提出来了. 最新版grub V1.99官 ...
- 八爪鱼采集器︱爬取外网数据(twitter、facebook)
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 要想采集海外数据有两种方式:云采集+单机采集. ...
- 【mysql】连接查询
- HighCharts中的Ajax请求的2D折线图
HighCharts中的Ajax请求的2D折线图 设计源码: <!DOCTYPE html> <html> <head> <meta charset=&quo ...
- CF370 D Memory and Scores
dp题 并运用了前缀和 我看题目提示中有fft 我想了下感觉复杂度不过关还是未解 #include<bits/stdc++.h> using namespace std; typedef ...
- js中的0就是false,非0就是true及案例
在处理js代码判断真假时经常会这么写. 但fun()可能得到的是数字0,这可不是表示的没有值,但是!js中的数字0就是false,非0就是true. 于是0就被无情的当做false了. 已经被这个坑过 ...
- Tomcat下使用C3P0配置JNDI数据源(在项目的META-INF目录下创建context.xml的文件)
一.C3P0下载 C3P0下载地址:http://sourceforge.net/projects/c3p0/files/?source=navbar 下载完成之后得到一个压缩包
- Python Web-第六周-JSON and the REST Architecture(Using Python to Access Web Data)
1.JavaScript Object Notation JSON 1.JSON 官方介绍 http://www.json.org/json-zh.html 2.json1.py import jso ...
- Python Web-第二周-正则表达式(Using Python to Access Web Data)
0.课程地址与说明 1.课程地址:https://www.coursera.org/learn/python-network-data/home/welcome 2.课程全名:Using Python ...
- 【BZOJ4653】【NOI2016】区间(线段树)
[BZOJ4653][NOI2016]区间(线段树) 题面 BZOJ 题解 \(NOI\)良心送分题?? 既然是最大长度减去最小长度 莫名想到那道反复减边求最小生成树 从而求出最小的比值 所以这题的套 ...