Three features of C + + based on polymorphism (customer, don't go, I'm mutton, not dog)

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > Polymorphism: Various forms </span>

For example, you have two fun functions, the first class fun function is to print 1, the second class of fun is the function of printing 2, you build a class object, tune fun, want to print 1 can print 1, want to print 2 can print 2, this is polymorphic, this is a variety of forms, That's what we need to achieve in our code.

The nature of polymorphism: Flexible multiplexing

The premise of polymorphism: On the basis of inheritance

Polymorphic implementations: Virtual functions (Some people overload these also as polymorphic, static polymorphism, this kind of thing to see personal understanding, do not buckle these words, the following is the heavy---virtual function)

Virtual function Keyword: virtual

Knowledge Point One:

Class A                 void A::fun ()      class b:public A<span style= "White-space:pre" ></span>void B::fun ()
{<span style= "white-space:pre" ></span>{<span style= "White-space:pre" ></span>{<span Style= "White-space:pre" ></span>{
Public:<span style= "White-space:pre" ></span>printf ("1"); <span style= "White-space:pre" ></ Span>public:<span style= "White-space:pre" ></span>printf ("2");
virtual void fun (); <span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></span >virtual void Fun (); <span style= "White-space:pre" ></SPAN>}
};<span style= "White-space:pre" ></span>};
b *p = new B;   P->fun ();
Requirements: Do not touch the last line of code, how can be transferred to a fun ();

Let's talk about the principle of virtual function

When there is a virtual function in the class, the object that constructs the class, the first four bytes of the object space is the virtual table address, the virtual table is found by the virtual table address, and the virtual table has the address of all the virtual functions of this class.

When you inherit a class that has a virtual function, for example, Class B above, the first four bytes of the Class B object has a virtual table of bytes, if Class B has its own virtual function, the first four bytes of Class B object is the address of the virtual table, the address finds the virtual function address of its own class----if Class B does not have a virtual function of its own class, it still has virtual table The virtual function address found by the virtual table is the address of the parent class (that is, the Class A) virtual function

If you want to change the situation above, just wipe off the fun function of Class B.

Knowledge Point two:

Have you noticed that my child's fun is preceded by a virtual function keyword?

Prevent effects

If one day a class C inherits my present subclass Class B, the Class B becomes the parent class, the C class object, the C class object has its own fun function, my request is to call the C class fun function, but if someone is disruptive or unclear, will get the C class object address of the pointer to a class B pointer, That's the fun function of Class B.

Knowledge Point three: Virtual destruction (to cope with the situation below)

What's the problem with this question?

CALSS a<span style= "White-space:pre" ></span>class B
{<span style= "white-space:pre" ></span>{<span style= "White-space:pre" ></span>A *p = new B;
Public:<span style= "White-space:pre" ></span>public:<span style= "White-space:pre" ></span> Delete p;
Char *p;<span style= "White-space:pre" ></span>char *t;
A () {p = new Char[20];<span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></ Span>b () {t = new char[20];}
~a () {delete p;} <span style= "White-space:pre" ></span>~b () {<span style= "white-space:pre" ></span>delete t;}
};<span style= "White-space:pre" ></span>};
Let's take a look at the disassembly.


Here CS is a subclass, that is, the above B, the above code execution assembly is so, see 01001757 lines of code calls the subclass of the destructor

So this question is a member of Class B, new space leaks out.

When we add a virtual look at the assembly before the destructor of the parent class

01001878 the contents of the called edx are then reconstructed from the last parent class after the sub-class is destructor

Virtual analysis, the deconstruction of the tune class---the analysis of the parent class

Order why is the first son stepfather it---you want to use the sub-class, the destruction of the parent class is not a wild pointer ~ ~

Virtual destruction is a good habit

Knowledge Point four: pure virtual function

When the virtual function declaration is followed by = 0, this class is an abstract class, the function is pure virtual function

Classes that have pure virtual functions cannot build objects

If HR asks me pure virtual function, I say =0 and cannot build object of this class, I do not say abstract class these concepts, what is called abstract

When to use pure virtual function, this is the class you are sure will not build objects, it is written as pure virtual function

Three features of C + + based on polymorphism (customer, don't go, I'm mutton, not dog)

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.