Virtual inheritance constructs (construct) and destructor (destruct) order in C + +

Source: Internet
Author: User
Tags constructor inheritance

Construction (construct) and destructor (destruct) Order of virtual inheritance (virtual inheritance)

In multiple inheritance, the constructor order is virtual inheritance in order, and again, in order, from the base class to the derived class; The deconstruction order is just the opposite;

The code is as follows:

* * * cppprimer.cpp * * Created on:2014.1.10 * author:spike//*eclipse CDT, gcc 4.8.1*/ #include <iostream> #include <string> class Zooanimal {public:zooanimal () {St  
    D::cout << "Zooanimal constructor!" << Std::endl;  
} void Zooprint (void) {std::cout << "Zoo animal!" << Std::endl;}  
      
}; Class Bear:public virtual Zooanimal {public:bear () {std::cout << "bear constructor!" <&lt ;  
    Std::endl;  
} void Bearprint (void) {std::cout << "bear!" << Std::endl;}  
      
};   
    Class Character {Public:character () {std::cout << "Character constructor!" << Std::endl;  
      
}  
}; Class Bookcharacter:public Character {Public:bookcharacter () {std::cout << "Bookcharacter Co  
    nstructor! "<< Std::endl;  
      
}  
};  
 Class Toyanimal {public:   Toyanimal () {std::cout << "toyanimal constructor!" << Std::endl;  
      
}  
}; Class Teddybear:public Bookcharacter, public bear, public virtual Toyanimal {Publ  
    Ic:teddybear () {std::cout << "teddybear constructor!" << Std::endl;  
      
}  
};  
int main (void) {teddybear TB; }

Output:

Zooanimal constructor!  
Toyanimal constructor!  
Character constructor!  
Bookcharacter constructor!  
Bear constructor!  
Teddybear constructor!

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

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.