C ++ Lesson 9 --- stunning inheritance
I. Concept of Inheritance
1. Inheritance in the object-oriented model refers to the parent-child relationship between classes.
2. subclass has all member variables and member functions of the parent class
3. Subclass is a special parent class.
4. Subclass objects can be used as parent objects
5. Subclass can have methods and attributes not available to the parent class
Inherit small instances:
# Include <iostream> # include <stdio. h> using namespace std; class Parent {private: int I; public: Parent () {I = 100 ;}}; class Child: parent/* Child inherits Parent */{}; int main (int argc, char ** argv) {ChildC; return0 ;}
6. Access Level and inheritance in C ++
The access level setting during inheritance affects the access level of members.
Class Child: Parent/* Child inherits Parent */{}; ========> class Child: private Parent/* Child inherits Parent */{};
7. Note:
In C ++, class inheritance is private by default.
The subclass inherited by private has all the members of the parent class.
Private inheritance changes all members of the parent class to private Members in the subclass.
8. Access Level and inheritance in C ++
Public inheritance
The parent class members maintain the original access level in the subclass.
Private inheritance
The parent class member becomes a private member in the subclass.
2. Is the access level of class members only public and private enough?
1. protected member of the class
The protected member can be accessed in the subclass, but cannot be accessed from outside.
The access permission of a protected member is between public and private.
Example:
#include <cstdlib>#include <iostream>#include <stdio.h> using namespace std;class Parent{ protected: int i; public: Parent() { i= 100; } void print(void) { std::cout<<"i = "<<i<<std::endl; } };class Child:public Parent{ private: int j; public: void print(void) { std::cout<<"i = "<<i<<std::endl; }}; int main(int argc, char **argv){ ChildC; C.print(); getchar(); return0; }
So how should we choose to use the inheritance method?
1. Principle of setting access level for Class Members
2. Members that need to be accessed are directly set to public.
3. Only members that can be accessed in the current class can be set to private.
4. Set the members that can only be accessed in the current class and subclass to protected.
The private member still exists in the subclass, but cannot be accessed.
Inheritance and access level formula:
3. A deep instance of inheritance and access
# Include <iostream> using namespace std; class A {private: int a; protected: int B; public: int c; A () {a = 1; B = 2; c = 3;} void setValue (int a, int B, int c) {this-> a = a; this-> B = B; this-> c = c ;}}; class B: private A {public: void print () {// cout <"a =" <a <endl; /* a isprivate */cout <"B =" <B <endl; cout <"c =" <c <endl ;}; class C: protected A {public: void print () {// cout <"a =" <a <endl; /* a is private */cout <"B =" <B <endl; cout <"c =" <c <endl ;}}; class D: public A {public: void print () {// cout <"a =" <a <endl; /* a is private */cout <"B =" <B <endl; cout <"c =" <c <endl ;}}; int main (int argc, char ** argv) {Aaa; Bbb; Ccc; Ddd; aa. c = 100; // bb. c = 100;/* private inheritance. All members become private * // cc. c = 100;/* protects inheritance and cannot be referenced outside the class */dd. c = 100 ;}
In the code above, the key points to be noted have been commented out. It is easy to understand the table above.
Vi. Summary:
1. inheritance is the relationship between classes, and subclass is a special parent class.
2. Subclass inheritance can be used to obtain all members of the parent class.
3. private Members can inherit from the quilt class, but cannot access the quilt class.
4. protected members can only be accessed in the current class and subclass.
5. Different inheritance methods may change the access attributes of inherited members.
New Concept 3 Lesson 9 understanding Question 3 cats can have ve greatly falls mainly because __
The penultimate sentence in the second paragraph of the article is option A. The reason why C is incorrect is that the answer is incomplete.
Please carefully read the last few sentences In Section 2: In a long drop, they reach speeds of 60 miles an hour and more. at high speeds, falling cats have time to relax. they stretch out their legs like flying squirrels. this increases their air-resistance and reduces the shock of impact when they hit the ground.
All the answers to the ninth lesson in the second exercise book of the New Concept
Ishare.iask.sina.com.cn/...ormat=