Replication function Order for C + + derived classes, and child parent class compatibility

Source: Internet
Author: User

#include <iostream>using namespacestd;classCfathersum//Parent class Sum{ Public: Cfathersum () {cout<<"1,default Constructing Cfathersum"<<endl;}//Default ConstructsCfathersum (intIREC) {cout<<"1,constructing cfathersum"<<irec<<endl;}//Constructor member initialization};classCfatherbru//Parent Class Bru{ Public: Cfatherbru () {cout<<"2,default Constructing Cfatherbru"<<endl;}//Default ConstructsCfatherbru (intIREC) {cout<<"2,constructing Cfatherbru"<<irec<<endl;}//Constructor member initialization};classCfatherobam//Parent Class Obam{ Public:    //The first line is used to compare the default constructs//cfatherobam (int irec) {cout<< "3,constructing Cfatherobam" <<IREC<<ENDL;}//Constructor member initializationCfatherobam () {cout<<"3,default Constructing cfatherobam*"<<endl;}//Default Constructs};classCSon: PublicCfathersum, PublicCfatherbru, PublicCfatherobam//Inherit parent Sum,bru,obam{ Public: CSon (intIfirst,intISec,intIthir,intIFOR): Cfathersum (Ifirst), M_bru (IFOR), M_sum (Ithir), Cfatherbru (ISEC) {//Cfatherobam (IFOR), M_obam (IFOR) non-default construct, must derive construct pass construction parameter} CSon (ConstCSon &co): Cfatherbru (CO), Cfathersum (CO), Cfatherobam (CO) {}//pass-through, child-parent class compatibility Public: Cfatherobam m_obamcopy;Private: Cfathersum m_sum;    Cfatherbru M_bru; Cfatherobam M_obam;};//Main functionintMain () {CSon son (1,2,3,4); CSon Otherson=Son;    GetChar (); return 0;}

The results are as follows:

Replication order: Copy values are compatible with the child parent class by the copy constructor of the data member of the derived class.

Thinking:

CSon (const CSon &co): Cfatherbru (CO), Cfathersum (CO), Cfatherobam (CO) {}// parameter, child parent class compatible

Pass valid values to the non-default constructor of the parent class when copying



Replication function Order for C + + derived classes, and child parent class compatibility

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.