The concept of inheritance and significance

  • Relationship between the class and the class
  1. A combination of relationships: whole and part of
  2. Inheritance (paternity)
  • A combination of the characteristics of the relationship
  1. Other objects of the class as a member of the class
  2. The current members of the same lifetime of objects and object classes
  3. Member objects fully consistent with ordinary objects on usage
  • Object-oriented inheritance in the parent-child relationship between classes
  1. Subclass has all the attributes and behavior of the parent class
  2. Subclass is a special kind of parent class
  3. Subclass object can be used as a parent class object to use
  4. Subclasses can add no parent class attributes and methods
  5. 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:
  1. Subclass is a special parent
  2. Subclass object can initialize direct parent class object
  3. 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:
  1. Inheritance is a relationship between the object-oriented classes
  2. Subclass has all the attributes and behavior of the parent class
  3. Subclass object as a parent class object using
  4. Subclasses can add no parent class attributes and methods
  5. 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
 

Guess you like

Origin www.cnblogs.com/chengeputongren/p/12240682.html