-
Relationship between the class and the class
-
A combination of relationships: whole and part of
-
Inheritance (paternity)
-
A combination of the characteristics of the relationship
-
Other objects of the class as a member of the class
-
The current members of the same lifetime of objects and object classes
-
Member objects fully consistent with ordinary objects on usage
-
Object-oriented inheritance in the parent-child relationship between classes
-
Subclass has all the attributes and behavior of the parent class
-
Subclass is a special kind of parent class
-
Subclass object can be used as a parent class object to use
-
Subclasses can add no parent class attributes and methods
-
C ++ inheritance relationship described in the following way
. 1 class the Parent 2 { . 3 int Music Videos; . 4 public : . 5 void Method (); . 6 }; . 7 . 8 class Child: public the Parent // describe inheritance relationships 9 { 10 . 11 };
-
Important rules:
-
Subclass is a special parent
-
Subclass object can initialize direct parent class object
-
Subclass object can be assigned directly to the parent object
-
Inherited significance
Inheritance is an important means of C ++ code reuse through inheritance, you can get all the functionality of the parent class and can override existing functionality in a subclass, or add new features
-
summary:
-
Inheritance is a relationship between the object-oriented classes
-
Subclass has all the attributes and behavior of the parent class
-
Subclass object as a parent class object using
-
Subclasses can add no parent class attributes and methods
-
Inheritance is an important means of object-oriented code reuse
Example:
1 // inherit .cpp: This file contains the "main" function. Program execution will begin and end here. 2 // . 3 #include <the iostream> . 4 #include < String > . 5 the using namespace STD; . 6 class the Parent . 7 { . 8 String Age; . 9 String name; 10 String height; . 11 String Sex; 12 is public : 13 is the Parent () 14 { 15 cout << " WE are parent !! " << endl; 16 } 17 Parent(string _age,string _name,string _height,string _sex) 18 { 19 age = _age; 20 name = _name; 21 height = _height; 22 sex = _sex; 23 } 24 void HELLO() 25 { 26 cout << "hello world" << endl; 27 cout << "my name is " << name << endl; 28 cout << "my age is " << age << endl; 29 cout << "my height is " << height << endl; 30 cout << "my sex is " << sex << endl; 31 } 32 }; 33 class Child : public Parent 34 { 35 public: 36 Child() 37 { 38 cout << "we are child" << endl; 39 } 40 }; 41 int main() 42 { 43 44 Parent Father("48","mingxing","180cm","man"); 45 Father.HELLO(); 46 Child CHENGE; 47 //子类可以初始化父类 48 Parent Mother = CHENGE; 49 }
operation result:
hello world
my name is mingxing
my age is 48
my height is 180cm
my sex is man
we are parent!!
we are child