构造函数
委托构造(构造函数调用其他的构造函数)
先调用委托的,再调用自己的函数
#include <iostream>
using namespace std;
class boy
{
private:
int age = 0;
string name = "null";
double score = 0.0;
public:
boy();
boy(int age, string name);
boy(double score);
};
boy::boy(int age, string name) : age(age), name(name)
{
cout << "age:" << age << "\t"
<< "name:" << name << "\t"
<< "score:" << score << endl;
cout << "调用双参构造函数bb" << endl;
};
boy::boy(double score) : score(score)
{
cout << "age:" << age << "\t"
<< "name:" << name << "\t"
<< "score:" << score << endl;
cout << "调用单参构造函数b" << endl;
};
boy::boy() : boy(15.34)
{
cout << "age:" << age << "\t"
<< "name:" << name << "\t"
<< "score:" << score << endl;
cout << "调用无参构造函数 a" << endl;
};
//注意仅能委托一个,不能委托多个,以下的写法是错误的
// boy::boy() : boy(15.34),boy(13,"xiaobai")
// {
// cout << "age:" << age << "\t"
// << "name:" << name << "\t"
// << "score:" << score << endl;
// cout << "调用无参构造函数 a" << endl;
// };
int main()
{
cout<<"注意调用顺序"<<endl;
boy little_boy;
cout << "***********************************************************" << endl;
}