子对象数组在构造函数中应该怎么初始化
例如
class person
{
protected:
int id;
student st[3];
int num;
public:
person(string name ,char sex,int age,float score);
void display();
};
你的student类中构造函数怎么写的?
在构造函数里面写
public:
person(string name ,char sex,int age,float score)
{
...
st[0].xxx = xxx;
st[1].xxx = xxx;
}
可以循环遍历数组没,然后一个个给值。
1.建议在student类的构造函数student()中进行合理初始化;2.person类中仍需对student类型的成员数组进行特殊初始化,那就在person类构造函数对数组分别初始化,3.可以用memset进行统一值初始化。
student类成员必须是public成员变量,或者增加public成员函数设置数值。
贴出代码,你怎么写的。构造函数访问私有成员没有问题。