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

Keywords: Programming

The relationship between C + + classes is not complicated. You need to have a good understanding.
&nbsp
This blog is very detailed:
C + + classes -- definition, construction and access control of derived classes

The following references:

This article mainly introduces the use of public, protected and private in C + +, which is a very important concept for C + + object-oriented programming. Please refer to

Friends who are new to C + + often see public, protected, private and some access scopes they represent in inheritance in their classes, which is easy to confuse. Today, I will analyze the public, protected and private usage in C + +. I believe it will be very helpful for us to master the C + + programming.

Here we first need to understand the following points.

One of the characteristics of the class is encapsulation. The role of public and private is to achieve this goal. Therefore: user code (out of class) can access public members but not private members; private members can only be accessed by class members (in class) and friends.

Another feature of class 2 is inheritance, which is what protected does. So:

protected members can be accessed by derived class objects, not by user code (out of class).

Let's 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;    //Correct
    cout << a1 << endl;   //Correct
    cout << a2 << endl;   //Correct, intra class access
    cout << a3 << endl;   //Correct, intra class access
  }
public:
  int a1;
protected:
  int a2;
private:
  int a3;
};
int main(){
  A itema;
  itema.a = 10;    //Correct
  itema.a1 = 20;    //Correct
  itema.a2 = 30;    //Error, cannot access protected member outside class
  itema.a3 = 40;    //Error, private member cannot be accessed outside the class
  system("pause");
  return 0;
}

Features in inheritance:

First of all, remember that the above rules will always apply regardless of inheritance.

There are three inheritance methods: public, protected, and private. They change the access properties of the base class members accordingly.

Public inheritance: the access properties of public members, protected members and private members of the base class are respectively changed into: public, protected, private in the derived class.

2.protected inheritance: the access properties of the public member, protected member and private member of the base class are respectively changed into: protected, protected, private in the derived class.

Private inheritance: the access properties of public member, protected member and private member of the base class are respectively changed into private, private and private in the derived class.

However, no matter which inheritance method, the above two points have not changed:

Private members can only be accessed by members of this class (within the class) and friends, not by derived classes;

The '2.protected' member can be accessed by a derived class.

Take a look at the following code:

1.public inheritance

The code is as follows:

#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;    //Correct
    cout << a1 << endl;   //Correct
    cout << a2 << endl;   //Correct
    cout << a3 << endl;   //Correct
  }
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;       //Correct, public member
    cout << a1 << endl;       //Correct, the public member of the base class is still a public member in the derived class.
    cout << a2 << endl;       //Correct, the protected member of the base class can still be accessed by the derived class in the derived class.
    cout << a3 << endl;       //Error, private member of base class cannot be accessed by derived class.
  }
};
int main(){
  B b(10);
  cout << b.a << endl;
  cout << b.a1 << endl;   //Correct
  cout << b.a2 << endl;   //Error, cannot access protected member outside class
  cout << b.a3 << endl;   //Error, private member cannot be accessed outside the class
  system("pause");
  return 0;
}

2.protected inheritance:

The code is as follows:

#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;    //Correct
    cout << a1 << endl;   //Correct
    cout << a2 << endl;   //Correct
    cout << a3 << endl;   //Correct
  }
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;       //Correct, public member.
    cout << a1 << endl;       //Correct, the public member of the base class becomes protected in the derived class and can be accessed by the derived class.
    cout << a2 << endl;       //Correct, the protected member of the base class, which is still protected in the derived class, can be accessed by the derived class.
    cout << a3 << endl;       //Error, private member of base class cannot be accessed by derived class.
  }
};
int main(){
  B b(10);
  cout << b.a << endl;       //Correct. public members
  cout << b.a1 << endl;      //Error, protected member cannot be accessed outside of class.
  cout << b.a2 << endl;      //Error, protected member cannot be accessed outside of class.
  cout << b.a3 << endl;      //Error, private member cannot be accessed outside of class.
  system("pause");
  return 0;
}

3.private inheritance:

The code is as follows:

#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;    //Correct
    cout << a1 << endl;   //Correct
    cout << a2 << endl;   //Correct
    cout << a3 << endl;   //Correct
  }
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;       //Correct, public member.
    cout << a1 << endl;       //Correct. The public member of the base class becomes private in the derived class and can be accessed by the derived class.
    cout << a2 << endl;       //Correct, the protected member of the base class becomes private in the derived class and can be accessed by the derived class.
    cout << a3 << endl;       //Error, private member of base class cannot be accessed by derived class.
  }
};
int main(){
  B b(10);
  cout << b.a << endl;       //Correct. public members
  cout << b.a1 << endl;      //Error, private member cannot be accessed outside of class.
  cout << b.a2 << endl;      //Error, private member cannot be accessed outside of class.
  cout << b.a3 << endl;      //Error, private member cannot be accessed outside of class.
  system("pause");
  return 0;
}

Through the above code, the reader should be able to understand. Take a close look at the member a with the same name as the base class defined in the derived class B in the code. At this time, the a of the base class still exists and can be verified.

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

Output:

       16

       20

So a derived class contains all members of the base class and new members. Members with the same name are hidden. When calling, only members of the derived class will be called.

If you want to call a member of the base class 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

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

Interested readers can debug and run the example of this article, deepen the impression and have new gains at the same time.

Posted by ruddyu78 on Fri, 25 Oct 2019 20:53:43 -0700