Briefly introduce the differences between protect, public, and private, which will not be detailed later. Welcome to correction ~ Take C ++ as an example: public: data members and function members can be directly used in member functions, friends, and inheritance classes. It can also be used as an interface for class users (A, A. Change ()). PRIVATE: Data and function members, which can only be accessed by class members and friends. It cannot be accessed by an inherited class (no matter what type of inheritance). Although it is a private member of the inherited class, parameters are transmitted by calling the constructors of the base class. The inherited class cannot access the Private Members of the base class. Protect: the usage of the base class is the same as that of the private class. The base class object cannot be accessed. In a derived class, a member of the same base class is public and can be accessed and used by its inherited class object.
class Base{ private: int age;protect: int num;}class Devi : public Base{}Base b; Devi d;b.num=3; //errord.num = 3; //ok
When a user of an inheritance class uses a member inherited from a base class, it not only depends on the access label (private, public, etc.) of the base class during definition ), it also depends on the Inheritance Method of the base class. At the same time, the inheritance class can only make the access permission smaller. At the same time, pay attention to the differences between the inheritance of the partition classification interface and the implementation inheritance. Interface inheritance, that is, the class user can call function members directly through "." or "->. Implement inheritance: inherits the access permissions of data members during the definition of member functions of the class. Note the differences between class members and class users. Class Members include data members and function members. They are about class implementation. A class user refers to a class object, which refers to the use of interfaces. Yes. The object uses "." to point to the object through "-> ".
Class A{public: A(string& Name = "lihao",int Age = 21) {} virtual void print()const;private : string name; int age;}void A::print()const{ cout<<name<<endl <<age<<endl;}Class B:public A{public: B(string& Name,int Age,int Grade):A(Name,Age),grade(Grade) {}private: int grade;}void B::print()const{ A::print(); cout<<grade<<endl;}