当前位置:网站首页>Discussion on inheritance of C + + learning notes (3)

Discussion on inheritance of C + + learning notes (3)

2020-12-06 10:03:12 osc_ uxgfefy0

Base classes and derived classes

Inheritance allows us to define a class based on another class , This makes it easier to create and maintain an application . To do so , It also achieves the effect of reusing code function and improving execution time .
When creating a class , You don't need to rewrite new data members and member functions , Just specify that the new class inherits the members of an existing class . This existing class is called the base class , The new class is called a derived class .
C++ There are three ways of inheritance , As shown in the following table .
 Insert picture description here
There's not much discussion here about what inheritance is , Here's a discussion of some of the things that come out of inheritance .



inheritance , What does the constructor do

Don't talk much , Code up

#include <iostream>
using namespace std;

class Father
{
   
   
   public:
      Father()
      {
   
   
        cout<<"this is Father()"<<endl;
      }
};

class Son : public Father
{
   
   
   public:
      Son()
       {
   
   
          cout<<"this is son()"<<endl;  
       }
};

int main()
{
   
   
   Son p;
   return 0;
}

Running results

this is Father()
this is son()

In the code above , I am here Father The base class , and Son In this derived class, constructors are added respectively . In the main program defined in a derived class instantiation object . However, it is found from the running results that , Respectively Father The constructor and Son Constructor for , And then come to the conclusion : When defining an instantiated object of a base class , Not only does it execute its own constructor , The constructor of the derived class is also executed .
Now that the constructor is executed , So what's the sequence of their implementation , From the code above , You can see that the constructor of the base class is executed before the derived class . Then there are multiple inheritance relationships , What is their inheritance relationship like ?
For the content not to be so long , Some codes are omitted here

class GrandFather{
   
   ...};
class Mother : public GrandFather{
   
   ...};
class Mr_Wang {
   
   };
class Son : public GrandFather, virtual public Mr_Wang{
   
   ...};

int main()
{
   
   
   Son s;
   return 0;
}

It can be seen from the above procedure that , The inheritance relationship is shown in the figure
 Insert picture description here
here Mr_Wang and Son Is virtual inheritance ( Virtual inheritance is not within the scope of this discussion ), So here it's in red .
The program runs as follows


Mr_Wang
GrandFather
mother
Son

From the running results , It's not hard to see.
Virtual inheritance constructors have the highest priority , secondly , The base class takes precedence over the derived class

Use the base class pointer to the derived class , What's going to happen

Here are two points to discuss

Use the base class pointer to the derived class , What's the order of constructors

Don't talk much , The code is still on

#include <iostream>
using namespace std;

class Father
{
   
   
   public:
      Father()
      {
   
   
        cout<<"this is Father()"<<endl;
      }
};

class Son : public Father
{
   
   
   public:
      Son()
       {
   
   
          cout<<"this is son()"<<endl;  
       }
};

int main()
{
   
   
   Father *p = new Son;
   delete p;
   p = NULL;
   return 0;
}

Execution results

this is Father()
this is son()

From the running results , It's not hard to see. , The base class pointer points to the derived class , The constructor will still construct the base class first , Construct derived classes again .

Use the base class pointer to the derived class , What is the access to a property like

Let's start with the conclusion : Use the base class pointer to the derived class , The pointer can only refer to base class members .
Now modify the code

#include <iostream>
using namespace std;

class Father
{
   
   
   public:
      Father()
      {
   
   
        cout<<"this is Father()"<<endl;
      }
      void test_func_one()
      {
   
   
              cout<<"this is test_func_one";
      }
};

class Son : public Father
{
   
   
   public:
      Son()
       {
   
   
          cout<<"this is son()"<<endl;
       }
      void test_func_two()
      {
   
   
              cout<<"this is test_func_two"<<endl;
      }
};

int main()
{
   
   
   Father *p = new Son;
   p->test_func_two();
   delete p;
   p = NULL;
   return 0;
}

Here I define a pointer to a base class object , Point to the derived class object . Intended to access functions in derived classes . Take a look at the compilation results .

error: ‘class Father’ has no member named ‘test_func_two’; did you mean ‘test_func_one’?
    p->test_func_two();
       ^~~~~~~~~~~~~

obviously , Wrong report . Cannot access . It verifies the conclusion just now .

So what if I have to access derived objects ?

Continue to modify the code

#include <iostream>
using namespace std;

class Father
{
   
   
   public:
      Father()
      {
   
   
        cout<<"this is Father()"<<endl;
      }
      virtual void test_func_one()
      {
   
   
              cout<<"this is test_func_one";
      }
};

class Son : public Father
{
   
   
   public:
      Son()
       {
   
   
          cout<<"this is son()"<<endl;
       }
      void test_func_one()
      {
   
   
              cout<<"this is test_func_two"<<endl;
      }
};

int main()
{
   
   
   Father *p = new Son;
   p->test_func_one();
   delete p;
   p = NULL;
   return 0;
}

Compile and pass , So let's see what happens

this is Father()
this is son()
this is test_func_two

Through the above phenomenon , It's not hard to find out .
If a pointer to a base class object points to a derived class object , You cannot directly access the properties of derived class objects . If you want to access , Virtual functions can be defined in the base class , Redefine the virtual function in the derived class . You can access derived class objects .

This fancy use , Is there any practical use

We can define a template through a base class , Derived classes are used to define something that the template goes up . That may be a little abstract , Take a specific example .
For example, we're going to make cameras , Different types of cameras may have different operations , But the process should be the same . So you can declare some in the base class Init,Start The virtual function of this type of operation serves as a template . Then we can redefine these virtual functions for specific camera models in derived classes .

版权声明
本文为[osc_ uxgfefy0]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201206095830735b.html