Deep understanding of public, protected, and private usage _c languages in C + +

Source: Internet
Author: User
Tags access properties assert inheritance

Beginners C + + friends often see in the class Public,protected,private and their inheritance in the expression of some of the scope of access, it is easy to confuse. Today this article will be very analysis of C + + public, protected and private usage. I believe that for everyone to master the C + + program design will be a great help.

Here we must first understand the following points.

1. a feature of a class is encapsulation, and public and private roles are to achieve this . So:

User code (outside of a class) can access public members without access to private members, and private members can only be accessed by class members (within the class) and friends.

2. another characteristic of the class is inheritance, and the role of protected is to achieve this . So:

Protected members can be accessed by derived class objects and cannot be accessed by user code (outside the Class).

Now take a look at the following examples:

#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, within class access
    cout << A3 << Endl;   Correct, within class access
  } public
:
  int A1;
Protected:
  int A2;
Private:
  int a3;
};
int main () {
  A itema;
  ITEMA.A = ten;    correct
  itema.a1 =;    correct
  itema.a2 =;    Error, outside class cannot access protected member
  ITEMA.A3 =;    Error, Private member
  system ("pause") cannot be accessed outside the class;
  return 0;
}

The characteristics of the inheritance:

Remember first: Whether or not inheritance, the above rules always apply!

There are public, protected, private three ways of inheriting, which change the access properties of the base class members accordingly.

1.public Inheritance: base class public members, protected members, access properties of private members in derived classes, respectively: public, protected, private

2.protected Inheritance: base class public member, protected member, private member's Access property in derived class, respectively: protected, protected, private

3.private Inheritance: base class public member, protected member, private member's access attribute in derived class, respectively: Private, private, private

But either way, none of the above two points have changed:

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

2.protected members can be accessed by derived classes .

Take another 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;
    True, the public member of the base class is still public in the derived class.       cout << A2 << Endl;
    Correctly, the protected member of the base class, which is still protected in the derived class, can be accessed by derived classes.       cout << A3 << Endl;
  Error, private members of the base class cannot be accessed by derived classes.
}
};
  int main () {b b (10);
  cout << B.A << Endl;   cout << b.a1 << Endl;   Correct cout << b.a2 << Endl;   Error, no access to protected member cout << b.a3 << Endl outside class; Error, no class outside the visitAsk Private member 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;
    Correctly, the public member of the base class becomes protected in the derived class and can be accessed by derived classes.       cout << A2 << Endl;
    Correctly, the protected member of the base class, in a derived class or protected, can be accessed by derived classes.       cout << A3 << Endl;
  Error, private members of the base class cannot be accessed by derived classes.
}
};
  int main () {b b (10);       cout << B.A << Endl; That's right.      Public member cout << b.a1 << Endl;
  Error, protected members cannot be accessed outside the class.      cout << b.a2 << Endl; Error, ProteCTED members cannot be accessed outside the class.      cout << b.a3 << Endl;
  Error, private members cannot be accessed outside the 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;
    correctly, the base class public member, which becomes private in a derived class, can be accessed by derived classes.       cout << A2 << Endl;
    Correctly, the protected member of the base class becomes private in the derived class and can be accessed by derived classes.       cout << A3 << Endl;
  Error, private members of the base class cannot be accessed by derived classes.
}
};
  int main () {b b (10);       cout << B.A << Endl; That's right.      Public member cout << b.a1 << Endl;
  Error, private members cannot be accessed outside the class.      cout << b.a2 << Endl; Error, private members cannot be inClass outside Access.      cout << b.a3 << Endl;
  Error, private members cannot be accessed outside the class.
  System ("pause");
return 0;

 }

The above code is provided with a more detailed comment that the reader should be able to understand. Look carefully in the code derived Class B defines a member A with the same name as the base class, at which point a of the base class still exists and can be validated.

int main () {
  cout << sizeof (A) << Endl;
  cout << sizeof (B) << Endl;

  System ("pause");
  return 0;
}

Output:

16

20

So the derived class contains all the members of the base class and the new members, the members of the same name are hidden, and only the members in the derived class are invoked when invoked.

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

int main () {

  b b ();
  cout << B.A << Endl;
  cout << b.a::a << Endl;

  System ("pause");
  return 0;
}

Output:

10

4

Remember that this is accessed outside of the class, and a is public in the base class, so the inheritance 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 of the same time there will be a new harvest.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.