继承

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class father{
  5.  
  6. public:
  7. void getHeight(){cout<<height<<endl;}
  8. void getAge(){cout<<age<<endl;}
  9. protected:
  10. int height,age;
  11. };
  12.  
  13. class son: public father{
  14. private:
  15. int weight;
  16. public:
  17. void getWeight(){cout<<weight<<endl;}
  18. void shwo()
  19. {
  20. cout<<this->weight<<endl;
  21. cout<<this->height<<endl;
  22. cout<<this->age<<endl;
  23. }
  24. };
  25.  
  26. int main()
  27. {
  28. son one;
  29. one.shwo();
  30.  
  31. }

子类对象赋值给父类

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class father{
  5.  
  6. public:
  7. int height;
  8. };
  9.  
  10. class son: public father{
  11. public:
  12. int weight;
  13.  
  14. };
  15.  
  16. int main()
  17. {
  18. son a;
  19. father b;
  20.  
  21. a.height=;
  22. b=a;//子类赋给父类
  23. }

父类引用指向子类对象

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class father{
  5.  
  6. public:
  7. int height;
  8. };
  9.  
  10. class son: public father{
  11. public:
  12. int weight;
  13.  
  14. };
  15.  
  16. int main()
  17. {
  18. father *p;
  19. son jack;
  20.  
  21. p=&jack; //父类引用指向子类对象 父类还可以作为子类的别名(虚函数)
  22. p->height=;
  23.  
  24. cout<<jack.height<<endl;
  25. cout<<jack.weight<<endl;
  26.  
  27. }
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class father{
  5.  
  6. protected:
  7. int height;
  8. };
  9.  
  10. //私有派生用的不多,因为继承的成员都变成私有的了,不可以访问
  11. class son: private father{ //继承的父类的protected,public全部变成 private
  12. public:
  13. int weight;
  14.  
  15. int getHeight()
  16. {
  17. return height;
  18.  
  19. }
  20.  
  21. void setHeight(int x)
  22. {
  23. this->height=x;
  24.  
  25. }
  26.  
  27. };
  28.  
  29. int main()
  30. {
  31. son a ;
  32. a.setHeight();
  33. cout<<a.getHeight()<<endl;
  34. }

继承中构造函数的执行顺序(先构造基类,后构造子类, 先析构子类,在析构基类)

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class father{
  5.  
  6. private:
  7. int height;
  8. public:
  9. father(){cout<<"father construct"<<endl;}
  10. ~father(){cout<<"father destruct"<<endl;}
  11.  
  12. };
  13.  
  14. class son: public father{
  15. public:
  16. int weight;
  17. son(){cout<<"son construct"<<endl;}
  18. ~son(){cout<<"son destruct"<<endl;}
  19.  
  20. };
  21.  
  22. int main()
  23. {
  24. son a ;
  25.  
  26. return ;
  27. }

多重继承,以 继承的顺序进行构造

向基类构造函数传递参数

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class father{
  5.  
  6. public:
  7. int height;
  8. int weight;
  9. public:
  10. father(int height, int weight ){
  11. this->height=height;
  12. this->weight=weight;
  13. cout<<"father construct"<<endl;
  14. }
  15. ~father(){cout<<"father destruct"<<endl;}
  16.  
  17. };
  18.  
  19. class son: public father{
  20. public:
  21. int age;
  22. son(int height,int weight,int age);
  23. ~son(){cout<<"son destruct"<<endl;}
  24.  
  25. };
  26.  
  27. son::son(int height, int weight, int age):father(height, weight)
  28. {
  29. this->age=age;
  30. cout<<"son construct"<<endl;
  31. }
  32.  
  33. int main()
  34. {
  35. son a(,,) ;
  36.  
  37. cout<<a.height<<endl;
  38. cout<<a.weight<<endl;
  39. cout<<a.age<<endl;
  40.  
  41. return ;
  42. }

多继承的歧义(作用域操作符)

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. //class father{
  5. //
  6. //public:
  7. // int height;
  8. // int weight;
  9. //public:
  10. // father(int height, int weight ){
  11. // this->height=height;
  12. // this->weight=weight;
  13. // cout<<"father construct"<<endl;
  14. // }
  15. // ~father(){cout<<"father destruct"<<endl;}
  16. //
  17. //};
  18. //
  19. //class son: public father{
  20. //public:
  21. // int age;
  22. // son(int height,int weight,int age);
  23. // ~son(){cout<<"son destruct"<<endl;}
  24. //
  25. //};
  26. //
  27. //son::son(int height, int weight, int age):father(height, weight)
  28. //{
  29. // this->age=age;
  30. // cout<<"son construct"<<endl;
  31. //}
  32.  
  33. class a{
  34. public:
  35. void hello(){cout<<"a hello"<<endl;}
  36. };
  37.  
  38. class b{
  39. public:
  40. void hello(){cout<<"b hello"<<endl;}
  41. };
  42.  
  43. class c : public a, public b{
  44. public:
  45. void hello(){cout<<"c hello"<<endl;}
  46. };
  47.  
  48. int main()
  49. {
  50.  
  51. c i;
  52. i.hello(); //本类的无需
  53. i.a::hello(); //作用域操作符,作用域分辨
  54. i.b::hello();
  55.  
  56. return ;
  57. }

解决两异性(虚基类)

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class common{
  5. public:
  6. void stand(){}
  7.  
  8. };
  9.  
  10. class a: virtual public common{
  11. public:
  12. void hello(){cout<<"a hello"<<endl;}
  13. };
  14.  
  15. class b: virtual public common{
  16. public:
  17. void hello(){cout<<"b hello"<<endl;}
  18. };
  19.  
  20. class c : public a, public b{
  21. public:
  22. void hello(){cout<<"c hello"<<endl;}
  23. };
  24.  
  25. int main()
  26. {
  27.  
  28. c i;
  29. i.stand();
  30.  
  31. return ;
  32. }

c++学习-继承的更多相关文章

  1. JavaScript 核心学习——继承

    本篇博文讲述如何在 JavaScript 中实现继承,以及原型与原型链的知识,在附录中将会讲述 JavaScript 面向对象的常见错误. ##原型与原型链在 JavaScript 中,使用类将会付出 ...

  2. Java基础学习-- 继承 的简单总结

    代码参考:Java基础学习小记--多态 为什么要引入继承? 还是做一个媒体库,里面可以放CD,可以放DVD.如果把CD和DVD做成两个没有联系的类的话,那么在管理这个媒体库的时候,要单独做一个添加CD ...

  3. python学习-继承

    # 继承# 你的是我的,我的还是我的 class Animal: def __init__(self,name,private_v1): self.name = name self._private_ ...

  4. c++学习--继承与派生

    继承和派生 1 含有对象成员(子对象)的派生类的构造函数,定义派生类对象成员时,构造函数的执行顺序如下: 1 调用基类的构造函数,对基类数据成员初始化: 2 调用对象成员的构造函数,对对象成员的数据成 ...

  5. Yii2的深入学习--继承关系

    想要了解 Yii2 的话,一定要对 Yii2 中相关类的继承关系有所了解.由于暂时读的代码有限,下面的图中只列出了部分继承关系,之后回跟着源码阅读的越来越多而增加 由上图可以看到 Yii2 中大多数类 ...

  6. Java学习——继承

    将学生工人的共性描述提取出来,单独进行描述,只要让学生和工人与单独描述的这个类有关系,就可以了. 继承:1,提高了代码的复用性.2,让类与类之间产生了关系.有了这个关系,才有了多态的特性. 注意:千万 ...

  7. day25 python学习 继承,钻石继承 多态

    ---恢复内容开始--- 通过一个列子认识父类和子类中,子类的如何实现对父类默认属性调用,同时拥有自己的属性,如何在子类中调用父类的方法,class Ainmal: country='afdas' d ...

  8. day25 python学习 继承,钻石继承

    通过一个列子认识父类和子类中,子类的如何实现对父类默认属性调用,同时拥有自己的属性,如何在子类中调用父类的方法,class Ainmal: country='afdas' def __init__(s ...

  9. OC学习--继承

     1.什么是继承? 继承是指一个对象直接使用另一对象的属性和方法. 继承可以使得子类具有父类的各种属性和方法,而不是再次编写相同的代码.在子类继承父类的同时,可以重新定义某些属性,并重写某些方法, 即 ...

随机推荐

  1. 改造dede 后台会员目录

    可以在后台目录(默认 dede),里面的inc->inc_menu.php 文件里进行添加或更改,实现功能!

  2. PHP太怪了,in_array() ,strpos,

    PHP中在某个字符中查找另外一个字符串,是否存在,用的是strpos,此函数用法,经常很多人用反了,正确的用法是strpos(string,search),strstr等,前面是原字符串,后面是要在原 ...

  3. IOS中打开应用实现检查更新的功能

    //检查更新页面 - (void)Renew{        NSDictionary *infoDic = [[NSBundle mainBundle]infoDictionary];        ...

  4. (转)Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)

    基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个 ...

  5. Android——文件的保存和读取

    Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中.可以使用Context ...

  6. java多线程之:SynchronousQueue队列

    SynchronousQueue是这样一种阻塞队列,其中每个 put 必须等待一个 take,反之亦然.同步队列没有任何内部容量,甚至连一个队列的容量都没有.      不能在同步队列上进行 peek ...

  7. java多线程机制

    多线程使用场景 1.同时需要做两件以上事情时需要开多个线程(例如:上传功能附带进度条显示,一边做上传,一边更新进度条状态.) 2.大量同类型数据需要进行处理(导入导出大量数据) 3.office文档转 ...

  8. Python的numpy库下的几个小函数的用法

    numpy库是Python进行数据分析和矩阵运算的一个非常重要的库,可以说numpy让Python有了matlab的味道 本文主要介绍几个numpy库下的小函数. 1.mat函数 mat函数可以将目标 ...

  9. Linux启动新进程的三种方法

    程序中,我们有时需要启动一个新的进程,来完成其他的工作.下面介绍了三种实现方法,以及这三种方法之间的区别. 1.system函数-调用shell进程,开启新进程system函数,是通过启动shell进 ...

  10. python_Day3[set集合,函数,全局变量之篇]

    一.set集合 1.Set集合特点:无序.不重复,可嵌套 2.set集合创建规则:set = {"123","234"} 字典创建规则:dic = {“Key” ...