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 方法. 它 ...
随机推荐
- VS2005、vs2008+WinXPDDK+DDKWizard配置驱动开发环境
所需软件下载地址如下(均为有效资源链接,速度都比较可以): vs2005: http://221.224.22.210/downloadsawyer/VS.Net2005简体中文版.rar wi ...
- Visual Studio 2012 和 SVN 结合实现版本控制 AnkhSvn
第一步: 安装VisualSVN Server Manager. 下载地址:http://www.onlinedown.net/soft/89603.htm 第二步: 安装TortoiseSVN.注意 ...
- C# 通过smtp服务器进行邮件发送 MailHelper
C# 通过smtp服务器进行邮件发送 MailHelper.cs using System; using System.Data; using System.Configuration; using ...
- 使用图片作为a标签的点击按钮时,当触发touchstart的时候,往往会有一个灰色的背景,想要去掉的话可以用下面这种方式
a,a:hover,a:active,a:visited,a:link,a:focus{ -webkit-tap-highlight-color:rgba(0,0,0,0); -webki ...
- CF371 D Searching Rectangles
基本思路就是二分 每条边分别二分求 c++11 用fflush(sdtout) 不行 囧啊 #include<bits/stdc++.h> using namespace std; int ...
- 【BZOJ3160】万径人踪灭(FFT,Manacher)
[BZOJ3160]万径人踪灭(FFT,Manacher) 题面 BZOJ 题解 很容易想到就是满足条件的子序列个数减去回文子串的个数吧... 至于满足条件的子序列 我们可以依次枚举对称轴 如果知道关 ...
- 【CJOJ2316】【模板】可持久化线段树
题面 Description 这是一道非常直白的可持久化线段树的练习题,目的并不是虐人,而是指导你入门可持久化数据结构. 线段树有个非常经典的应用是处理RMQ问题,即区间最大/最小值询问问题.现在我们 ...
- 解决Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project autotest_fchtgl: Compilation failure的方法
在碰到maven install 发现报错 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:comp ...
- python中的小知识点
这里是一些小知识点的汇集,为的是以后查找的方便. 行与缩进: 物理行:实际看到的代码行数. 逻辑行:在意义上的函数(即解释器执行的行数) 如果一个物理行中包含了多个逻辑行,则每个逻辑行之间需要用分号 ...
- 学习ASP.NET Core Razor 编程系列一
一. 概述 .NET Core 1.0发布的时候就想进行学习的,不过根据微软的以往的发布规律1.0版可以认为是大众测试版,2.0才算稳定.现在2.1都已经发布了预览版,之前对其"不稳定&qu ...