如何在公共类C ++中访问变量的值

时间:2019-02-25 05:01:35

标签: c++

我有3个不同的班级:学生,课程和注册。每个班级都有相同的功能来添加新的学生,课程或注册。我主要创建了三个动态对象数组,其大小取决于用户(每次在数组中添加新元素时,我的对象数组都会增加1)。我已经为数组中的每个新元素成功添加了值,并正常打印。但是,如果我需要从另一个类中的一个类访问变量的值,那么我将得到0。 例如,

我的学生班级将如下所示:

class Student {
    studentID;
    studentName;
    Student *AddStudent(getting value for object in class Student);
}

我的课程:

class Course {
    courseID;
    courseName;
    Course *addCourse(getting value for object in class Course)
}

我的注册课程:

class Enrollment {
    Student getStudentID[totalStudent];//Here I declare an empty object of class Student, that means by default, my studentID value in arrays is 0.
    Course getCourseID[totalCourse];//Same for here
    void AddEnrollment(Enrollment *newEnrollment, int totalCourse, int numStudent, int &countEnrollment, int &totalEnrollment) {
        // I can user Student *newStudent, and Course *newCourse
        // as a paramater because it is not declare in my Enrollment.h
        // Checking student ID and course ID to see if these ID exists
        // in either class or not, if not, then the user have to enter
        // a valid ID before enrolling.Enrollment tempEnrollment;
        Student tempStudent[numStudent]; //I declare an empty array so by default constructor, studentID is 0
        Course tempCourse[totalCourse];//Same for this one
        int studentID, courseID;
        bool checkStuID = false;
        bool checkCourID = false;

        cout << "Enter a student ID: ";
        cin >> tempEnrollment.student_ID;
        while (checkStuID == false)
        {
            for (int i = 0; i < numStudent; i++)
            {
                cout << tempStudent[i].getID() << " ";
                if (tempEnrollment.student_ID == tempStudent[i].getID())
                {
                checkStuID = true;
                }
            }
            if (checkStuID == false)
            {
                cout << "This student ID is not in the system. Please enter a valid student ID. \n";
                cin >> tempEnrollment.student_ID;
            }
        }
    }
};

我的注册标题:

class Enrollment{
public:
    int course_ID;
    int student_ID;
    int enrollment_ID;
    int student_Grade[10];
    int countGrade;
    int averageGrade;
    char letterGrade;
    Enrollment();


//If I try to pass array as reference such as Student *s, Course *c or Student s[], Course c[], I would get an error as class Student and Course is not declared in this Enrollment class so access them is impossible
        void AddEnrollment(Enrollment* &newEnrollment, int totalCourse, int numStudent, int &countEnrollment, int &totalEnrollment);
        void AddGrade(Enrollment *newGrade, int totalEnrollment);
        Enrollment* IncreasingSize(Enrollment *newEnrollment, int oldSize, int newSize);
};

我的主班:

int main() {
    Student *newStudent = new Student[countStudent];
    Course *newCourse = new Course[countCourse];
    Enrollment *newEnrollment = new Enrollment[countEnrollment];

    newStudent->AddStudent();
    newCourse->AddCourse();
    newEnrollment->AddEnrollment(...);
}

我的问题是我无法获取StudentID和课程ID,以检查其是否与从用户输入的ID匹配。因为我在Enrollment类中创建的对象数组中没有数据,所以它总是得到“ 0”或随机数。那么我有什么办法可以获取这些价值?我曾尝试在Enrollment.h标头文件中使用include“ Student.h”或“ Course.h”,但这会给我一个错误,例如重新定义类Student或Course。

例如:

My student ID is: 1 - 2 - 3 - 4 - 5

My Course ID is: 6 - 7 - 8 - 9 - 10

ID the user input for studentID and courseID is: 1 - 6
The ID I will get in Enrollment class is either '0' or '251235' (random number)  

我想解决这个问题的方法是使用Student和Course类的指针作为参数来访问数组的变量值,例如addEnrollment(Enrollment *newEnrollment, Student *newStudent, Course *newCourse),但这会给我一个错误,因为我的函数标头中的函数确实不包含学生或课程声明。

4 个答案:

答案 0 :(得分:0)

通常在这种情况下使用合成 https://www.learncpp.com/cpp-tutorial/102-composition/

addEnrollment(Enrollment *newEnrollment, Student *newStudent, Course *newCourse)

这是进行以下修改的正确方法。

addEnrollment(Student *newStudent, Course *newCourse){
// here you try to create the new enrollment object and push it in your data structure.
}

答案 1 :(得分:0)

在阅读您的问题后,我的理解是您无法从课堂上获得价值?

原因是axios({ method: "GET", url: "api/user", headers: { Accept: "application/json" } }); 在默认情况下是私有的,因此如果需要检索或更改值,则需要创建一个getter和setter。

答案 2 :(得分:0)

  1. 希望在您的实际代码中将成员属性声明为Public

  2. 学生* newStudent =新学生[countStudent]; 课程* newCourse =新课程[countCourse];

newStudent和newCourse指向一系列“学生”类,而不是一个特定的学生。 您要这样工作吗?在AddEnrollment方法内部,您是否指向正确的对象实例? 请粘贴完整的代码,以进行更好的调查。

//declaration
void addEnrollment(Student s[], Course c[]);
Student *newStudent = new Student[countStudent];
Course *newCourse = new Course[countCourse];

addEnrollment(newStudent, newCourse);

addEnrollment(Student s[], Course c[]) {
    for(i=0; i<countStudent;i++)
    cout << s[i].studentID;
}

注意:您也可以通过引用

答案 3 :(得分:0)

看看并运行此最小示例,让我们知道这是否与您要实现的目标类似。

#include <iostream>
class Course {
public:
    int ID;
    Course(int id) : ID(id) {};
};

class Student {
public:
    Student(int id) : ID(id) { noOfCourses = 0; }
    void addCourse(Course c) {
        courses[noOfCourses] = c.ID;
        noOfCourses++;
    }
    int ID;
    int *courses;
    int noOfCourses;
};

class Enrolment {
public:
    static bool checkEnrolment(Student my) {
        bool checkStuID = false;
        int courseID = 0;
        while (!checkStuID) {
            std::cout << "check a course: " << std::endl;
            std::cin >> courseID;
            // iterate through students courses comparing with input course.
            for (int i = 0; i < my.noOfCourses; i++)
            {
                if (my.courses[i] == courseID)
                {
                    checkStuID = true;
                    std::cout << my.ID << " is enrolled in " 
                              << courseID << "." << std::endl;
                    return 1;
                }
            }
            std::cout << my.ID << " is not enrolled in " 
                      << courseID << "." << std::endl;
            courseID = 0;
        }
        return 0;
    }
};

int main()
{
    Student johnny(1234);
    Course math(123);
    Course science(456);
    johnny.addCourse(math);
    johnny.addCourse(science);
    Enrolment::checkEnrolment(johnny);
}