C ++ class relationships (private \ protect \ public)

Original link: https://blog.csdn.net/wuguangbin1230/article/details/76796891

C ++ class relationship is not complicated, needs a good look oh.
& nbsp
blog post was written very detailed:
C ++ class - defined derived class, composition and access control

The following references:

       This article introduces the C ++ in public, protected and private usage, for C ++ object-oriented programming is a very important concept, can refer a friend in need

       Beginner C ++ friends often see public, protected, private as well as some of the scope of access they represent in succession in the class, it is easy to get confused. Today, this paper is to analyze in C ++ public, protected and private usage. We believe that our in-depth grasp the C ++ programming will be very helpful.

Here we must first understand the following points.

       1. One feature of the package is based, public, and private role is to achieve this purpose. So: the user code (outer class) can access public members and can not access the private members; private member can only be accessed by the members of the class (the class) and friends.

       2. Another feature of the class is to inherit the role of protected is to achieve this purpose. and so:

protected member may be accessed derived object, the user code can not be (outside the class) access.

Now take a look at the following example

#include<iostream>
#include<assert.h>
using namespace std;
class A{
public:
  int a;
  A(){
    a1 = 1;
    a2 = 2;
    a3 = 3;
    a = 4;
  }
  void fun(){
    cout << a << endl;    //正确
    cout << a1 << endl;   //正确
    cout << a2 << endl;   //正确,类内访问
    cout << a3 << endl;   //正确,类内访问
  }
public:
  int a1;
protected:
  int a2;
private:
  int a3;
};
int main(){
  A itema;
  itema.a = 10;    //正确
  itema.a1 = 20;    //正确
  itema.a2 = 30;    //错误,类外不能访问protected成员
  itema.a3 = 40;    //错误,类外不能访问private成员
  system("pause");
  return 0;
}

Features of inheritance:

First remember: regardless of whether inheritance, the above rules will always apply!

There are public, protected, private three kinds of inheritance, are changed accordingly the properties of the base access class members.

       1.public inheritance: the base class public members, protected members, private members access attributes were changed in a derived class: public, protected, private

       2.protected inheritance: the base class public members, protected members, private members access attributes were changed in a derived class: protected, protected, private

       3.private inheritance: the base class public members, protected members, private members access attributes were changed in a derived class: private, private, private

But no matter what kind of inheritance, two points above have not changed:

       1.private members can only be a member of this class (within the class) and friends visit, the derived class can not be accessed;

       2.protected derived class members can be accessed.

Let's look at the following code:

1.public inheritance

code show as below:

#include<iostream>
#include<assert.h>
using namespace std;
 
class A{
public:
  int a;
  A(){
    a1 = 1;
    a2 = 2;
    a3 = 3;
    a = 4;
  }
  void fun(){
    cout << a << endl;    //正确
    cout << a1 << endl;   //正确
    cout << a2 << endl;   //正确
    cout << a3 << endl;   //正确
  }
public:
  int a1;
protected:
  int a2;
private:
  int a3;
};
class B : public A{
public:
  int a;
  B(int i){
    A();
    a = i;
  }
  void fun(){
    cout << a << endl;       //正确,public成员
    cout << a1 << endl;       //正确,基类的public成员,在派生类中仍是public成员。
    cout << a2 << endl;       //正确,基类的protected成员,在派生类中仍是protected可以被派生类访问。
    cout << a3 << endl;       //错误,基类的private成员不能被派生类访问。
  }
};
int main(){
  B b(10);
  cout << b.a << endl;
  cout << b.a1 << endl;   //正确
  cout << b.a2 << endl;   //错误,类外不能访问protected成员
  cout << b.a3 << endl;   //错误,类外不能访问private成员
  system("pause");
  return 0;
}

2.protected Inheritance:

code show as below:

#include<iostream>
#include<assert.h>
using namespace std;
class A{
public:
  int a;
  A(){
    a1 = 1;
    a2 = 2;
    a3 = 3;
    a = 4;
  }
  void fun(){
    cout << a << endl;    //正确
    cout << a1 << endl;   //正确
    cout << a2 << endl;   //正确
    cout << a3 << endl;   //正确
  }
public:
  int a1;
protected:
  int a2;
private:
  int a3;
};
class B : protected A{
public:
  int a;
  B(int i){
    A();
    a = i;
  }
  void fun(){
    cout << a << endl;       //正确,public成员。
    cout << a1 << endl;       //正确,基类的public成员,在派生类中变成了protected,可以被派生类访问。
    cout << a2 << endl;       //正确,基类的protected成员,在派生类中还是protected,可以被派生类访问。
    cout << a3 << endl;       //错误,基类的private成员不能被派生类访问。
  }
};
int main(){
  B b(10);
  cout << b.a << endl;       //正确。public成员
  cout << b.a1 << endl;      //错误,protected成员不能在类外访问。
  cout << b.a2 << endl;      //错误,protected成员不能在类外访问。
  cout << b.a3 << endl;      //错误,private成员不能在类外访问。
  system("pause");
  return 0;
}

3.private Inheritance:

code show as below:

#include<iostream>
#include<assert.h>
using namespace std;
class A{
public:
  int a;
  A(){
    a1 = 1;
    a2 = 2;
    a3 = 3;
    a = 4;
  }
  void fun(){
    cout << a << endl;    //正确
    cout << a1 << endl;   //正确
    cout << a2 << endl;   //正确
    cout << a3 << endl;   //正确
  }
public:
  int a1;
protected:
  int a2;
private:
  int a3;
};
class B : private A{
public:
  int a;
  B(int i){
    A();
    a = i;
  }
  void fun(){
    cout << a << endl;       //正确,public成员。
    cout << a1 << endl;       //正确,基类public成员,在派生类中变成了private,可以被派生类访问。
    cout << a2 << endl;       //正确,基类的protected成员,在派生类中变成了private,可以被派生类访问。
    cout << a3 << endl;       //错误,基类的private成员不能被派生类访问。
  }
};
int main(){
  B b(10);
  cout << b.a << endl;       //正确。public成员
  cout << b.a1 << endl;      //错误,private成员不能在类外访问。
  cout << b.a2 << endl;      //错误, private成员不能在类外访问。
  cout << b.a3 << endl;      //错误,private成员不能在类外访问。
  system("pause");
  return 0;
}

       Through the above code with more detailed comments, the reader should be able to understand. Close look at the code defined in the derived class B and members of a base class of the same name, at this time there is still a base class can be verified.

int main(){
  cout << sizeof(A) << endl;
  cout << sizeof(B) << endl;
 
  system("pause");
  return 0;
}

Output:

       16

       20

       So derived class contains all the members of the base class as well as members of the new members of the same name is hidden, only when calls to call members of the derived class.

If you want to call the base class members with the same name, you can use the following methods:

int main(){
 
  B b(10);
  cout << b.a << endl;
  cout << b.A::a << endl;
 
  system("pause");
  return 0;
}

Output:

       10

       4

       I remember here is access outside the class, and a is public in the base class, so the inheritance should be public, so that in a derived class is still public, can be accessed outside the class.

Interested readers can look at the examples herein commissioning, and will deepen the impression of a new harvest.

Guess you like

Origin blog.csdn.net/qq_32642107/article/details/102754571
Recommended