Inside C ++ object model-polymorphism (object-oriented)

Source: Internet
Author: User

C ++ supports the following three types of programming paradigms ):

1.Procedural ModelAs programmed in C, and, of course, supported within C ++.

2.ABSTRACT Data Type (ADT) ModelIn which users of the specified action are provided with a set of operations (the public interface), while the implementation remains hidden.

3.Object-oriented (OO) ModelIn which a collection of related types are encapsulated through an abstract base class providing a common interface.

We can see that the procedural mode is equivalent to C programming. This is a good understanding.

The ADT mode encapsulates data in a class by establishing a connection with a group of operation, as previously mentioned. many people think of this pattern as Oo, which is inaccurate. This pattern is calledObject-based (OB)-Nonpolymorphic data types, such as a string class.

Compared with OO, ob does not support polymorphism, and the design speed is faster and the space is more compact. It can be said that, compared with C Programming, ADT has no significant difference in space and efficiency, so it is also very efficient.

But there is no polymorphism in the design, lack of flexibility.

Both OB and OO have their own advocates and critics,

The trade-off usually boils down to one of flexibility (OO) versus efficiency (OB ).

In fact, there is no better one. The two have their own characteristics and have different application fields.

 

Oo-Polymorphism

Next, let's take a look at the essence and polymorphism of the C ++ language OO design.

There is a set of mutually related types encapsulated by an abstract base class, which specifies the common interfaces of this set of types.

The programmer does not need to care about the specific type of the object obtained during the running, but only needs to use the base class pointer or reference to use its common interface (virtual function ). during runtime, this code segment can be applied to any type in this group. Therefore, for programmers, the base class pointer may point to an object that is unknown, which one is also possible.

 

Pointer type

Polymorphism can work. We must use pointers. Why can we use pointers to achieve so flexible features?

First, the pointer is an address. No matter what type of pointer, the size is the same, because it stores a machine address and the size is a machine word.

So there is no difference in memory requirements for different types of pointers. What does this type mean?

A pointer records an address and the first address of a data. It is not enough to know the first address for a data, right?

We also need to know the length of the data so that we can retrieve the data, then the pointer type is to define the length. the pointer type is to tell the compiler how to explain the content stored on this address and the size of the content.

Therefore, the void * pointer can contain an address, but it cannot be used to operate the object. Because you did not tell the compiler what you are pointing to, and the compiler does not know how to obtain it.

Therefore, cast is actually a compiler instruction. It does not change the address value, but only changes the explanation of the memory content and size.

 

Polymorphism

Let's take a look at the following multi-state example.

There is a zooanimal base class, and bear derived class. below is the memory layout of bear class objects,

Pay attention to the memory layout. After the data member of the base class is completed, the virtual table pointer is placed, and then the derived class data members.

This is very important. This memory layout determines the feasibility of polymorphism.

As you can see, for any zooanimal derived class object, the memory layout of the first sizeof (zooanimal) is the same and there will be no difference.

Therefore, you can use the zooanimal pointer to point to any derived class object and use a common interface (through vptr ).

 

However, if you want to use a base class pointer, you cannot directly access the derived class members.

Zooanimal * PZ = & B;

Pz-> cell_block; // fail

For the compiler, if the length of the object you give does not contain cell_block, cast is required to change the pointer interpretation.

// Okay: an explicit downcast

(Bear *) PZ)-> cell_block;

// Better: but a run-time operation

If (bear * master = dynamic_cast <bear *> (PZ) master-> cell_block;

The advantage of dynamic cast is that it extracts the object type from the first slot in the virtual table for verification. If the cast type is not compatible, an error is returned.

This will reduce the efficiency, and the implementation of dynamic_cast is related to the compiler, but its advantage is security. Otherwise, the cast type will lead to program crash, because you may include memory that does not belong to an object, and the compiler strictly retrieves data according to your explanation.

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.