#include <iostream>
#include <string>
// 基本学生类
class Student {
protected:
std::string name;
int id;
std::string major;
public:
Student(std::string n, int i, std::string m) : name(n), id(i), major(m) {}
virtual void input() {
std::cout << "Enter student name: ";
std::cin >> name;
std::cout << "Enter student ID: ";
std::cin >> id;
std::cout << "Enter major: ";
std::cin >> major;
}
virtual void output() const {
std::cout << "Name: " << name << std::endl;
std::cout << "ID: " << id << std::endl;
std::cout << "Major: " << major << std::endl;
}
};
// 自动化专业学生类
class AutomationStudent : public Student {
private:
double autoControlGrade;
public:
AutomationStudent(std::string n, int i, double grade) : Student(n, i, "Automation"), autoControlGrade(grade) {}
void input() override {
Student::input();
std::cout << "Enter Auto Control grade: ";
std::cin >> autoControlGrade;
}
void output() const override {
Student::output();
std::cout << "Auto Control Grade: " << autoControlGrade << std::endl;
}
};
// 通信专业学生类
class CommunicationStudent : public Student {
private:
double signalSystemGrade;
public:
CommunicationStudent(std::string n, int i, double grade) : Student(n, i, "Communication"), signalSystemGrade(grade) {}
void input() override {
Student::input();
std::cout << "Enter Signal System grade: ";
std::cin >> signalSystemGrade;
}
void output() const override {
Student::output();
std::cout << "Signal System Grade: " << signalSystemGrade << std::endl;
}
};
// 物联网专业学生类
class IoTStudent : public Student {
private:
double iotIntroductionGrade;
public:
IoTStudent(std::string n, int i, double grade) : Student(n, i, "IoT"), iotIntroductionGrade(grade) {}
void input() override {
Student::input();
std::cout << "Enter IoT Introduction grade: ";
std::cin >> iotIntroductionGrade;
}
void output() const override {
Student::output();
std::cout << "IoT Introduction Grade: " << iotIntroductionGrade << std::endl;
}
};
int main() {
AutomationStudent autoStudent("Alice", 12345, 90.5);
CommunicationStudent commStudent("Bob", 67890, 85.0);
IoTStudent iotStudent("Charlie", 54321, 88.0);
autoStudent.input();
commStudent.input();
iotStudent.input();
autoStudent.output();
commStudent.output();
iotStudent.output();
return 0;
}
代码说明:
基本学生类 (Student):
包含学生的基本信息:姓名 (name)、学号 (id) 和专业 (major)。
提供了输入 (input) 和输出 (output) 成员函数。
自动化专业学生类 (AutomationStudent):
继承自 Student,并添加了自动控制原理课程成绩 (autoControlGrade)。
重写了 input 和 output 函数,以包含自动控制原理课程成绩的输入和输出。
通信专业学生类 (CommunicationStudent):
继承自 Student,并添加了信号与系统课程成绩 (signalSystemGrade)。
重写了 input 和 output 函数,以包含信号与系统课程成绩的输入和输出。
物联网专业学生类 (IoTStudent):
继承自 Student,并添加了物联网导论课程成绩 (iotIntroductionGrade)。
重写了 input 和 output 函数,以包含物联网导论课程成绩的输入和输出。
Student.h
#ifndef __STUDENT_H__
#define __STUDENT_H__
class CStu
{
public:
CStu();
CStu(const char* Name, unsigned short Age, const char* Sex, const char* Rem);
CStu(const CStu& s);
~CStu();
private:
char m_Name[11]; // 姓名
char m_Sex[3]; // 性别
unsigned short m_Age; // 年龄
int m_English_Score; // 英语成绩
int m_Math_Score; // 数学成绩
char* m_Rem; // 指针,指向存放简历的地址
protected:
unsigned int m_RemBytes; // 简历占存储空间的字节数
public:
void Show(); // 显示函数
unsigned int GetRemBytes(); // 返回简历所占空间字节数
bool SetName(const char* Name); // 设置姓名
char* GetName(); // 返回姓名
bool SetSex(const char* Sex); // 设置性别
char* GetSex(); // 返回性别
bool SetAge(unsigned short Age); // 设置年龄
unsigned short GetAge(); // 返回年龄
bool SetEnglishScore(int EnglishScore); // 设置英语成绩
int GetEnglishScore(); //返回英语成绩
bool SetMathScore(int MathScore); // 设置数学成绩
int GetMathScore(); //返回数学成绩
bool SetRem(const char* Rem); //设置简历
const char* GetRem(); //返回简历
};
#endif
Student.cpp
#include "Student.h"
#include <iostream>
#include "Student.h"
#include <string>
using namespace std;
int main() {
//使用变量方式创建学生对象
CStu stu1;
CStu stu2("mhx", 21, "男", 0);
stu1.Show(); stu2.Show();
//使用指针方式创建学生对象
CStu* ps1 = new CStu();
CStu* ps2 = new CStu("mhhhx", 22, "男", 0);
ps1->Show(); ps2->Show();
return 0;
}
//Ctrl k + c 注释, Ctrl k + u 取消注释
CStu::CStu() { // 默认构造函数
m_Name[0] = 'x'; // 没有名字
m_Age = 18;
strcpy(m_Sex, "男");
m_Rem = nullptr;
m_RemBytes = 0;
}
CStu::CStu(const char* Name, unsigned short Age, const char* Sex, const char* Rem) {
if (!SetName(Name))
m_Name[0] = 0;
if (!SetAge(Age))
m_Age = 18;
if (!SetSex(Sex))
strcpy(m_Sex, "男");
m_Rem = nullptr;
m_RemBytes = 0;
if (!SetRem(Rem))
SetRem(Rem);
}
CStu::CStu(const CStu& s){
strcpy(m_Name, s.m_Name);
strcpy(m_Sex, s.m_Sex);
m_Age = s.m_Age;
if (!s.m_Rem) {
m_RemBytes = s.m_RemBytes;
m_Rem = new char[m_RemBytes + 1];
strcpy(m_Rem, s.m_Rem);
}
else {
m_Rem = nullptr;
m_RemBytes = 0;
}
}
CStu::~CStu(){
if (!m_Rem)
delete[]m_Rem;
}
void CStu::Show() // 显示函数
{
cout << "姓名:" << m_Name << endl;
cout << "性别:" << m_Sex << endl;
cout << "年龄:" << m_Age << endl;
cout << "英语成绩:" << m_English_Score << endl;
cout << "数学成绩:" << m_Math_Score << endl;
cout << "个人简介: " << m_Rem << endl;
cout << endl;
}
unsigned int CStu::GetRemBytes() // 返回简历所占空间字节数
{
return m_RemBytes;
}
bool CStu::SetName(const char* Name) // 设置姓名
{
if (strlen(Name) >= 10) //姓名最多10字符
return false;
else
strcpy(m_Name, Name);
return true;
}
char* CStu::GetName() // 返回姓名
{
return m_Name;
}
bool CStu::SetSex(const char* Sex) // 设置性别
{
if (strcmp("男", Sex) == 0 || strcmp("女", Sex) == 0)
{
strcpy(m_Sex, Sex);
return true;
}
return false;
}
char* CStu::GetSex() // 返回性别
{
return m_Sex;
}
bool CStu::SetAge(unsigned short Age) // 设置年龄
{
if (Age >= 60) // 学生年龄应该大于60
return false;
else
m_Age = Age;
return true;
}
unsigned short CStu::GetAge() // 返回年龄
{
return m_Age;
}
bool CStu::SetEnglishScore(int EnglishScore) // 设置英语成绩
{
if (EnglishScore < 0 || EnglishScore > 100)
return false;
else
m_English_Score = EnglishScore;
return true;
}
int CStu::GetEnglishScore() //返回英语成绩
{
return m_English_Score;
}
bool CStu::SetMathScore(int MathScore) // 设置数学成绩
{
if (MathScore < 0 || MathScore > 100)
return false;
else
m_Math_Score = MathScore;
return true;
}
int CStu::GetMathScore() //返回数学成绩
{
return m_Math_Score;
}
bool CStu::SetRem(const char* Rem) //设置简历
{
if (m_Rem != NULL)
delete[]m_Rem; //释放原来的空间
if (Rem != NULL)
{
int len = strlen(Rem);
char* p = new char[len + 1];//申请存储空间
if (!p)
return false;
m_RemBytes = len;
m_Rem = p;
strcpy(m_Rem, Rem); // 拷贝数据
}
else
{
m_Rem = NULL;
m_RemBytes = 0;
}
return true;
}
const char* CStu::GetRem() //返回简历
{
return m_Rem;
}