Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to define the access rights in C++

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to define access rights in C++". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "how to define access rights in C++" together.

It is well known that C++ has three object-oriented features: encapsulation, inheritance, and polymorphism. Let's start with a simple understanding of packaging. Encapsulation is accomplished through classes in C++, a tool for converting abstractions into user-defined types. Class is defined as follows:

class circle{ public: //member variable int m_L; int m_H; public: //member function (method) double get_Square() { return m_L*m_H; }}

In the above code, we define a "circle" class that has three main parts: member variables, member methods, and access control (public, protect, private).

public: Public permission, accessible within class, accessible outside class

protected: protected permissions, accessible within the class, not accessible outside the class, accessible to inherited subclasses

private: private permission, can be accessed within the class, can not be accessed outside the class, inherited subclasses can not be accessed

Here's a test of these three points to impress:

class circle{ public: int public_L; int public_H; private: int private_L; int private_H; protected: int protect_L; int protect_H; public: //member function (method) circle() //assigns a member variable by constructor { public_L = 20, public_H = 30; private_L = 40, private_H = 50; protect_L = 60, protect_L = 70; } double get_Square() { return private_L*private_H; }};

In the defined "circle" class, since the variables public_L,public_H, and get_Square are all public permissions, they can be accessed inside and outside the class. The following two ways are correct:

circle C1; //instantiate class object double S_1 = C1.get_Square();double S_2 = C1.public_H * C1.public_L;

However, since the variables private_L,private_H are private permissions, they can be accessed within the class, that is, get_Square() can be accessed, and the following writing outside the class is wrong:

int S_3 = C1.private_H * C1.private_L; //Error: member variable is inaccessible

Similarly, for variables with protected permissions, they can be accessed within the class, but not outside the class:

int S_3 = C1.private_H * C1.private_L; //Error: member variable is inaccessible

There are three types of inheritance: public, protect, and private:

public: public inheritance, keep access rights of members in parent class, inherit to child class

protected: protected inheritance, except private permissions, all members of the parent class inherit to the child class in a protected way

private: Private inheritance, all members of the parent class are inherited to the child class in a private way.

Here's an example:

class son1:public circle{};//public inheritance, keep the access permissions in the circle parent class unchanged class son2:protected circle{}; //protected inheritance, except for the private permissions in the circle parent class, all your access permissions are protected class son3:private circle{}; //private inheritance, all access permissions of all members in the parent class are private

Then through the above analysis,

The access rights of son1 are consistent with the parent class;

son2 can only access members of parent class within class, but its children can also access members of parent class "circle" through public or protected inheritance

son3 can only inherit members of its parent class privately

So:

son1 s1;s1.get_Square(); //Access public permissions, correct son2 s2;s2.get_Square(); //Error: Member variables inaccessible, protected permissions son3 s3;s3.get_Square(); //Error: Member variables inaccessible, private permissions

In C++, classes can be defined either by class or struct. The only difference between struct and class is that struct defaults to public access and Class defaults to private access. For example:

class a{ int m_a; //class definition, default to private} if access permission is not declared;struct b{ int m_b; //struct definition, default to public} if access permission is not declared;

Hence:

a c_a;c_a.m_a = 20; // Error: member variables are not accessible b s_b;s_b.m_b = 20; //Correct, access public type variables

Summary of permissions for members of classes in C++:

private: can only be accessed by member functions and friend functions of this class, and cannot be accessed by objects of this class.

Protected: In addition to private, it can also be accessed by functions of subclasses, and it cannot be accessed by objects of this class.

public: In addition to protected, it can also be accessed by objects of this class.

privateprotectedfunctions and friend functions of public class √√ Functions of subclasses ×√ Objects of class ××√

Accessibility after inheritance:

After private inheritance, all methods in the parent class become private in the child class.

After protected inheritance, all methods (protected,public) of the parent class>=protected become protected in the subclass.

public After inheritance, the properties of the parent class method remain unchanged.

Thank you for reading, the above is "C++ access rights how to define" the content, after the study of this article, I believe we have a deeper understanding of how to define access rights in C++ this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report