主文件
#include <iostream>
#include "GradeBook.h"
using namespace std;
int main()
{
GradeBook myGradeBook; // 创建一个对象
cout << "请输入课程名称:" ;
string courseName;
cin >> courseName;
cout << "请输入学生人数" ;
int studentCount;
cin >> studentCount;
myGradeBook.resizeGrades(studentCount); // 调用动态分配数组
myGradeBook.setStudentCount(studentCount);
//cout <<"The number of the student is:"<< myGradeBook.getStudentCount() << endl;
myGradeBook.setCourseName(courseName); // 调用类里面的函数
//cout <<"The name of this Grade Book is:"<< myGradeBook.getCourseName() << endl;
myGradeBook.print();
return 0;
}
GradeBook.cpp文件,完成其相应的功能。
#include "GradeBook.h"
using namespace std;
void GradeBook::setCourseName(string name)
{
courseName = name;
}
void GradeBook::setStudentCount(int id)
{
studentCount = id;
}
string GradeBook::getCourseName()
{
return courseName;
}
int GradeBook::getStudentCount()
{
return studentCount;
}
void GradeBook::displayMessage()
{
cout << "Welcome to the GradeBook" << endl;
}
void GradeBook::resizeGrades(int newsize) // 调整成绩数组大小
{
grade.resize(newsize); // 使用vector的resize方法
}
void GradeBook::print()
{
cout << "The number of the student is:" << getStudentCount() << endl;
cout << "The name of this Grade Book is:" << getCourseName() << endl;
}
GradeBook.h文件
#ifndef GRADEBOOK_H
#define GRADEBOOK_H
#include <iostream>
#include <vector>
using namespace std;
class GradeBook // GradeBook is the class namespace
{
public: // open to the users
void print();
void setCourseName(string name);
void setStudentCount(int id);
void resizeGrades(int newsize); // 调整成绩数组大小
string getCourseName();
int getStudentCount();
private: // not open to the users
string courseName; // string 字符串
int studentCount;
//double grade[];
vector<int> grade; // 使用vector动态管理数组
};
#endif // gradebook