zoukankan      html  css  js  c++  java
  • 类与类之间的关系

    • 横向关系

    依赖 关联 聚合 组合

    判断方法:

      生命周期有关系:组合,聚合
      聚合:包含多个相同的类
      组合:定义的时候就要有
      依赖:只要使用就必须要有
      关联:可有可无

    • 纵向关系

    继承

    基类( 父类 )->派生类(子类)

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class CPerson
     5 {
     6 protected:
     7     
     8 public:
     9     int age;
    10     CPerson()
    11     {
    12         age = 100;
    13     }
    14 };
    15 class CSuperman :public CPerson
    16 {
    17 protected:
    18     
    19 public:
    20     int age;
    21     CSuperman()
    22     {
    23         age = 123;
    24     }
    25 };
    26 int main()
    27 {
    28     CPerson person;
    29     CSuperman superman;
    30     cout<<superman.age<<endl;                                  //123
    31     cout<<superman.CPerson::age<<endl;                         //100
    32     superman.CPerson::age = 111;
    33     cout<<person.age<<endl;//改写的为父类中的子类,与父类没关系      //100
    34     cout<<superman.CPerson::age<<endl;                         //111
    35 }
      父类中 private 成员在无论怎样继承,在子类中都不可访问
      public 继承 public和protected 没有变化
      protected  继承  public 变成 protected 
      private    继承   public, protected 变成 private
    继承的构造和析构
     1 #include<iostream>
     2 using namespace std;
     3 
     4 class AA
     5 {
     6 public:
     7     AA()
     8     {
     9         cout << "AA" << endl;
    10     }
    11     ~AA()
    12     {
    13         cout << "~AA" << endl;
    14     }
    15 };
    16 
    17 class BB:public AA
    18 {
    19 public:
    20     BB()
    21     {
    22         cout << "BB" << endl;
    23     }
    24     ~BB()
    25     {
    26         cout << "~BB" << endl;
    27     }
    28 };
    29 
    30 class CC
    31 {
    32 public:
    33     CC()
    34     {
    35         cout << "CC" << endl;
    36     }
    37     ~CC()
    38     {
    39         cout << "~CC" << endl;
    40     }
    41 };
    42 
    43 class DD:public CC
    44 {
    45 public:
    46     BB b;
    47 public:
    48     DD()
    49     {
    50         cout << "DD" << endl;
    51     }
    52     ~DD()
    53     {
    54         cout << "~DD" << endl;
    55     }
    56 };
    57 
    58 int main()
    59 {
    60     DD d;
    61 
    62     return 0;
    63 }
    
    
    
    
  • 相关阅读:
    背水一战 Windows 10 (90)
    背水一战 Windows 10 (89)
    背水一战 Windows 10 (88)
    背水一战 Windows 10 (87)
    背水一战 Windows 10 (86)
    背水一战 Windows 10 (85)
    背水一战 Windows 10 (84)
    背水一战 Windows 10 (83)
    背水一战 Windows 10 (82)
    背水一战 Windows 10 (81)
  • 原文地址:https://www.cnblogs.com/Lune-Qiu/p/7912422.html
Copyright © 2011-2022 走看看