#include "iostream"
using namespace::std;
class AA
{
public:
int m_a;
int m_b;
double m_c;
AA(){};
//有一个参数的构造函数,初始化m_c
AA(double c)
{
m_c=c+3;//初始化m_c
cout<<"AA(double c)"<<endl;
}
AA(int a,int b)
{
m_a=a+1;
m_b=b+2;
cout<<"AA(int a,int b)"<<endl;
}
//构造函数?
AA(int a,int b,const string&str):AA(a,b)
{
cout<<"m_a="<<m_a<<",m_b="<<m_b<<",str="<<str<<endl;
}
//构造函数?
AA(double const c,const string& str):AA(c)
{
cout<<"m_c="<<m_c<<",str="<<str<<endl;
}
};
class BB :public AA//派生类
{
public:
double m_c;
using AA::AA;//使用基类的构造函数。
//有三个参数的构造函数,调用A(a,b)初始化m_a和m_b,同时初始化m_c
BB(int a,int b,double c):AA(a,b),m_c(c)
{
cout<<"BB(int a,int b,double c)"<<endl;
}
void show()
{
cout<<"m_a="<<m_a<<",m_b="<<m_b<<",m_c="<<m_c<<endl;
}
};
int main()
{
// AA a1(10,20,"我是一只傻傻鸟。");
// AA a2(3.8,"我有一只小小鸟。");
//就使用基类有一个参数的构造函数,参数化m_a;
BB b1(10);
b1.show();
//将使用基类有两个参数的构造函数,初始化m_a和m_b
BB b2(10,20);
b2.show();
//将使用派生类有三个参数的构造函数,调用A(a,b)初始化m_a和m_b,通时初始化m_c
BB b3(10,20,10.58);
b3.show();
}
AA(double c)
m_a=8,m_b=0,m_c=1.22691e-311
AA(int a,int b)
m_a=11,m_b=22,m_c=0
AA(int a,int b)
BB(int a,int b,double c)
m_a=11,m_b=22,m_c=10.58
发表回复