我是网站的新手(以及编程),所以我希望我能在网站的所有正确指导下适当地发布这个问题。好的,这就是:
所以我对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函数从我的实体类转换为容器类的正确方法,这样我就可以在动态分配的数组中更改每个类对象的各个元素。这些更改将在容器类中的更多公共成员函数中发生,但我完全难倒。我希望这个问题有意义,而且我不是在寻找任何人为我编写所有的setter和getter,我只需要有人指出我正确的语法方向。谢谢所有通过这个的人!
答案 0 :(得分:0)
如果你有这样的东西:
class GradeBook
{
public:
...
Student& student(int idx) { /*some boundary check here*/
return students[idx]; }
}
然后您可以将该方法用作:
GradeBook theBook;
...
auto idOfFirstStudent = theBook.student(0).getStudentID();
您只需要确定该student()方法应返回的内容:它可以返回引用(如上所述)或指向student(实例)的指针。在以后的情况下,您可以在出现超出错误的情况下返回nullptr。在第一种情况下,唯一合理的选择是抛出错误。
答案 1 :(得分:0)
所以这里不需要魔法,但你需要决定你想怎么做。一种方法是写一些类似的东西:
void GradeBook::setCourseText(int i, const string &txt) {
courses[i].setCourseText(txt);
}
顺便说一句,我强烈建议您使用std::vector
和at()
而不是new
。