1.定义3个类: class Date、class Person 、class Student (1)class Date 数据:年、月、日 (protected) 接口:构造函数、输出函数 (public) (2)class Person 数据:姓名、出生日期、年龄、性别 (protected) 接口:构造函数、输出函数(所有数据)(public) (3)class Student 公有继承 Person类 数据:姓名、出生日期、年龄、性别、学号、专
时间: 2023-07-10 18:38:34 浏览: 105
业(protected) 接口:构造函数、输出函数(所有数据)(public)
以下是代码实现:
```c++
#include<iostream>
#include<string>
using namespace std;
class Date {
protected:
int year, month, day;
public:
Date(int y, int m, int d) : year(y), month(m), day(d) {}
void print() {
cout << year << "/" << month << "/" << day << endl;
}
};
class Person {
protected:
string name;
Date birthdate;
int age;
char gender;
public:
Person(string n, Date b, int a, char g) : name(n), birthdate(b), age(a), gender(g) {}
void print() {
cout << "Name: " << name << endl;
cout << "Birthdate: ";
birthdate.print();
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
}
};
class Student : public Person {
protected:
string student_id;
string major;
public:
Student(string n, Date b, int a, char g, string id, string m) : Person(n, b, a, g), student_id(id), major(m) {}
void print() {
Person::print();
cout << "Student ID: " << student_id << endl;
cout << "Major: " << major << endl;
}
};
int main() {
Date d(2000, 10, 1);
Student s("Tom", d, 20, 'M', "20191001", "Computer Science");
s.print();
return 0;
}
```
输出结果:
```
Name: Tom
Birthdate: 2000/10/1
Age: 20
Gender: M
Student ID: 20191001
Major: Computer Science
```
阅读全文