首页 > > > 求大神帮忙改进程序,我是c++菜鸟
#ifndef COURSE_H
#define COURSE_H
#include
using namespace std;
class Course
{
public:
Course(const string& courseName, int capacity);
~Course();
string getCourseName() const;
void addStudent(const string& name);
void dropStudent(const string& name);
string* getStudents() const;
int getNumberOfStudents() const;
private:
string courseName;
string* students;
int numberOfStudents;
int capacity;
};
#endif
int main()
{
Course course1("Data Structures", 10);
Course course2("Database Systems", 15);
course1.addStudent("Peter Jones");
course1.addStudent("Brian Smith");
course1.addStudent("Anne Kennedy");
course2.addStudent("Peter Jones");
course2.addStudent("Steve Smith");
cout << "Number of students in course1: "
<< course1.getNumberOfStudents() << endl;
string* students = course1.getStudents();
for (int i = 0; i < course1.getNumberOfStudents(); i++)
cout << students[i] << ", ";
cout << endl;
cout << "Number of students in course2: "
<< course2.getNumberOfStudents() << endl;
students = course2.getStudents();
for (int i = 0; i < course2.getNumberOfStudents(); i++)
cout << students[i] << ", ";
cout << endl;
return 0;
}
#include
#include "Course.h"
using namespace std;
int main()
{
Course course1("C++", 10);
Course course2(course1);
course1.addStudent("Peter Pan");
course2.addStudent("Lisa Ma");
cout << "students in course1: " <<
course1.getStudents()[0] << endl;
cout << "students in course2: " <<
course2.getStudents()[0] << endl;
return 0;
}ps:
添加学生时,若数组容量不够,则创建一个更大的数组,并将内容拷贝到新的数组;
实现dropStudents函数;
添加一个新的函数clear(),删除该课程的所有学生;
在该类中实现自定义的析构函数和拷贝构造函数,达到深拷贝的目的;
编写测试程序,测试实现的类。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/758/viewspace-3158239/ ,如需转载,请注明出处,否则将追究法律责任。