直接上图:

以及:

实践如下:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Father{
  5.  
  6. private:
  7. int father1;
  8. int getFather1Private(){
  9. return this->father1;
  10. }
  11.  
  12. protected:
  13. int father2;
  14. int getFather2Protected(){
  15. return this->father2;
  16. }
  17. public:
  18. int father3;
  19. Father(){
  20. this->father1 = ;
  21. this->father2 = ;
  22. this->father3 = ;
  23. cout<< "无参构造器"<<endl;
  24. }
  25. // 基类构造器要加virtual的原因:
  26. // 这是因为当用基类引用派生类的时候,如果此时对基类进行delete操作,对于没有虚函数析构函数,那么只会调用基类的析构函数,而对派生类的析构函数不会进行析构
  27. virtual ~Father(){
  28. cout<< "Father析构函数"<<endl;
  29. }
  30. Father(int aa){
  31. this->father1 = aa;
  32. this->father2 = ;
  33. this->father3 = ;
  34. cout<< "有参构造器"<<endl;
  35. }
  36. int getFather1(){
  37. return father1;
  38. }
  39. int getFather2(){
  40. return father2;
  41. }
  42. int getFather3(){
  43. return this->father3;
  44. }
  45. };
  46.  
  47. class Son : public Father{
  48.  
  49. private:
  50. int son1;
  51.  
  52. public:
  53. Son(){
  54. this->son1 = ;
  55. }
  56. ~Son(){
  57. cout<< "Son析构函数"<<endl;
  58. }
  59. // 通过这样的方式调用父类构造器
  60. Son(int fatherV, int sonV):Father(fatherV){
  61. Father::father3 = fatherV;
  62. this->son1 = sonV;
  63.  
  64. }
  65. int method(){
  66. int value = son1 * Father::father3;
  67. return value;
  68. }
  69. int getSon1(){
  70. return son1;
  71. }
  72.  
  73. };
  74.  
  75. int main(){
  76.  
  77. cout << "类继承实践:" << endl;
  78.  
  79. // 调用无参构造器
  80. Son son1;
  81.  
  82. // 有参构造器
  83. Son son(,);
  84.  
  85. // 在类外部仅有父类的public可以访问
  86. // error: cout << "son.getFather1Private():" << son.getFather1Private() << endl;
  87. // error: cout << "son.getFather2Protected():" << son.getFather2Protected() << endl;
  88. cout << "son.getFather1():" << son.getFather1() << endl;
  89. cout << "son.getFather2():" << son.getFather2() << endl;
  90.  
  91. cout << "son.father3:" << son.father3 << endl;
  92. // error: cout << "son.father2:" << son.father2 << endl;
  93.  
  94. cout << "son.getSon1():" << son.getSon1()<< endl;
  95.  
  96. //cout << "son.father3:" << son.son1 << endl;
  97. cout << "son.method():" << son.method() << endl;
  98.  
  99. // https://blog.csdn.net/u014453898/article/details/60402586
  100. // 这是因为当用基类引用派生类的时候,如果此时对基类进行delete操作,对于没有虚函数析构函数,那么只会调用基类的析构函数,而对派生类的析构函数不会进行析构
  101. Father *ptr = new Son();
  102. cout << "泛型:" << ptr->father3 << endl;
  103. delete ptr;
  104.  
  105. cout << "类继承 实践 end." << endl;
  106.  
  107. return ;
  108. }

C++类继承方式及实践的更多相关文章

  1. threading 多线程类继承方式调用

    import threading #线程import time class Work(threading.Thread): def __init__(self,n): threading.Thread ...

  2. C++学习笔记(十二):类继承、虚函数、纯虚函数、抽象类和嵌套类

    类继承 在C++类继承中,一个派生类可以从一个基类派生,也可以从多个基类派生. 从一个基类派生的继承称为单继承:从多个基类派生的继承称为多继承. //单继承的定义 class B:public A { ...

  3. C++中的类继承(1) 三种继承方式

    继承是使代码可以复用的重要手段,也是面向对象程序设计的核心思想之一.简单的说,继承是指一个对象直接使用另一对象的属性和方法.继承呈现了 面向对象程序设 计的层次结构, 体现了 由简单到复杂的认知过程. ...

  4. JS类继承常用方式发展史

    JS类继承常用方式发展史 涉及知识点 构造函数方式继承 1-继承单个对象 1.1 多步走初始版 1.2 多步走优化版 1.3 Object.create()方式 2-继承多个对象 2.1 遍历 Obj ...

  5. Object-C的类可以多重继承吗?可以实现多个接口吗?category是什么?重写一个类的方式用继承好还是分类好,为什么?

    Object-C的类可以多重继承吗?可以实现多个接口吗?category是什么?重写一个类的方式用继承好还是分类好,为什么? 答:Object-c的类不可以多重继承,可以实现多个接口(协议),Cate ...

  6. mfc 类三种继承方式下的访问

    知识点 public private protected 三种继承方式 三种继承方式的区别 public 关键字意味着在其后声明的所有成员及对象都可以访问. private 关键字意味着除了该类型的创 ...

  7. 理解C++类的继承方式(小白)

    基类里的 public(大人) protect(青年) private(小孩) 在通过继承时  继承方式public(我是大人咯) protect(我是青少年) private(我系小孩纸啦) &qu ...

  8. C++ 类中的3种访问权限和继承方式

    访问权限:public 可以被任意实体访问,protected 只允许子类(无论什么继承方式)及本类的成员函数访问,private 只允许本类的成员函数访问.三种继承方式分别是 public 继承,p ...

  9. C++ 中三种继承方式的理解

    一.公有继承(public inheritance) 1.概念:当类的继承方式为公有继承时,基类的公有成员和保护成员的访问属性在派生类中不变,而基类的私有成员不可以直接访问. 实验一下:   我们用代 ...

随机推荐

  1. 利用selenium 爬取豆瓣 武林外传数据并且完成 数据可视化 情绪分析

    全文的步骤可以大概分为几步: 一:数据获取,利用selenium+多进程(linux上selenium 多进程可能会有问题)+kafka写数据(linux首选必选耦合)windows直接采用的是写my ...

  2. (转)Python3.X如何下载安装urllib2包 ?

    python 3.X版本是不需要安装:urllib2包的,urllib和urllib2包集合成在一个包了那现在问题是:在python3.x版本中,如何使用:urllib2.urlopen()? 答:i ...

  3. 因xhost命令和DISPLAY环境变量操作不当导致无法启动Oracle图形化安装界面

    在redhat操作系统上安装Oracle 11.1时,遇到在执行runInstaller后无法启动安装图像化界面,甚是郁闷. 问题现象: 使用Xmanager2.0软件登陆AIX桌面,root用户可以 ...

  4. Delphi 变量的作用域

  5. 韦东山嵌入式Linux学习笔记02--如何给开发板烧录程序

    购买韦东山嵌入式开发板jz2440 v3会标配两根usb线和一根网线,OpenJtag需要单独购买, 我暂时还没买到该工具. 下面介绍usb烧录以及通过网线烧录程序. 1.usb烧录程序: 借助DNW ...

  6. tornado实现高并发爬虫

    from pyquery import PyQuery as pq from tornado import ioloop, gen, httpclient, queues from urllib.pa ...

  7. deep_learning_Function_numpy_argmax()函数

    numpy里面的argmax函数 函数原型:def argmax(a, axis=None, out=None)a----输入arrayaxis----为0代表列方向,为1代表行方向out----结果 ...

  8. 04-spring框架—— Spring 集成 MyBatis

    将 MyBatis与 Spring 进行整合,主要解决的问题就是将 SqlSessionFactory 对象交由 Spring来管理.所以,该整合,只需要将 SqlSessionFactory 的对象 ...

  9. JavaScript 复制变量的三种方法

    参考:Copying Objects in JavaScript - Orinami Olatunji(@orinamio_) October 23, 2017    直接将一个变量赋给另一个变量时, ...

  10. 数字转化为汉字,如5->五

    //数字转化为汉字 如5-->五-(NSString*)translation:(NSString *)arebic{   NSString *str = arebic;    NSArray ...