虚基类 隐示转换 派生类的复制构造函数 派生类中使用作用域分辨符可直接调用不同参数类型的同名函数,但不是重载 调用顺序

#include<iostream>
#include<cstring>
using namespace std;
class zero{
  int ending;
  public:
    zero(int love):ending(love){  }
    zero(){ending=0;}
    void different(){cout<<"i am number zero"<<endl;}
    void difference(int hua){cout<<"i am number zero copy"<<hua<<endl;}
    zero(const zero &hao){  ending=hao.ending;}
    void show(){ cout<<ending<<endl;}

};

class one:virtual public zero{
  private:
    int i;
protected:
    int j;
  public:
    one(int z,int q):j(q),i(z){cout<<"one i:"<<i<<"\tone j:"<<j<<endl;}
    void difference(){cout<<"i am number one!"<<endl;}
    one (const one &hehe):zero(hehe){i=hehe.i ;j=hehe.j;}    // 兼容性规则  指针 与 引用 都适用 // 派生类指针 或 派生类引用 都可替代 对应基类
    using zero::difference;   //  只需要把函数名写进就可  无论函数是否带参       把作用域包含进来 就可以随意访问基类的同名函数,要求才同重载一样
    };

class two:virtual public zero{
  private :
    int ii;
protected:
    int jj;
  public:
    two(int z,int q):ii(z),jj(q){cout<<" two i:"<<ii<<"\ttwo j:"<<jj<<endl;}
    void difference(){cout<<"i iam number one!"<<endl;}
    };





   class three:public one,public two   //这里的顺序决定基类构造函数调用顺序
   {
   private :
    int ba;
protected:
    int la;
  public:
    three(int z,int q,int a,int b,int c,int d):ba(z),la(q),one(a,b),two(c,d)   // 1.传进参数顺序不需要与右边初始化列表对应 2.当初始化时,构造函数调用先后与
    {cout<<" three i:"<<ba<<"\ttwo j:"<<la<<endl;}                             //这里得顺序无关
     int show(){cout<<j<<"\t"<<jj<<endl;}
     void difference(){cout<<"i iam number three!"<<endl;}

   } ;
//   void truth(zero *oba){  oba->difference();  cout<<endl;}   //派生类的隐示转换  实例

int main(void)
{
   // three oba(1,2,3,4,5,6);
   //  oba.show();
   // oba.one::difference();  //同名函数的访问方法
   //  oba.zero::difference();
   //  oba.different();    // 第二代 共享同一个基类时  可用virtual 只保留 同基类派生的一个副本
  // oba.difference();   // 隐藏规则 外层同名标识符在内层不可见 除非使用作用域分辨符   派生类中 派生类所有数据和函数成员均在内层,基类都在外层
                     //  接上 : 函数重载 仅在相同作用域内有效  即使 派生类同名函数与父类有不同参数个数或形式,依旧需要用作用域分辨符来区分
                        //或在 派生类 public处   加入    using 基类名::函数名   来直接引用,前提是参数个数不同或类型不同
   //truth(&oba);      //派生类 隐示转换 基类 实例
    one ok(3,4);
    one copyok(ok);  //复制构造函数 相当于 另一种构造函数
    copyok.show();
    ok.difference(2);
    ok.difference();
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值