(十)继承四(多继承)

来源:互联网 发布:江西省网络诈骗 编辑:程序博客网 时间:2024/06/03 16:39

(十)继承四(多继承)
2016 6 30
八、多继承重点内容
1、多继承:一个子类有两个或两个以上的父类

2.多继承主要的三个常见问题:
1)不同的父类中有同名函数,如何区分?(二异性)
2)两个父亲有共同的祖先。
3)菱形继承

4.多继承问题1:父类构造函数执行顺序:根据继承顺序来执行。
5.多继承问题2:父类中的同名函数的区分调用:“类名::”来区分同名函数。
6.多继承问题3:菱形继承。(孙子类中出现2份爷爷类中的成员)
使用virtual继承(孙子类直接从爷爷类继承成员,再分别继承父类中各自扩展的成员)

孙子类的构造函数:先调用爷爷类构造函数,再各个父类构造函数,最后孙子类扩展。

#include <iostream>using namespace std;class GrandFather{protected:    int tree;public:    GrandFather(int _tree){tree=_tree;}    void print(){        cout<<"tree="<<tree<<endl;    }};class Father:public virtual GrandFather{    friend ostream& operator<<(ostream& out,Father & f){        out<<"name="<<f.name<<endl;        out<<"age="<<f.age<<endl;        out<<"==============="<<endl;        return out;    }protected:    char *name ;    int age;public:    Father(char* _name,int _age,int _tree):GrandFather(_tree){        name=new char[strlen(_name)+1];        strcpy(name,_name);        age=_age;    }    Father(Father & f):GrandFather(f){        name=new char[strlen(f.name)+1];        strcpy(name,f.name);        age=f.age;    }    virtual~ Father(){        if(name!=NULL){            delete [] name;            name=NULL;        }    }    void print(){        cout<<"name="<<name<<endl;        cout<<"age="<<age<<endl;    }};class Mather:public virtual GrandFather{    friend ostream& operator<<(ostream& out,Mather & f){        out<<"sex="<<f.sex<<endl;        out<<"enjoy="<<f.enjoy<<endl;        out<<"-------------------"<<endl;        return out;    }protected:    char *sex;    char enjoy[20];public:    Mather(char* _sex,char *_enjoy,int _tree):GrandFather(_tree){        sex=new char[strlen(_sex)+1];        strcpy(sex,_sex);        strcpy(enjoy,_enjoy);    }    Mather(Mather &m):GrandFather(m){        sex=new char[strlen(m.sex)+1];        strcpy(sex,m.sex);        strcpy(enjoy,m.enjoy);    }    virtual~ Mather(){        if(sex!=NULL){            delete [ ] sex;            sex=NULL;        }    }//    void print(){//        cout<<"sex="<<sex<<endl;//        cout<<"enjoy="<<enjoy<<endl;////    }};class Son:public Father,public Mather{    friend ostream& operator<<(ostream& out,Son & f){        out<<"work="<<f.work<<endl;        out<<"*************"<<endl;        return out;    }private:    char *work;public:    Son(char *_name,int _age,char *_sex,char *_enjoy,char *_work,int _tree):GrandFather(_tree), Father(_name,_age,_tree),Mather(_sex,_enjoy,_tree){        work=new char [strlen(_work)+1];        strcpy(work,_work);    }    Son(Son &s):GrandFather(s), Father(s),Mather(s){                 /////        work=new char[strlen(s.work)+1];        strcpy(work,s.work);    }    ~ Son(){        if(work!=NULL){            delete [] work;            work =NULL;        }    }    void print(){        cout<<"work="<<work<<endl;        Father::print();        Mather::print();    }};int main(){    Son s((char*)"bb",45,(char*)"girl",(char*)"asdffg",(char*)"IT",10);    cout<<s<<endl;   // s.print();//多个同名函数,    s.print();//使用虚继承,解决菱形继承问题。}
0 0
原创粉丝点击