Object-oriented notes in C ++
Public inheritance is equivalent to a link. For example, class B: public A, which means that B is A, A does, and B does.
Class a {}; class B: public A {}; A a; B B; a = B; // error, because a is not necessarily bb = a; // correct, B is
Under public inheritance, the name will be overwritten: If B and A have functions with the same name, the function in A will be overwritten, including the overload function of the function in.
Virtual Functions
Classes containing pure virtual functions such as virtual void fun () = 0; are abstract classes. We cannot create abstract class objects. If A has A pure virtual function, a A = new A is wrong, and a A = new B is correct.
Pure virtual functions:
The function must be declared again in the derived class. (The purpose of declaring a pure virtual function is to make the derived class inherit only the interface)
To implement pure virtual functions, you need to apply for them, as shown below:
Class a {virtual void fun () = 0 ;}; void A: fun () {// Implementation} class B {virtual void fun () {:: fun (); // The Implementation of A needs to be declared}; class c {virtual void fun (); // You can implement it yourself}; void C: fun () {// C fun}
Non-pure virtual functions:
The derived class inherits the declaration and implementation of the function, which can be implemented by itself to overwrite. The security is caused by the default implementation (that is, the implementation of the base class) when the derived class forgets the UDF inherited from the custom class ). The default function implementation may not be suitable for this derived class.
Non-virtual functions:
Abstract classes (only abstract classes) will force the derived classes to inherit the non-virtual functions of the class, that is, the non-virtual functions of the abstract class will overwrite all functions of the same name of the derived classes, this approach is just opposite to the inheritance of non-abstract classes.
Summary:
Under Public inheritance, pure virtual functions, non-pure virtual functions, and non-virtual functions play the following roles in the inheritance of abstract classes: only interfaces, inherited interfaces and default implementations, inherited interfaces, and forced implementations are inherited.