// Demo: common error // : in updating name in Student #include #include #include using namespace std; class Student { private: string id,name,class_no; public: Student(){ id="id00"; name="none"; class_no="00"; } // initial values given for very student void show() { cout << left << setw(5) << id << setw(15) << name << class_no << endl; } void enter(); // enter name only }; void Student::enter() { // updates name to local variable string name; // this SHOULD NOT be declared cout << "enter name "; getline(cin,name); } void main() { Student x,y; // create 2 students x and y x.show(); // display student x"id00 none 00" x.enter(); // enter the name "BENG" for student x x.show(); // display "id00 none 00" (note BENG is not updated) } /* Note The name BENG entered does not update the name of student x because in the function enter(), a local variable "name" was declared and the name was entered into the local variable of enter(), but NOT the 'name' attribute in Student. */