/*The base class defines the following three types of member variables: public: Both the base class and the derived class object can access the protected: neither the base class nor the object of the derived class can be accessed, and the private is accessible in its own member function: the base class and the pie cannot be accessed by a derived class in its member function .*/#include<iostream>using namespacestd;classitem_base{ Public: intA;Private: intb;protected: intc;};classBulk_item: Publicitem_base{ Public: voidInit (item_base&Item) {a=2; //B = 2; //Error Privatec =2;//OK protectedITEM.A =1; //item.b = 1;//Error Private//ITEM.C = c * 3;//Error protected }};intMain () {item_base Item; ITEM.A=1; //item.b = 1; //Error Private//item.c = 1; //Error protectedBulk_item Bulk_item; Bulk_item.init (item); BULK_ITEM.A=2; //bulk_item.c = 2;//Error protected//bitem.b = 2; //Error Private return 0;}
View Code
C + + Class access restrictions