Summary of C + + review 11--Polymorphism (ii)

Source: Internet
Author: User

Can you call virtual functions in a constructor to achieve polymorphism?

1) When is the Vptr pointer in the object initialized?

The vptr pointer is initialized by the compiler when the object is created.

Only when the object's construction is completely finished will the VPTR's point be finalized.

Parent-Class object Vptr point to parent virtual function table

The vptr of the subclass object points to the subclass virtual function table

Class parent{public:parent (int a=0)//The print function of the call at the time of execution is still a function of the parent class (at which point the vptr pointer to the virtual function table of the parent class) {this->a = A;print ();} virtual void print () {cout<< "I am the Father" <<endl;} Private:int A;}; Class Child:public parent{public:child (int a = 0, int b=0):P arent (a)//executes the parent class constructor first, returns the subclass after execution (vprt pointer refers to the subclass virtual function table) {this->b = b ;p rint ();} virtual void print () {cout<< "I am the Son" <<endl; Private:int b;}; void Howtoplay (Parent *base) {base->print ();//polymorphic occurs//2 hands}void main () {child C1;//define a subclass object, in which case Call virtual function in parent class constructor can the print occur polymorphic? System ("pause"); return;}

When two parent class pointer steps are inconsistent with the child class pointer step size

Class parent{public:parent (int a=0) {this->a = A;} virtual void print () {cout<< "I am the Father" <<endl;} Private:int a;};/ /success, an accidental success, inevitable failure more terrible class child:public parent{public:/*child (int a = 0, int b=0):P arent (a) {this->b = B;print ();} */child (int b = 0):P arent (0) {//this->b = b;} virtual void print () {cout<< "I am the Son" <<endl; Private://int b;}; void Howtoplay (Parent *base) {base->print ();//polymorphic occurs//2 hands}void main () {child C1;//define a subclass object, in which case Call virtual function in parent class constructor can print occur polymorphic?//c1.print (); Parent *pp = NULL; Child *pc = NULL; Child array[] = {Child (1), Child (2), Child (3)};pP = array;pc = Array;pp->print ();p c->print (); polymorphic occurrence pp++;p C + +;pP->print ();p c->print (); polymorphic occurrence pp++;p C + +;pP->print ();p c->print (); polymorphic occurrence cout<< "Hello ..." <<endl;system ("pause"); return;}

The above is the C + + Review Essentials summary 11-polymorphism (ii) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.