不使用vector的c++动态类
c++ dynamic classes without using vector
所以我碰巧完成了我的家庭作业程序,但今天在讲座上,我的好教授告诉我们,我们不允许使用STL作为向量或列表来创建数据库。我们还被告知,我们需要所有的变量都是私有的。我做这个程序完全错了。这就是我到目前为止所拥有的。
h级
class Student {
private:
string last;
string first;
int student_id;
int enroll_id;
int *grades;
}
class Gradebook {
public:
Gradebook();
void newCourse(Gradebook *info);
private:
string name;
int course_id;
int count_course;
int enroll;
Student *students;
public:
//Constructor
}
我知道我可以通过使用构造函数访问Gradebook
的私有成员,这样我就可以设置Gradebook
中的每个成员。
创建newCourse
的函数
Gradebook::Gradebook() {
students = new Student;
course_id=0;
count_course=0;
enroll = 0;
}
Gradebook::newCourse(Gradebook *info) {
int i, loop=0;
cout << "Enter Number of Courses: ";
cin >> loop;
info = new Gradebook[loop];
for(i=0; i<loop; i++) {
cout << "Enter Course ID: ";
cin >> info[info->count_course].course_id;
cout << "Enter Course Name: ";
cin >> info[info->count_course].name;
info->count_course++
}
}
所以课程现在已经设置好了。由于Student
中的变量是私有的。我不能只使用指向变量的指针来访问它们。有人能教我怎么做吗?
好的,我不知道该怎么问这个问题,但我确实回答了。但我想让大家对我的方法有意见。
class Student {
public:
void setID(int ID){student_id = ID; };
int getID(){return student_id);
private:
string last;
string first;
int student_id;
int enroll_id;
int *grades;
};
class Gradebook {
public:
Gradebook();
void newCourse(Gradebook *info);
void newStudent(Gradebook *info);
private:
string name;
int course_id;
int count_course;
int count_student;
int enroll;
Student *students;
public:
//Constructor
}
void Gradebook::newStudent() {
int i, loop=0;
int student=0;
string lastName;
cout << "Enter number of students: ";
cin >> loop;
for(i=0; i<loop; i++) {
cout << "Enter Student ID: ";
cin >> student;
info->students[info->count_student].setID(student);
cout << "Enter Last Name: ";
cin >> lastName;
info->students[info->count_student].setLast(lastName);
info->count_student++;
}
}
这样做有什么不对吗?
edit:不能将"Student*students"用于多个students的容器。。。
你可以使用自己的列表。类似的东西
struct Participant{ // or class (and maybe more clever name)
Student *student;
Participant *prev;
Participant *next;
};
你必须做一些小技巧,但也许这就是这个练习的想法。。和前面的答案一样,在学生类中使用get和set函数。
好的,很抱歉,您的代码一团糟。。。我不能为你做这个家庭作业,但这里有一些脑海中的提示
-
你确定为学生、课程和成绩册制作不同的课程不会更容易吗?我不知道你的家庭作业规范,所以我不能确定你的程序到底应该做什么
-
您不能使用int*成绩来存储一个学生的所有成绩。学生也是如此。你不能像数组students[i].something()那样访问itect*students若您使用像Participant这样的帮助类(或结构),则必须通过循环中的迭代来找到合适的学生。注意,你已经在课堂上存储了"第一个"参与者,并在你的学生计数中(也在课堂上)存储了"内部学生"
Participant *current = first; int id = Id; // Id is from function/method call for(unsigned int i = 0; i < students; ++i){ if(current->getId()== id){ // do something and break; } else if(current->next != NULL){ // test this just in case current = current->next; } else break; // something went wrong }
最好将所有学生存储在一个列表中(你制作的),并在课程或成绩册中或任何时候使用指针。。。如果你知道"静态",这里是一个提示
class Student{
public:
. // get and set functions
static Student* getStudent(int id); // return NULL if no student with id
private:
static Student *first;
static Student *last;
static unsigned int counter; // helps with loops, but you can always use next != NULL
Student *next;
Student *prev;
}
将其放入Student构造函数中对于您创建的第一个学生,您将first和last设置为指向他/她,next和prev设置为NULL。之后,当你创建一个新学生时,你总是设置
this->prev = last;
last->next = this;
last = this;
this->next = NULL;
- 不要在类内实现程序逻辑。在main()或其他函数中执行,但我认为main()目前还可以。如果你需要在成绩册中添加新学生,请在main()中询问所有必要的信息,并制作一些函数,如成绩册::newStudent(int&Id,string&last_name){//将学生信息存储在成绩册}
通常,这些家庭作业编程练习不必过于花哨、精简或完全优化。所以不要做得太过火;)
class Student {
private:
string last;
string first;
int student_id;
int enroll_id;
int *grades;
public:
string &getLast(){ return Last; }
...
...
};
当您需要访问last
变量等时,请致电Student::getLast()
或void setLast(string sLast){last = sLast;}
用于书写和
读取的string getLast(){return last;}
动态阵列示例:
struct Student;
int Count = 0;
Student *Students = nullptr;
int AddStudent(Student nStudent)
{
Student *Buffer = new Student[Count + 1];
for (int a = 0; a < Count; a++)
Buffer[a] = Student[a];
Buffer[Count] = nStudent;
if(Students)
delete[] Students;
Students = Buffer;
return ++Count -1
}
void RemoveStudent(int Index)
{
Student *Buffer = new Student[Count - 1];
for (int a = 0; a < Index; a++)
Buffer[a] = Students[a];
for (int a = Index; Index < Count - 1; a++)
Buffer[a] = Students[a - 1];
if (Students)
delete[] Students;
Students = Buffer;
}
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- C++中的动态铸造故障
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 内联映射初始化的动态atexit析构函数崩溃
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 控制允许动态运行c++的并发操作数
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 输出没有重复元素的动态数组(收缩数组)C++
- C++为线程工作动态地分割例程
- 正在插入动态数组
- 在c++中使用动态分配的问题
- C++中的动态对象与非动态对象
- 如何在动态数组上使用搜索函数
- 视觉studo 2019中的漫画和静态/动态绑定
- 从C++中的数字输入动态创建矩阵
- 如何从QToolBox中动态创建的QLineEdit中获取文本
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁