xybxbh 2015-05-14 13:19 采纳率: 0%
浏览 1604
已结题

初学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;

}


要求:添加学生时,若数组容量不够,则创建一个更大的数组,并将内容拷贝到新的数组;
实现dropStudents函数;
添加一个新的函数clear(),删除该课程的所有学生;
在该类中实现自定义的析构函数和拷贝构造函数,达到深拷贝的目的;

编写测试程序,测试实现的类。

  • 写回答

4条回答 默认 最新

  • 想飞的小小菜鸟 2015-05-14 16:22
    关注

    第3 行include哪个头文件?
    还有:麻烦写上注释和增加缩进,不然没什么人会看的……
    建议使用链表而不是数组,那样删除和添加就轻松很多
    建议数组的话clear()就来个for循环就可以将学生清零,链表就删除节点
    而深拷贝指的是拷贝类里面的指针时
    你这哪来的两个main函数?菜鸟搞不懂飘过。
    其实我也是新手,请多指教

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?