Home > Mobile >  A class that change the value in another class in c
A class that change the value in another class in c

Time:12-11

I have a class named subjects consist only with variables without methods and another class for students like this: class Student:public Persone, public Subjects{...} Student class have this two methodes:

void setAr(int ar){
    this->arabic = ar;
}
int getAr(){
    return arabic;
}

I created another class for teacher and I want it to be able to change the note for the student my idea is using something like this:

void setStudentNote(Student student, int note){
    bool access = false;
    for(int i=0;i<Groups.size();i  ){
        if(student.showGroup()==Groups[i])
            access = true;
    }
    if(access){
        if(Subject==Ar){
            student.setAr(note);
            cout << student.getFname() << " "<<student.getLname() <<" " <<Subject << " note: " << note << endl;
        }
    }else{
        cout << "error, this student is not in your group!";
    }
}

the student.setAr(note) is not changing the var for the student.

CodePudding user response:

You're taking the student by value, which means you're making a copy of the student whenever you call that function, and then changing the copy. To change the original, take a (mutable) reference.

void setStudentNote(Student& student, int note)

Note the ampersand after the word Student. Student& is a non-const lvalue reference to a Student, which means modifications to the variable will affect the caller's variable.

  • Related