1.定义3个类: class Date、class Person 、class Student (1)class Date 数据:年、月、日 (protected) 接口:构造函数、输出函数 (public) (2)class Person 数据:姓名、出生日期、年龄、性别 (protected) 接口:构造函数、输出函数(所有数据)(public) (3)class Student 公有继承 Person类 数据:姓名、出生日期、年龄、性别、学号、专业 (protected) 接口:构造函数、输出函数(所有数据)(public) 2.主函数设计要求: 定义一个Student类的静态数组,长度为10 输入3个学生的信息 按行打印3个学生的信息
时间: 2024-03-11 09:49:48 浏览: 83
IntelliJ IDEA引入第三方jar包或查看Java源码的时候报decompiled.class file bytecode version:52.0(java 8)错误的解决办法
以下是类的定义和主函数设计:
```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;
}
};
class Person {
protected:
string name;
Date birthdate;
int age;
char gender;
public:
Person(string n, int y, int m, int d, char g): name(n), birthdate(y, m, d), gender(g) {
age = 2022 - y;
}
void print() {
cout << "Name: " << name << endl;
cout << "Birthdate: ";
birthdate.print();
cout << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
}
};
class Student : public Person {
protected:
string id, major;
public:
Student(string n, int y, int m, int d, char g, string i, string mj): Person(n, y, m, d, g), id(i), major(mj) {}
void print() {
Person::print();
cout << "ID: " << id << endl;
cout << "Major: " << major << endl;
}
};
int main() {
Student students[10];
for (int i = 0; i < 3; i++) {
string name, id, major;
int y, m, d;
char gender;
cout << "Enter student " << i+1 << " name: ";
cin >> name;
cout << "Enter student " << i+1 << " birthdate (year month day): ";
cin >> y >> m >> d;
cout << "Enter student " << i+1 << " gender: ";
cin >> gender;
cout << "Enter student " << i+1 << " ID: ";
cin >> id;
cout << "Enter student " << i+1 << " major: ";
cin >> major;
students[i] = Student(name, y, m, d, gender, id, major);
}
cout << endl;
for (int i = 0; i < 3; i++) {
students[i].print();
cout << endl;
}
return 0;
}
```
在这个程序中,我们定义了三个类:Date、Person、Student。其中,Date类表示日期,Person类表示人员,Student类表示学生,继承自Person类。每个类都有自己的数据和接口。主函数中,我们定义了一个静态数组students来存储学生信息,长度为10。然后,我们输入3个学生的信息,将其存储在数组中,最后按行打印3个学生的信息。
阅读全文