对于类的继承的思考

类的继承的思考

#include <iostream>
using namespace std;

class animal
{
  public:
    animal(void);
    animal(int eye,int ear);
  protected:
    int eeye;
    int eear;
};

animal :: animal(void)
{
  eeye=0;
  eear=1;
}

animal :: animal(int eye,int ear)
{
  eeye=eye;
  eear=ear;
}

class tiger : public animal
{
  public:
    tiger(void);
    tiger(int tieye,int tiear);
    void prit(void);
  private:
    int eteye;
    int etear;
};

tiger :: tiger(void) : animal()
{
  eteye=4;
  etear=5;   
}

tiger :: tiger(int tieye,int tiear) : animal()
{
  eteye=tieye;
  etear=tiear;
}

void tiger :: prit(void)
{
  cout << eteye <<"/t" <<etear<< "/t" <<eeye <<"/t" <<eear <<"/n";
}

main()
{
  tiger aa;
  tiger bb(5,6);
  aa.prit();
  bb.prit();
}

以上是是一个类的继承的简单的小程序,目的是想看下派生类和基类构造函数的情况,以上程序设定了两个基类的构造函数,两个派生类的构造函数。原来的想法是让第一个派生类的构造函数继承第一个基类的构造函数,第二个派生类的构造函数继承第二个基类的构造函数,即

tiger :: tiger(void) : animal()
{
  eteye=4;
  etear=5;   
}

tiger :: tiger(int tieye,int tiear) : animal(int eye int ear)
{
  eteye=tieye;
  etear=tiear;
}

但是那样在编译的时候出项了错误, 

jc.cpp: In constructor `tiger::tiger()':
jc.cpp:37: parse error before `)' token

我想可能是派生类继承基类,但基类中有参数的构造函数要求有参数,因此不能继承的缘故。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值