Polymorphic one of C + + classes (Virtual keyword--a deep understanding of the constructor function)

Source: Internet
Author: User

//Virtual keyword--a deep understanding of the constructor function#include <iostream>using namespacestd;/*C language Compiler, C + + compiler is all static chain, is a piece of code compilation, do not consider the context such as compiling to the void Protecta (point pin) compiler will think the pin is the point type, but not to contact the context, To analyze whether a pin might be a subclass object The virtual keyword automatically detects whether the parent object is a subclass object*//*the function of the virtual keyword assumes that there is a function a in the parent class that is decorated with the virtual keyword and that the function is overridden in a subclass when the variable is a parent class, the C + + compiler detects that the passed variable is a subclass object, and if it passes through a subclass object, then C + + The compiler automatically calls the subclass a with the same name, and if the parent object is passed in, C + + compiles cannot access the subclass's function A with the same name, and can only call function A of the parent class*/classpoint{ Public: Point&2) {cout<<"I am the parent class, the copy constructor is executed 1"<<Endl; //this= Error The type of this is found here is the point *const the this description has been allocated memory space before executing the constructor//further, the constructor knowledge simply assigns a value to the member property and does not allocate memory space//allocating memory space is definitely allocated at the time of definition//Pointa PA; In this definition, the memory is allocated directly to the stack. Call the parameterless constructor initialization//the point *pa=new Point () is defined so that the memory is already allocated in the heap call parameterless constructor initialization//Summary; constructors simply initialize member properties and do not allocate memory          This->x =p.x;  This->y =p.y; } Point (intA =1,intb =1){         This->x =A;  This->y =b; }    Virtual voidPrint () {cout<<"x="<< x <<Endl; }Private:    intx; inty;};classPointa: Publicpoint{ Public:    voidPrint () {cout<<"I am a subclass, I override the member function of the parent class"<<Endl; }};//Assignment Compatibility Principlevoidprotecta (point pin) {pin. Print ();}voidPROTECTC (Point &pin) {pin. Print ();}voidPROTECTD (Point *pin) {Pin-Print ();}voidPROTECTB () {Pointa pa;    Protecta (PA); //It is found that the print is x= garbled------did not call the subclass of the print () function? //This error is due to the protecta (point pin),//Call the Protecta () function,//① Executing the parent copy constructor//② generates a new parent temporary object pin (the PIN is already a parent object, and he executes a copy constructor)//③ The parent class object calls the virtual keyword-decorated function,//C + + compilation searches backwards for subclass properties in the parent memory area, discovering that there are no subclass attributes//The C + + compiler determines this object when the parent class object is called directly to the parent class's print ()PROTECTC (PA); PROTECTD (&PA);}voidMain () {PROTECTB (); System ("Pause");}

Polymorphic one of C + + classes (Virtual keyword--a deep understanding of the constructor function)

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.