[C++基础]在子类中向父类的构造函数传递参数的小例子,包括类中常量的初始化

//基类:
#include <iostream.h>
class Animal()
{
public:
  Animal(int height,int weight)
  {
         cout<<"Animal"<<endl;
    }   
    void eat()   
    {       
         cout<<"animal eat"<<endl;   
    }
};
//子类:
class fish:public Animal()
{
   public:
        fish():Animal(400,300),a(20)//在子类fish中向父类的构造函数传递参数.a(20)是初始化fish类中的常量a
        {            
           cout<<"fish"<<endl;
        }
   private:
       const int a;
};


//说明:在子类中调用父类的带参数的构造函数,在fish类的构造函数后,加一个冒号(:),然后加上父类的带参数的构造函数.这样,在子类的构造函数被调用哪个时,系统就会去调用父类的带参数的构造函数去构造对象. 


原文地址:https://www.cnblogs.com/bbsno1/p/3258342.html