C learning incoming (2) - Inheritance and derived Chen Gongfa due to the existence of classes and objects, making C becomes more powerful, which is one of the reasons for C . But C facing the magic of the object in the end? I gradually felt out yesterday! That is the three major features - packages, inheritance and polymorphism, let me learn to inherit! The class has a powerful inheritance and derived function, making the programmer using C can greatly improve the reuse rate of program code, and give an example: Or "I" as a class. Class me {private: char name [10]; int Age; char sex;} When I want to declare a "he" class, because those data members are the same, so we can use "I" to do Class, derived "he", derived the application method for class derived class name: derived method base class name {// derived class data member and member functions
}; // Note, should be; First, private derived, keyword is private, and the other is the public, the keyword is public. So, what there is from different inheritance methods? Still first look at an example! #include class base {int x; public: void setx (int N) {x = n;} void showX () {cout << x << endl;}}; class derived: private base {int Y; public: void setxy (int N, int m) {setX (n); y = m;} void showxy () {couid x x 非 非;;;} // illegally, private derived class cannot be directly Reference base class private member}; main () {derived obj; obj.Setxy (10, 20); obj.showxy (); return 0;} Run error! The error message of the VC prompt is Error C2248: 'X': Cannot Access Private Member Declared In Class' What is going on? "What is going on in Class' Base 'See Declaration Of' X '? Just because the private derived class cannot directly reference the base class's private member. C to derived regulations: In the case of public derivation, the access characteristics of all members in the base class remain unchanged in the derived class; in the private sector, all members in the base class are also private members in the derived class. . This is the reason for the error, since X is a private member in the base class, in the private derived, private members in the base class cannot be accessed by the derived class member. As long as we change to: showX (); cout << Y << Endl; we can see the result: 1020 I don't know if I know it, I think it should be class, I should have a class. Inheritance and derived characteristics: 1. In private derived, private members in the base class have inherited in derived classes. All public members can only be a private member of the privately derived class, which can be used by a member of the derived class, but the private member in the base class does not allow member functions of the derived class. 2. In the public. Accessibility of base class members remain unchanged in derived classes! 3. In public derivation, access characteristics of all members in the base class remain unchanged in derived classes; in the private sector, all members in the base class are also private members in the derived class. Ok, inherit is here. I have to learn the polymorphism in tomorrow. ------------------------------------ to do a 100% programmer! Personal description: The son of the farmer, or the farmer! I have lived a mediocrity in the age of 200. I don't want to legend after I am in my year. Poor is the main song, inferior coat, cave, not sold cool, but reality. One year of rice <= 365 * 2.1.5 yuan a meal accounts for 80%.