C + + uses public, protected, private three keywords to control the access rights of member variables and member functions, each of which represents a common, protected, private, called member access qualifier. The so-called access permission is that you can use the members in that class.
Java, C # Programmers note that the public, private, protected in C + + can only modify members of a class, cannot decorate classes, and classes in C + + do not share private.
Inside the class (within the code that defines the class), whether the members are declared public, protected, or private, they are accessible to each other and have no restrictions on access rights.
Outside of the class (outside the code that defines the class), members can only be accessed through the object, and only members of the public property can be accessed through the object, and members of the private and protected properties cannot be accessed.
The following is a Student class that demonstrates the access rights of members:
#include <iostream>using namespace std;//class declaration class Student{private: //private char *m_name; int m_age; Float m_score;public: //Total void SetName (char *name); void setage (int age); void SetScore (float score); void Show ();};/ /member function definition void Student::setname (char *name) { m_name = name;} void student::setage (int age) { m_age = age;} void Student::setscore (float score) { m_score = score;} The Age of Void Student::show () { cout<<m_name<< "is <<m_age<<" and the result is "<<m_score<< Endl;} int main () { //Create object on stack Student Stu; Stu.setname ("Xiao Ming"); Stu.setage (a); Stu.setscore (92.5f); Stu.show (); Create an object on the heap Student *pstu = new Student; Pstu-SetName ("Li Hua"); Pstu, Setage (+); Pstu, SetScore (); Pstu, Show (); return 0;}
C + + class member access rights