Qt some miscellaneous notes

                                                                                  QWidget relationship class diagram 

QMainWindow is a main window class with a menu bar and a toolbar, and QDialog is the base class of various dialog boxes, and they all inherit the child QWidget.

C++ inheritance:

One of the most important concepts in object-oriented programming is inheritance. Inheritance allows us to define a class based on another class, which makes it easier to create and maintain an application. In this way, the code reuse function and the effect of improving execution time are also achieved.

When creating a class, you do not need to rewrite new data members and member functions, just specify that the newly created class inherits the members of an existing class. This existing class is called the base class , and the newly created class is called the derived class

Base class & derived class

A class can be derived from multiple classes, which means that it can inherit data and functions from multiple base classes. To define a derived class, we use a class derived list to specify the base class. The class derivation list is named after one or more base classes in the following form:

class derived-class: access-specifier base-class

Among them, the access modifier access-specifier is one of  public, protected  or  private  , and base-class is the name of a certain class defined before. If the access-specifier is not used, it defaults to private.

Suppose there is a base class  Shape , and Rectangle  is its derived class, as shown below:

#include <iostream>
 
using namespace std;
 
// 基类
class Shape 
{
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }
   protected:
      int width;
      int height;
};
 
// 派生类
class Rectangle: public Shape
{
   public:
      int getArea()
      { 
         return (width * height); 
      }
};
 
int main(void)
{
   Rectangle Rect;
 
   Rect.setWidth(5);
   Rect.setHeight(7);
 
   // 输出对象的面积
   cout << "Total area: " << Rect.getArea() << endl;
 
   return 0;
}

Access control and inheritance

The derived class can access all the non-private members of the base class. Therefore, if members of the base class do not want to be accessed by member functions of the derived class, they should be declared as private in the base class.

We can summarize different types of access based on access permissions, as follows:

access public protected private
Same class yes yes yes
Derived class yes yes no
External class yes no no

 

A derived class inherits all base class methods, except in the following cases:

  • The constructor, destructor, and copy constructor of the base class.
  • The overloaded operator of the base class.
  • Friend function of the base class.

Inheritance type

When a class is derived from a base class, the base class can be inherited as  public, protected,  or  private  . The inheritance type is specified by the access-specifier explained above.

We hardly use  protected  or  private  inheritance, usually  public  inheritance. When using different types of inheritance, follow the following rules:

  • Public inheritance (public): When a class is derived from the public base class, the base class's public member of the derived class public members of the base class to protect a member of the derived class to protect members of the base class's private members can not be directly derived class Access, but it can be accessed by calling the public and protected members of the base class .
  • Protected inheritance (protected):  When a class is derived from protecting base class, the base class public and protection of the members of the derived class will be to protect members.
  • Private inheritance (private): When a class is derived from the private base class, the base class public and protection of the members of the derived class will become private members.

public inheritance

protected inheritance

private inheritance

We know that the private and protected members of a class cannot be used outside the class. Only public members can be used directly outside the class.

In the case of public inheritance, the private member derived class of the base class is not available, and the public and protected members of the base class can be used directly in the derived class. Inherited (becomes the corresponding public and protected members of the derived class) only public members are in the derived class It can be used directly outside.

When protecting inheritance, the private members of the base class are still private. The public and protected members of the base class become the protected members of the derived class. At this time, the public members of the original base class cannot be used directly outside the derived class.

In private inheritance , the private members of the base class are still private. The public and protected members of the base class will become private members of the derived class.

for example.

class A

{

public:

   int m_nTelNum;

protected:

   int m_nAge;

private:

   int m_nMoney;

};

class B:public A

{

   void SetTelNum(int nTelNum)

   {

       m_nTelNum=nTelNum;

   }

   void SetAge(int nAge)

   {

       m_nAge = nAge;

   }

   void SetMoney(int nMoney)

   {

       m_nMoney=nMoney;//There is an error here, because the private members of the base class cannot be used.

   }

};

B objB;//Create an object of type B objB

objB.m_nTelNum=123456;//Yes

objB.m_nAge=30;//error. The protected of the base class in public inheritance is protected in the derived class

objB.m_nMoney=100;//More wrong, it can't be used directly in derived classes. It can't be used outside the class.

class C:protected A

{

   void SetTelNum(int nTelNum)

   {

       m_nTelNum=nTelNum;

   }

   void SetAge(int nAge)

   {

       m_nAge = nAge;

   }

   void SetMoney(int nMoney)

   {

       m_nMoney=nMoney;//There is an error here, because this is a private member of the base class and cannot be used.

   }

};

C objC;//Create an object of class C objC

objC.m_nTelNum=123456;//Note the difference between this and public, here is an error, m_nTelNum has become a protected member of class C

objC.m_nAge=30;//error. The protected of the base class in protected inheritance is protected in the derived class, which is the same as public

objC.m_nMoney=100;//More wrong, it can't be used directly in derived classes. It can't be used outside the class.

class D:private A

{

   void SetTelNum(int nTelNum)

   {

       m_nTelNum=nTelNum;

   }

   void SetAge(int nAge)

   {

       m_nAge = nAge;

   }

   void SetMoney(int nMoney)

   {

       m_nMoney=nMoney;//There is an error here, because this is a private member of the base class and cannot be used.

   }

};

D objD;//Create an object of class D objD

objD.m_nTelNum=123456;//error, m_nTelNum became a private member of class D

objD.m_nAge=30;//error. The protected of the base class in private inheritance is private in the derived class

objD.m_nMoney=100;//More wrong, it can't be used directly in derived classes. It can't be used outside the class.

Judging from the example, the three types of inheritance seem to be the same from the reference of the derived class, only when they are referenced outside the class. Now there is no difference between public and protected inheritance.

Let's look at another example.

class E:public B

{

   void SetTelNum(int nTelNum)

   {

      m_nTelNum=nTelNum;//Yes, because this is a public member of B

   }

   void SetAge(int nAge)

   {

      m_nAge=nAge;//Yes, because this is a protected member of B, it becomes a protected member of E

   }

   void SetMoney(int nMoney)

   {

      m_nMoney=nMoney;//This is definitely not possible!

   }

};

E objE; //

objE.m_nTelNum=123456;//Yes

//The other two cannot be used.

class F:public C

{   

   void SetTelNum(int nTelNum)

   {

      m_nTelNum=nTelNum;//Yes, because this is a protected member of C, it is different from public inheritance here but it has not been shown

   }

   void SetAge(int nAge)

   {

      m_nAge=nAge;//Yes, because this is a protected member of C, it becomes a protected member of E

   }

   void SetMoney(int nMoney)

   {

      m_nMoney=nMoney;//This is definitely not possible!

   }

};

F objF;

objF.m_nTel=123456;//error, because this is a protected member of F. Note the difference with class E

class G:public D

{

   void SetTelNum(int nTelNum)

   {

      m_nTelNum=nTelNum;//No, because this is a private member of D, pay attention to the difference here

   }

   void SetAge(int nAge)

   {

      m_nAge=nAge;//No, because this is a private member of D, pay attention to the difference

   }

   void SetMoney(int nMoney)

   {

      m_nMoney=nMoney;//This is definitely not possible!

   }

};

//The G has no inherited members that can be referenced outside the class!

//These inheritance methods are difficult to understand. The best way is to write more code to try.

Label is built on the heap, and app is built on the stack. This means that the label will be destroyed after the app. In other words, the life cycle of the label is longer than the life cycle of the app. This is a taboo of Qt programming .

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_36171263/article/details/88170917