[C ++] D & amp; A 5 Collection with Inheritance

Source: Internet
Author: User

[C ++] D & A 5 Collection with Inheritance
D & A 5 Collection with Inheritance

Collection & list are all abstract classes that declare different functions respectively.
The above class must be implemented.

Knowledge Point supplement: Virtual inheritance

Virtual inheritance is a technology in object-oriented programming. It refers to a specified base class. In the inheritance architecture, it shares its member data instances to and from this base type.Direct or indirectOther Derived classes.

For example, if Class A and Class B are derived from Class X (non-virtual inheritance and assume Class X contains some data members), and class C inherits from Class A and Class B at the same time, then, the C object will have two sets of X instance data (which can be accessed independently. Generally, an appropriate offset is used ). However, if both Class A and Class B inherit Class X, the object of class C contains only A set of instance data of class X. The typical programming language for this concept is C ++.

This feature is very useful in Multi-inheritance applications, so that the virtual base class can be directly or indirectly derived from it,Has a common base class object instance. Avoid problems caused by ambiguous combinations (such as the issue of diamond inheritance "). The principle is that indirect Derived classes (C) penetrate through their parent classes (A and B in the above example), essentially directly inheriting the virtual base class X.
(From Wikipedia)

class Animal { public:  virtual void eat();};// Two classes virtually inheriting Animal:class Mammal : public virtual Animal { public:  virtual void breathe();};class WingedAnimal : public virtual Animal { public:  virtual void flap();};// A bat is still a winged mammalclass Bat : public Mammal, public WingedAnimal {};

The Animal part in Bat: WingedAnimal is now the same as the Animal part in Bat: Mammal. This means that Bat now has only one shared Animal part, so for Bat :: the call of eat () is no longer ambiguous. In addition, the process of directly assigning a Bat instance to an Animal instance does not produce ambiguity, because there is only one Bat entity that can be converted to an Animal.

Because the starting address of the Mammal instance and the memory offset of its Animal part are not clear until the program runs to allocate memory, the virtual inheritance application creates a virtual table (vtable) for Mammal and WingedAnimal) pointer ("vpointer "). Therefore, "Bat" includes vpointer, Mammal, vpointer, WingedAnimal, Bat, and Animal. There are two virtual table pointers. The object address of the most derived class points to the virtual table pointer and points to the virtual table of the most derived class; another virtual table Pointer Points to the virtual table of the WingedAnimal class. Animal virtual inherited. In the preceding example, one is assigned to Mammal, and the other is assigned to WingedAnimal. Therefore, the memory occupied by each object increases the size of two pointers, but the Animal ambiguity problem is solved. All Bat class objects contain these two virtual pointers, but each object contains a unique Animal object. Assuming that a Squirrel-like statement inherits Mammal, the virtual pointer of the Mammal object in Squirrel is different from that of the Mammal object in Bat, even though they occupy the same memory space. This is because the Mammal-to-Animal distance in the memory is the same. Different virtual tables actually occupy the same space.

Pure virtual functions:

A pure virtual function or pure virtual method is a virtual function that needs to be executed by a non-Abstract derived class. classes that contain pure virtual methods are called abstract classes. abstract classes cannot be called directly. A subclass of an abstract base class is only available to all pure virtual functions in this class (or its parent class) can be called directly. pure virtual methods are usually only declared (Signature) but not defined (implemented ).

(From Wikipedia)

In many cases, virtual functions cannot be meaningful in the base class, but declared as pure virtual functions. Its implementation is left to the derived class of the base class. This is the role of pure virtual functions.

A pure virtual function allows a class to have an operation name without operation content, so that the derived class can be defined in a specific way during inheritance. Classes that contain pure virtual functions are called abstract classes. This type cannot declare objects, but serves as a base class as a derived class. Unless all the pure virtual functions in the base class are fully implemented in the derived class, the derived class also becomes an abstract class and the object cannot be instantiated.

A class maintains a table related to a virtual function-vtable (_ vfptr points to it). The function declaration contains a function with the keyword "virtual" in front, A pointer (function pointer) pointing to the function is created and saved to the vtable. Virtual function tables are used to achieve polymorphism, but they also increase execution efficiency and extra memory space.

Virtual destructor in the virtual base class

Let's take a look at the Code:

#include 
  
   using namespace std;class A{public:    A()    {        cout <<"A..."<
   
  

Output:

A…B…~A…

The destructor of the derived class has not been called. Why?

If the derived class inherits from the base class, the base class will only exist in the derived class until the derived class calls the destructor.

Assume that the destructor of the base class is called earlier than the derived class, which may cause a situation where the class member does not exist, but the class itself is still present, but the class exists, class Members should still exist. So this is a conflict, so the destructor of the derived class will be called first, and the destructor of the base class will be called again.

#include  using namespace std;class A{public: A() { cout <<"A..."<  
A…B…~B…~A…

Summary: If a class does not contain virtual functions, it generally means that it will not be used as a base class. When a class is not used as a base class, do not define virtual destructor because it will add a virtual function table, doubling the volume of the object, it may also reduce its value portability.

So the basic one is:Declaring a virtual destructor for no reason is the same as never declaring it.

Virtual destructor are declared only when the class contains at least one virtual function.

Abstract classes are used as base classes,The base class must have a virtual destructor., Pure virtual functions generate abstract classes, so declare a pure virtual destructor in the class that you want to become an abstract class.

Defining a function as a virtual function does not mean that the function is not implemented, but only to implement polymorphism.

Define a function as a pure virtual function to indicate that the function is not implemented.Implement an Interface. This function must be implemented in the derived class that inherits the abstract class...

Unexpected situation Link error

When I wrote the code for the first time, Xcode gave the following error: linker command failed with exit code 1 (use-v to see invocation)

After reading a lot of information, I found that there wasFunction name duplicationAnd they are all included in main at the same time, so a link error occurs.

Stack Overflow:

Stack Overflow is because the C series does not have a built-in check mechanism to ensure that the data copied to the buffer zone cannot be larger than the buffer zone size. Therefore, when the data is large enough, the buffer range will overflow.

Stack Overflow is a type of buffer overflow. Buffer overflow often leads to unpredictable consequences when useful storage units are rewritten. During the running process of the program, some memory space is usually allocated to the temporary data access needs, which is usually called a buffer zone. If the data written to the buffer exceeds its own length, the buffer cannot be accommodated, it will cause the storage units outside the buffer to be rewritten. This phenomenon is called Buffer Overflow. Buffer? The length is generally related to the type of the buffer variable defined by the user.

Buffer overflow often leads to unpredictable consequences when useful storage units are rewritten. Writing arbitrary data to these units usually only causesProgram crashFor such incidents, we can say at most that this program has bugs. However, if well-prepared data is written to these units, the program flow may be hijacked, causing unwanted code execution and falling into the control of attackers. This is not just a bug, but the Vulnerability (exploit.

When the data is too big and the Code uses too much stack memory, a runtime error occurs. Although there is no error in your IDE.

Summary:

The above are all the knowledge points and problems in the entire question. In general, this question is still relatively difficult. We need to grasp the code efficiency well and reconstruct the code in a timely manner.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.