[C + +] D&a 5 Collection with inheritance

Source: Internet
Author: User

D&a 5 Collection with inheritance

Collection & lists are abstract classes that declare different functions, respectively.
This topic requires completion of the above class implementation.

Title Requirements Documents and answers

Knowledge Point supplement: Virtual inheritance

Virtual inheritance is a technique in object-oriented programming, which refers to a specified base class that, in an inheritance architecture, shares its member data instances with other classes that derive directly or indirectly from this base type.

For example, if Class A and class B each derive from Class X (non-virtual inheritance and assume that class X contains some data members), and Class C inherits from classes A and B at the same time, then the object of C will have two sets of X instance data (which can be accessed independently, usually with the appropriate disambiguation qualifier). However, if Class A and B have their own virtual inheritance of Class X, then the object of C will contain only one set of instance data for Class X. The programming language typically implemented for this concept is C + +.

This feature is useful in multiple inheritance applications, allowing the virtual base class to have a common base class object instance for classes that derive directly or indirectly from it. Avoid problems that result from ambiguous combinations (such as "Diamond Inheritance Problems"). The principle is that the indirect derived class (C) penetrates through its parent class (A and B in the example above), essentially inheriting the virtual base class X directly.
(Excerpt from Wikipedia)

Class Animal { Public:Virtual void Eat();};//Classes virtually inheriting Animal:Class mammal: Public VirtualAnimal { Public:Virtual void Breathe();}; Class Wingedanimal: Public VirtualAnimal { Public:Virtual void Flap();};//A bat is still a winged mammalClass Bat: PublicMammal, PublicWingedanimal {};

The animal part of Bat::wingedanimal is now the same as the animal part of Bat::mammal, which means that bat now has only one shared animal part, so the call to Bat::eat () is no longer ambiguous. Also, the process of assigning a bat instance directly to a animal instance is not ambiguous because there is now only one bat entity that can be converted to animal.

Virtual inheritance is applied to mammal and wingedanimal to create a virtual table (vtable) pointer ("Vpointer") because the mammal instance's start address and the memory offset of its animal portion are not clear until the program runs allocated memory. So "Bat" contains vpointer, mammal, Vpointer, Wingedanimal, Bat, Animal. There are two virtual table pointers, where the object address of the most derived class points to the virtual table pointer, pointing to the virtual table of the most derived class, and the other virtual table pointer to the virtual table of the Wingedanimal class. Animal of the virtual inheritance. In the example above, one is assigned to mammal and the other is assigned to Wingedanimal. Therefore, the memory consumed by each object increases the size of two pointers, but resolves the ambiguity of the animal. All Bat class objects contain both virtual pointers, but each object contains a unique animal object. Assuming a class squirrel declaration inherits mammal, the virtual pointer to the mammal object in squirrel and the virtual pointer to the mammal object in bat are different, although they occupy the same amount of memory space. This is because the distance from mammal to animal in memory is the same. Virtual tables are different and actually occupy the same space.

Pure virtual function:

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, and a subclass of an abstract base class can be called directly only if all pure virtual functions are implemented within the class (or its parent class). A pure virtual method is usually only declared (signed) and not defined (implemented).

(from Wikipedia)

In many cases, it is not possible to give a meaningful implementation of a virtual function in a base class, but to declare it as a pure virtual function, and its implementation is left to the derived class of the base class. This is the function of pure virtual functions.

A pure virtual function allows a class to have an operation name first, without manipulating the content, allowing derived classes to specify the definition when inheriting. Any class that contains a pure virtual function is called an abstract class. This class cannot declare an object, only as a base class for a derived class service. Unless all pure virtual functions in the base class are fully implemented in a derived class, the derived class becomes an abstract class and cannot instantiate an object.

A class maintains a virtual function-related table –vtable (__vfptr points to it), and the function declares a function preceded by the keyword "virtual", creating a pointer to the function (function pointer) to be stored in the vtable. The virtual function table is used to implement polymorphism, but it also leads to an increase in execution efficiency and additional memory space.

Virtual destructor in virtual base class

Let's look at the code first:

#include <iostream>using namespace STD;classa{ Public: A () {cout<<"A ..."<<endl; } ~a () {cout<<"~a ..."<<endl; }};classD | Publica{ Public: B () {cout<<"B ..."<<endl; } ~b () {cout<<"~b ..."<<endl; }    };intMain () {A *a =NewB ();DeleteAreturn 0;}

Output:

A…B…~A…

The destructor of the derived class was not called, why?

Derived classes inherit from the base class, the base class will only exist in the derived class until the derived class calls the destructor.

Assuming that the destructor invocation of the base class is earlier than the derived class, one case is that the class member does not exist, and the class itself is still present, but the class member should still exist in the case where the class exists. So this is contradictory, so the destructor of the derived class is called first, and the destructor of the base class is called again.

#include <iostream>using namespace STD;classa{ Public: A () {cout<<"A ..."<<endl; }Virtual~a () {cout<<"~a ..."<<endl; }};classD | Publica{ Public: B () {cout<<"B ..."<<endl; } ~b () {cout<<"~b ..."<<endl; }    };intMain () {A *a =NewB ();DeleteAreturn 0;}
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 intended to be used as a base class, do not define a virtual destructor because it adds a virtual function table, doubling the volume of the object, and possibly reducing its possible value.

So the basic one is: declaring a virtual destructor without reason is as wrong as never declaring it.

Declare a virtual destructor when and only if the class contains at least one virtual function.

Abstract class is prepared to be used as a base class, the base class must have a virtual destructor , pure virtual function will produce abstract classes, so in the class to become abstract class to declare a pure virtual destructor.

Defining a function as a virtual function does not mean that the function is not implemented, just to achieve polymorphism.

Defining a function as a pure virtual function means that the function is not implemented, and it is defined to implement an interface that acts as a canonical function. Inherit the derived class of the abstract class to implement this function ...

Unexpected link error

The first time you finish writing your code, Xcode gives you this error: Linker command failed with exit code 1 (use-v to see invocation)

After flipping through a lot of data, it turns out that there is a function name, and they are also all include to main, so there will be link errors.

Stack Overflow:

Stack overflow is due to the fact that the C language family does not have a built-in check mechanism to ensure that data copied to the buffer is not larger than the buffer size, so when the data is large enough it overflows the buffer range.

A stack overflow is a buffer overflow. The use of buffer overflows to make useful storage units rewritten can often lead to unpredictable consequences. In the process of running the program, in order to temporarily access the data needs, it is generally necessary to allocate some memory space, usually called the buffer space. If you write data to a buffer that exceeds its own length, so that the buffer does not fit, it causes the storage unit outside the buffer to be overwritten, a phenomenon called a buffer overflow. The buffer length is generally related to the type of buffer variable that is defined by the user.

The use of buffer overflows to make useful storage units rewritten can often lead to unpredictable consequences. Writing arbitrary data to these units usually results in an accident such as a program crash , and we can say at most that this program has bugs. But if you write carefully prepared data to these cells, it can cause the program process to be hijacked, causing unwanted code to be executed and falling into the control of the attacker, not just bugs, but vulnerabilities (exploit).

The runtime error occurs when the data is too large and the code uses too much stack memory. Although there is no error on your own IDE.

Summarize:

The above is the whole problem of the various knowledge points and problems. In general, the problem is still relatively difficult. Need to grasp the efficiency of the code is very good, timely refactoring code.

[C + +] D&a 5 Collection with inheritance

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.