因此,我下面有一个计划的一部分,该计划管理校务委员会内的一系列学校。每所学校都有大量动态分配的学生及其信息,例如姓名和性别。每个学校都有一个特定的ID号。
另外,控制类允许用户创建一个新学生,并根据他们输入的ID将其添加到特定学校。
因此,例如,一个用户的输入可能是这样:
在下面输入数据:
[学校编号:1001
名称:艾玛·道尔
性别:女性
因此,由于用户输入了'1001',因此应创建一个名为Emma的女学生并将其添加到ID为1001的现有学校中(在本例中为schoolTwo对象,因为下一个ID会增加1,如图所示在School.cc中)。假设ID的唯一范围是1到10,并且用户输入的ID不存在,例如“ 1002”,则什么也不会发生,也不应添加任何学生。
因此,如果我要在schoolTwo中打印所有内容,则应该打印Emma Doyle和Johnny Doe。
我遇到的麻烦是从其他班级访问权限,并且将用户输入的学校ID与学区中的现有学校ID进行匹配。当所有学校都已经在initStudents()函数下创建(显示为schoolOne和schoolTwo)时,我很难克服过去的限制并将学生对象添加到现有学校,而不必在launch()函数下创建新对象。任何帮助,将不胜感激。
我在Control.cc下的尝试是行
Student* student = new Student(name, gender);
if(fid == schoolDistrict.getNextId()){
schoolDistrict.add(fid,student);
}
但是我知道这行不通,因为当我尝试编译时一切都超出了范围,我认为我的逻辑也是错误的。
Control.cc:
#include <iostream>
using namespace std;
#include <string>
#include "Control.h"
#include "Student.h"
#include "Person.h"
#include "School.h"
void Control::launch()
{
initAnimals();
int choice, fid;
string name, gender;
while (1) {
view.showMenu(choice);
if (choice == 0)
break;
if (choice == 1) {
view.printStr("School ID: ");
view.readInt(fid);
view.printStr("Name: ");
view.readStr(name);
view.printStr("Gender: ");
view.readInt(gender);
Student* student = new Student(name, gender);
if(fid == schoolDistrict.getNextId()){
schoolDistrict.add(fid,student);
}
}
}
}
void Control::initStudents(){
School* schoolOne;
School* schoolTwo;
schoolOne = new School();
schoolTwo = new School();
schoolDistrict.add(schoolOne);
schoolDistrict.add(schoolTwo);
Student* studentOne;
studentOne = new Student("Johnny Doe", "Male");
schoolTwo->add(student);
}
Control.h:
#ifndef CONTROL_H
#define CONTROL_H
#include "View.h"
#include "SchoolDistrict.h"
class Control
{
public:
void launch();
private:
View view;
SchoolDistrict schoolDistrict;
void initStudents();
};
School.cc:
#include <iostream>
using namespace std;
#include <string>
#include "School.h"
int School::nextId = 1000;
School::School(){
id = nextId;
++nextId;
}
int School::getNextId() {
return nextId;
}
void School::add(Student* s){
studentsList.add(s);
}
SchoolDistrict.cc:
#include <iostream>
#include <iomanip>
using namespace std;
#include <string>
#include "SchoolDistrict.h"
SchoolDistrict::SchoolDistrict(){
currentNumSchools = 0;
}
SchoolDistrict::~SchoolDistrict()
{
//code
}
void SchoolDistrict::add(School* s){ // adds the given school s to the back (the end) of the school array
if(currentNumSchools != MAX_SCHOOLS){
for(int i = 0; i < currentNumSchools; ++i){
schools[i] = s;
++currentNumSchools;
}
}else{
cout << "Maximum number of schools reached. " << endl;
}
}
void SchoolDistrict::add(int id, Student* s){
//adds the given student s to the school with the unique identifier matching the parameter id
for(int i = 0; i < currentNumSchools; ++i){
if(schools[i]->getNextId() == id){
schools[i]->add(s);
}
}
}
删除您的School::getNextId()
(因为它令人困惑且无用),而改写School::getId()
。类似于:
int School::getId() const {
return id;
}
使用它,您的SchoolDistrict :: add开始正常工作,如其注释所示:
void SchoolDistrict::add(int id, Student* s){
//adds the given student s to the school with the unique identifier matching the parameter id
for(int i = 0; i < currentNumSchools; ++i){
if(schools[i]->getId() == id){
schools[i]->add(s);
}
}
}