1 继承中的访问权限问题

  • 所有继承方式, 子类都无法访问父类的private成员.

那么用如下测试代码尝试一下:

#include <iostream>
using namespace std;class father {
public:int m_father_public_value;
protected:int m_father_protected_value;
private:int m_father_private_value;
};class public_inherited_son : public father {
public:void public_inherited_son_visit_father_public_value() {cout << m_father_public_value << endl;}void public_inherited_son_visit_father_protected_value() {cout << m_father_protected_value << endl;}void public_inherited_son_visit_father_private_value() {cout << m_father_private_value << endl;}
};class protected_inherited_son: protected father {
public:void protected_inherited_son_visit_father_public_value() {cout << m_father_public_value << endl;}void protected_inherited_son_visit_father_protected_value() {cout << m_father_protected_value << endl;}void protected_inherited_son_visit_father_private_value() {cout << m_father_private_value << endl;}
};class private_inherited_son: private father {
public:void private_inherited_son_visit_father_public_value() {cout << m_father_public_value << endl;}void private_inherited_son_visit_father_protected_value() {cout << m_father_protected_value << endl;}void private_inherited_son_visit_father_private_value() {cout << m_father_private_value << endl;}
};int main() {return 0;
}

编译一下发现:

说明了子类不能访问父类的私有成员.

  • public继承, 父类的public和protected成员还是子类的public和protected成员.

试着用public继承的子类实例化的对象类外访问父类的public和protected成员, 如下测试代码:

int main() {public_inherited_son public_inherited_son_instance1;cout << public_inherited_son_instance1.m_father_public_value << endl;cout << public_inherited_son_instance1.m_father_protected_value << endl;return 0;
}

编译发现:

说明public继承中父类的public和protected成员在子类中还是public和protected成员, 且protected成员在类外不能访问.

  • protected继承, 父类的public和protected成员还是子类的protected成员.

试着用protected继承的子类实例化的对象类外访问父类的public和protected成员, 如下测试代码:

int main() {protected_inherited_son protected_inherited_son_instance1;cout << protected_inherited_son_instance1.m_father_public_value << endl;cout << protected_inherited_son_instance1.m_father_protected_value << endl;return 0;
}

编译发现:

父类的public和protected成员都不能在类外访问了, 说明都变成了protected成员.

  • private继承, 父类的public和protected成员成为子类的private成员.

就不测试了...

2 继承后的子类和父类的析构顺序问题

当继承后的子类实例化对象之后, 其对象所占用的内存空间中会保留一份父类的对象空间. 所以子类实例化对象的过程中, 其父类的构造函数也会被调用, 析构过程同理.

用如下代码测试一下其发生顺序:

#include <iostream>
using namespace std;class father {
public:father() {cout << "calling father's constructor" << endl;}~father() {cout << "calling father's disconstructor" << endl;}
};class public_inherited_son : public father {
public:public_inherited_son() {cout << "calling son's constructor" << endl;}~public_inherited_son() {cout << "calling son's disconstructor" << endl;}
};int main() {public_inherited_son public_inherited_son_instance1;return 0;
}

结果为:

说明了子类实例化对象的过程中会先调用父类的构造函数, 然后是子类的构造函数. 同时也表明在栈上的话析构顺序相反.

那么在堆区呢?

用如下代码测试:

#include <iostream>
using namespace std;class father {
public:father() {cout << "calling father's constructor" << endl;}~father() {cout << "calling father's disconstructor" << endl;}
};class public_inherited_son : public father {
public:public_inherited_son() {cout << "calling son's constructor" << endl;}~public_inherited_son() {cout << "calling son's disconstructor" << endl;}
};int main() {public_inherited_son *p_public_inherited_son_instance1 = new public_inherited_son;delete p_public_inherited_son_instance1;return 0;
}

运行结果:

总结:

子类实例化对象的过程会先调用父类的构造函数, 然后是子类的构造函数. 析构函数的顺序会反过来, 无论是栈上还是堆上.

2.1 调用父类构造函数想要传递参数方式

前面说到, 子类实例化对象时默认先调用父类构造函数, 再调用子类构造函数, 如果想要在调用父类构造函数时传递参数, 就需要显示的调用父类构造函数, 即在子类构造函数初始化列表上显示调用父类构造函数并传递参数.

2.2 C++11调用父类构造函数新方法

3 父类子类的同名成员处理方法

如果想要在子类对象访问父类同名成员, 须在成员名前加上父类名称作用域, 如下:

#include <iostream>
using namespace std;class father {
public:int member_value;
};class public_inherited_son : public father {
public:int member_value;
};int main() {public_inherited_son public_inherited_son_instance1;public_inherited_son_instance1.member_value = 10;public_inherited_son_instance1.father::member_value = 1;cout << public_inherited_son_instance1.member_value << endl;cout << public_inherited_son_instance1.father::member_value << endl;return 0;
}

结果如下:

需注意: 如果子类中存在同名成员函数, 子类成员函数会隐藏掉父类中所有的同名成员函数(包括所有的重载函数), 如以下测试代码:

#include <iostream>
using namespace std;class father {
public:void func1(int arg1) {cout << arg1 << endl;}void func1(int arg1, int arg2) {cout << arg1 << arg2 << endl;}
};class public_inherited_son : public father {
public:void func1(int arg1, int arg2, int arg3) {cout << arg1 << arg2 << arg3 << endl;}
};int main() {public_inherited_son public_inherited_son_instance1;public_inherited_son_instance1.func1(1);public_inherited_son_instance1.func1(1, 2);return 0;
}

编译发现:

如果想要使用父类函数的话, 需要加上父类作用域, 如下:

#include <iostream>
using namespace std;class father {
public:void func1(int arg1) {cout << arg1 << endl;}void func1(int arg1, int arg2) {cout << arg1 << arg2 << endl;}
};class public_inherited_son : public father {
public:void func1(int arg1, int arg2, int arg3) {cout << arg1 << arg2 << arg3 << endl;}
};int main() {public_inherited_son public_inherited_son_instance1;public_inherited_son_instance1.father::func1(1);public_inherited_son_instance1.father::func1(1, 2);return 0;
}

结果为:

C++中类和对象的一些注意事项 ---继承相关推荐

  1. C++中类和对象的一些注意事项

    1. struct和class的区别 默认的访问权限不同, struct默认访问权限是public, 而class的默认访问权限是private. 2. 构造析构函数 2.1 注意事项 匿名构造函数在 ...

  2. C++中类和对象的一些注意事项 --- 多态

    1. 一些继承中的问题 1.1 多继承中父类含有重名成员问题 如下: #include <iostream> #include <string> using namespace ...

  3. web前端培训分享:面向对象中类和对象的定义是什么?

    在学习web前端技术的时候,我们接触的最多的便是面向对象这一块,其实很多编程技术都有用到这个现象,下面我们就为大家详细的介绍一下面向对象中类和对象的定义是什么? web前端培训分享:面向对象中类和对象 ...

  4. Vue:对象更改检测注意事项

    还是由于 JavaScript 的限制,Vue 不能检测对象属性的添加或删除: var vm = new Vue({data: {a: 1} }) // `vm.a` 现在是响应式的vm.b = 2 ...

  5. java 对象的定义是_浅析Java编程中类和对象的定义

    1,什么是类? 答:类是客观存在的,抽象的,概念的东西. 2,什么事对象? 答:对象是具体的,实际的,代表一个事物.例如:车是一个类,汽车,自行车就是他的对象. 关于类与对象的描述:类是对象的模版,对 ...

  6. php中的类 对象的方法的区别,php中类和对象的区别是什么

    php中类和对象的区别:类是对象的抽象,对象是类的具体实例:类是抽象的,不占用内存,而对象是具体的,占有内存空间.打个比方:类就是水果,对象就是苹果. 本教程操作环境:windows7系统.PHP7. ...

  7. 学习笔记之——Python中类和对象的理解

    学习笔记之--Python中类和对象的理解 面向对象的含义和特性 类 Python中类的定义.结构.创建 Python类的定义 Python类的结构 类的创建 类的属性 类的方法 对象 对象的创建 参 ...

  8. Java语言中类与对象的创建

    Java语言中类与对象的创建 文章目录 Java语言中类与对象的创建 一.实验目的: 二.实验要求: 三.实验内容: 一.实验目的: 1.掌握类.对象的概念: 2.掌握对象的创建过程: 3.理解对象的 ...

  9. python中对象的特性_python中类与对象之继承,python类和对象理解,面对对象的三大特性之...

    python中类与对象之继承,python类和对象理解,面对对象的三大特性之 面对对象的三大特性之继承 1.什么是继承? 在程序中,继承指的是class与class之间的关系 继承是一种关系,必须存在 ...

最新文章

  1. STL容器迭代过程中删除元素技巧
  2. java校验字符串是否为json格式
  3. 用CSS3让不知道宽高的元素居中
  4. could not load java7_xml导入properties文件报异常:Could not load JDBC driver class [${jdbc.driver}]...
  5. HTML5 Canvans 常用API整理
  6. windows7未能启动怎么修复计算机,win7系统提示windows无法启动这个硬件设备怎么办...
  7. MySQL存储引擎:MyISAM InnoDB
  8. Oracle logmnr使用
  9. SQL Server 默认跟踪应用4 -- 检测日志文件自动增长
  10. IIS安装时,安装程序无法复制一个或多个文件。特定错误码是0x4b8
  11. 高等数学(第七版)同济大学 习题1-4 个人解答
  12. Tomcat安装及配置
  13. 模糊控制(一)模糊控制简介及数学基础
  14. 双三次插值算法的C++实现与SSE指令优化
  15. 基于easyui的 增 删 改 查
  16. React router v6写法总结
  17. 手机sd卡恢复工具android版,手机内存卡文件恢复工具(SD卡数据恢复助手)V1.5 正式版...
  18. ZooKeeper 的 Watch 机制是什么?
  19. std::thread与pthread
  20. Android 解决Tablayout不显示标题,头部一片空白的问题

热门文章

  1. oracle19c 安装权限_Oracle19c 安装及SQL developer连接
  2. pytorch保存模型时报错***object has no attribute 'state_dict'
  3. Hadoop分布式集群搭建详细过程
  4. Scrapy-Item Loaders(项目加载器)
  5. jdk1.8 base64注意事项
  6. Java加密与解密的艺术~DES实现
  7. Java加密与解密的艺术~MD算法实现
  8. Java并发编程实战~ThreadLocal
  9. 商城报表系统html5,关于html5:推荐这几款主流报表产品
  10. PID控制器开发笔记之十三:单神经元PID控制器的实现