c语言访问控制安全问题
Now before studying how to define class and its objects, lets first quickly learn what are access modifiers.
现在,在研究如何定义类及其对象之前,首先让我们快速学习什么是访问修饰符。
Access modifiers in C++ class defines the access control rules. C++ has 3 new keywords introduced, namely,
C ++类中的访问修饰符定义访问控制规则。 C ++引入了3个新关键字,分别是:
public
上市
private
私人的
protected
受保护的
These access modifiers are used to set boundaries for availability of members of class be it data members or member functions
这些访问修饰符用于为数据成员或成员函数的类成员的可用性设置边界
Access modifiers in the program, are followed by a colon. You can use either one, two or all 3 modifiers in the same class to set different boundaries for different class members. They change the boundary for all the declarations that follow them.
程序中的访问修饰符,后跟冒号。 您可以在同一类中使用一个,两个或所有三个修饰符为不同的类成员设置不同的边界。 它们更改了跟随它们的所有声明的边界。
C ++中的公共访问修饰符 (Public Access Modifier in C++)
Public, means all the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too. Hence there are chances that they might change them. So the key members must not be declared public.
公开,意味着公开宣布的所有班级成员将对所有人开放。 声明为public的数据成员和成员函数也可以由其他类访问。 因此,他们有可能改变它们。 因此,关键成员不得宣布为公开。
class PublicAccess
{
// public access modifier
public:
int x; // Data Member Declaration
void display(); // Member Function decaration
}
C ++中的专用访问修饰符 (Private Access Modifier in C++)
Private keyword, means that no one can access the class members declared private, outside that class. If someone tries to access the private members of a class, they will get a compile time error. By default class variables and member functions are private.
Private关键字,表示没有人可以访问在该类之外声明为private的类成员。 如果有人试图访问一个类的私有成员,他们将得到一个编译时错误 。 默认情况下,类变量和成员函数是私有的。
class PrivateAccess
{
// private access modifier
private:
int x; // Data Member Declaration
void display(); // Member Function decaration
}
C ++中的受保护的访问修饰符 (Protected Access Modifier in C++)
Protected, is the last access specifier, and it is similar to private, it makes class member inaccessible outside the class. But they can be accessed by any subclass of that class. (If class A is inherited by class B, then class B is subclass of class A. We will learn about inheritance later.)
受保护的是最后一个访问说明符,它与private相似,它使类成员在类外部无法访问。 但是它们可以被该类的任何子类访问。 (如果类A由类B 继承 ,则类B是类A的子类。稍后我们将学习继承。)
class ProtectedAccess
{
// protected access modifier
protected:
int x; // Data Member Declaration
void display(); // Member Function decaration
}
c语言访问控制安全问题