关于类中类成员的初始化
程序员文章站
2022-07-12 22:50:20
...
关于类中类成员的初始化
student类添加一个date类数据成员birthday,date类包含三个数据成员:year、month、day,以及用于初始化的构造函数,用于输入数据的input和输出数据的output成员函数。student类构造函数需要完成birthday的数据初始化
#include <iostream>
using namespace std;
class Data
{
public:
Data(int n, int y, int r) { this->n = n; this->y = y; this->r = r; }
void input(Data& a) { cout << "请输入年月日:" << endl; cin >> a.n >> a.y >> a.r; }
void output(Data& a) { cout << "该学生 生日年月日为\t" << a.n << '\t' << a.y << '\t' << a.r << endl; }
private:
int n, y, r;
};
class Student
{
public:
Student(string name, int id, double score, int n, int y, int r):birth(n,y,r){ this->name = name; this->id = id; this->score = score; }
void input() { birth.input(birth); }
void output() { birth.output(birth); }
private:
string name;
int id;
double score;
Data birth;
};
int main()
{
Student S("a", 88, 99, 2002, 2, 24);
S.output();
S.input();
S.output();
}