定义一个类 Student管理一个学生的基本信息包括学生姓名、学号、英 语成绩、数学成绩、程序设计语言成绩等数据成员以及对这些数据成员的操作和 平均成绩的计算、学生信息的输出显示。注意:不要改动类成员的定义可以扩充 类成员。 ---------------------------------------------------------------------------------------
注:以下为完整代码
#include
#pragma warning(disable:4996)
class Student { private: char* stu_name; char stu_no[10]; float english_score; float math_score; float program_score; static int ObjectAliveNo; // 静态数据成员,记录当前对象个数 public: Student(); Student(const char* sName, const char* sNo, float e, float m, float p); Student(const Student& s); // 拷贝构造函数 ~Student(); Student& operator=(const Student& s); // 赋值运算符函数 void setName(const char* sName); void setNo(const char* sNo); char* getName(); char* getNo(); void setEnglish(float e); void setMath(float m); void setProgram(float p); float getEnglish(); float getMath(); float getProgram(); float getAvg(); void print(); static void showObjectAliveNo(); // 静态成员函数,输出当前对象个数 };
int Student::ObjectAliveNo = 0;
Student::Student() { stu_name = NULL; stu_no[0] = '\0'; english_score = math_score = program_score = 0; ObjectAliveNo++; cout << "I am the constructor!" << endl; }
Student::Student(const char* sName, const char* sNo, float e, float m, float p) { stu_name = new char[strlen(sName) + 1]; strcpy_s(stu_name, strlen(sName) + 1, sName); strncpy_s(stu_no, sNo, 10); english_score = e; math_score = m; program_score = p; ObjectAliveNo++; cout << "I am the constructor!" << endl; }
Student::Student(const Student& s) { stu_name = new char[strlen(s.stu_name) + 1]; strcpy_s(stu_name, strlen(s.stu_name) + 1, s.stu_name); strncpy_s(stu_no, s.stu_no, 10); english_score = s.english_score; math_score = s.math_score; program_score = s.program_score; ObjectAliveNo++; cout << "I am the constructor!" << endl; }
Student::~Student() { if (stu_name != NULL) { delete[] stu_name; } ObjectAliveNo--; cout << "I am the destructor!" << endl; }
Student& Student::operator=(const Student& s) { if (this == &s) { return *this; } if (stu_name != NULL) { delete[] stu_name; } stu_name = new char[strlen(s.stu_name) + 1]; strcpy_s(stu_name, strlen(s.stu_name) + 1, s.stu_name); strncpy_s(stu_no, s.stu_no, 10); english_score = s.english_score; math_score = s.math_score; program_score = s.program_score; return *this; }
void Student::setName(const char* sName) { if (stu_name != NULL) { delete[] stu_name; } stu_name = new char[strlen(sName) + 1]; strcpy_s(stu_name, strlen(sName) + 1, sName); }
void Student::setNo(const char* sNo) { strncpy_s(stu_no, sNo, 10); }
char* Student::getName() { return stu_name; }
char* Student::getNo() { return stu_no; }
void Student::setEnglish(float e) { english_score = e; }
void Student::setMath(float m) { math_score = m; }
void Student::setProgram(float p) { program_score = p; }
float Student::getEnglish() { return english_score; }
float Student::getMath() { return math_score; }
float Student::getProgram() { return program_score; }
float Student::getAvg() { return (english_score + math_score + program_score) / 3; }
void Student::print() { cout << "姓名:" << stu_name << endl; cout << "学号:" << stu_no << endl; cout << "英语成绩:" << english_score << endl; cout << "数学成绩:" << math_score << endl; cout << "程序设计语言成绩:" << program_score << endl; cout << "平均成绩:" << getAvg() << endl; }
void Student::showObjectAliveNo() { cout << "当前共有" << ObjectAliveNo << "个Student类的对象" << endl; }
int main() { Student s1("张三", "2016211101", 70, 80, 90), s2(s1), s3, * ps1, * ps2; s3 = s1; ps1 = &s1; ps2 = new Student;
s1.print();
s2.print();
s3.print();
ps1->print();
ps2->print();
delete ps2;
Student::showObjectAliveNo();
Student stu[3];
for (int i = 0; i < 3; i++) {
char name[20], no[10];
float e, m, p;
cout << "请输入第" << i + 1 << "个学生的姓名、学号、英语成绩、数学成绩、程序设计语言成绩:" << endl;
cin >> name >> no >> e >> m >> p;
stu[i] = Student(name, no, e, m, p);
}
for (int i = 0; i < 3; i++) {
stu[i].print();
}
return 0;
原文地址: http://www.cveoy.top/t/topic/ebvz 著作权归作者所有。请勿转载和采集!