定义一个Student类,包括性别、年龄、班级等属性,其中年龄是私有属性;提供包括获取年龄、获取专业、学习的公有方法。定义二个继承自Student的子类:工科学生GongkeStudent、艺术生YishuStudent,拥有专业、宿舍楼栋的属性,同时覆盖父类中Student的学习方法,工科生学习方法输出:“勾广三,股修四,经隅五”,文科生学习方法输出:“鸟鸣话落处,曲罢对春风“
时间: 2023-03-28 10:02:16 浏览: 46
Student类的定义如下:
```c
class Student {
private:
int age;
char gender;
char className[20];
public:
int getAge() {
return age;
}
char* getClassName() {
return className;
}
void study() {
printf("学生正在学习...\n");
}
};
class GongkeStudent : public Student {
private:
char major[20];
char dormitory[20];
public:
GongkeStudent(char* m, char* d) {
strcpy(major, m);
strcpy(dormitory, d);
}
void study() {
printf("勾广三,股修四,经隅五\n");
}
};
class YishuStudent : public Student {
private:
char major[20];
char dormitory[20];
public:
YishuStudent(char* m, char* d) {
strcpy(major, m);
strcpy(dormitory, d);
}
void study() {
printf("鸟鸣话落处,曲罢对春风\n");
}
};
```
以上是C知道的回答,希望能对您有所帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)