C++ 容器和实体类

标签 c++ containers

我是这个网站(和编程)的新手,所以我希望我能根据网站的所有正确指南适本地发布这个问题。好的,开始了:

所以我对 C++ 还很陌生,正在尝试为程序创建类。我必须构建“容器和实体类”,但我正在努力为容器类中的 getter 和 setter 函数确定正确的语法。所以这是我到目前为止的代码:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

using namespace std;

const int MAX_STUDENTS=100;
const int MAX_COURSES=25;
const int NAME_SIZE=30;
const int COURSE_COLUMNS=4;
const int GRADE_ROWS=10;

//Entity Classes
class Course
{
    //Two private member variables
    private:
          string courseText;
          int courseID;

    public:
      //Constructor
      Course(void)
      {
            //Just providing initial value to the two object variables
            courseText;
            courseID=-1;
      }

      //Setters and Getters for each variable
      string getCourseText(){
        return courseText;}
      void setCourseText(string userEnteredText){
        courseText = userEnteredText;}

      int getCourseID(){
        return courseID;}
      void setCourseID(int userEnteredID){
        courseID = userEnteredID;}  

};

class Student
{
    //Private member variables
    private:
      string studentText;
      int studentID;

      int** coursesAndGrades;
      int enrolledCoursesCount;

      int timesReallocatedColumns;
      int timesReallocatedRows;

    public:
      //Constructor
      Student(void)
      {
        //Just providing initial value to the object variables
        studentText;
        studentID=-1;

        coursesAndGrades = new int*[GRADE_ROWS+1];
        for(int i=0;i<(GRADE_ROWS+1);i++)
        {
            coursesAndGrades[i] = new int[COURSE_COLUMNS];
        }

        enrolledCoursesCount=0;

        timesReallocatedColumns=0;
        timesReallocatedRows=0;
      }


      //Setters and Getters for each variable
      string getStudentText(){
        return studentText;}
      void setStudentText(string userEnteredText){
        studentText = userEnteredText;}

      int getStudentID(){
        return studentID;}
      void setCourseID(int userEnteredID){
        studentID = userEnteredID;}

      int getCoursesAndGrades(int gradeRow, int courseColumn){
        return coursesAndGrades[gradeRow][courseColumn];}
      void setCoursesAndGrades(int gradeRow, int courseColumn, int entry){
        coursesAndGrades[gradeRow][courseColumn]=entry;}


      int getEnrolledCoursesCount(){
        return enrolledCoursesCount;}
      void setEnrolledCoursesCount(int enrolledCount){
        enrolledCoursesCount = enrolledCount;}

      int getTimesReallocatedColumns(){
         return timesReallocatedColumns;}
      void setTimesReallocatedColumns(int reallocColumnCount){
         timesReallocatedColumns = reallocColumnCount;}

      int getTimesReallocatedRows(){
        return timesReallocatedRows;}
      void setTimesReallocatedRows(int reallocRowCount){
        timesReallocatedRows = reallocRowCount;}
};

现在,我有一个名为 GradeBook 的容器类,它包含这两个实体类对象的动态分配数组。

class GradeBook
{
    private:
      Course* courses;
      Student* students;

    public:
      //Constructor
          GradeBook(void)
      {
        courses = new Course [MAX_COURSES];
            students = new Student [MAX_STUDENTS];
      }

}

我正在尝试找出将 setter 和 getter 函数从我的实体类转换为容器类的正确方法,以便我可以更改动态分配数组中每个类对象的各个元素。这些更改将发生在容器类中的更多公共(public)成员函数中,但我完全被难住了。我希望这个问题是有道理的,我不是在寻找任何人为我编写所有的 setter 和 getter,我只需要有人为我指出语法的正确方向。感谢所有挺过来的人!

最佳答案

如果你有这样的事情:

class GradeBook
{

  public: 
    ...

  Student& student(int idx) { /*some boundary check here*/ 
                              return students[idx]; }

}

然后您可以将该方法用作:

GradeBook theBook;
...
auto idOfFirstStudent = theBook.student(0).getStudentID();

您只需要决定 student() 方法应返回什么:它可以返回引用(如上所述)或指向学生(实例)的指针。在以后的情况下,您可以在出现越界错误时返回 nullptr。在第一种情况下,唯一合理的选择是抛出错误。

关于C++ 容器和实体类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23378211/

相关文章:

docker - 为什么要求在 Kubernetes PodSecurityPolicy 冗余中删除所有功能并具有非 root + 禁止权限提升?

docker - 如何快速删除被停止的容器阻塞的多个 <none> docker 图像

c++ - 将递归解决方案转换为迭代解决方案

c++ - DECLARE_DYNAMIC 和 DECLARE_DYNCREATE 之间的区别?

c++ - 为什么 unix-socket 文件在其创建者退出后仍然存在?我可以重复使用它吗?

symfony - 我应该将学说注册表或特定存储库、实体管理器传递到 symfony2 中的 DI 容器吗?

linux - 限制对 Docker 容器的访问

C++ - 交叉谱密度中的复值计算错误

c++ - 安装 dlib 时出错

c++ - 不可复制但可移动的容器