undergrad.h file #ifndef _UNDERGRAD_H #define _UNDERGRAD_H // Q1a: Create Undergrad class (5 points) // Part 1: Create a child class of the Student class named 'Undergrad'
// Part2: Declare constructor which accepts the same 3 parameters as the parent class Student's constructor. // Pass the 3 parameters to the super constructor of the Student class.
// Part 3: Re-declare the method displayInfo (virtual method found inside of parent class Student)
#endif // _UNDERGRAD_H
grad.h file
#ifndef _GRAD_H_ #define _GRAD_H_ // Q1b: Create Grad class (5 points) // Part 1: Create a child class of the Student class named 'Grad'
// Part2: Declare constructor which accepts the same 3 parameters as the parent class Student's constructor. // Pass the 3 parameters to the super constructor of the Student class.
// Part 3: Re-declare the method displayInfo (virtual method found inside of parent class Student)
#endif // _GRAD_H_
student.h file
#ifndef _STUDENT_H_ #define _STUDENT_H_ #include using namespace std; enum studentType { undergrad = 0, grad }; // definition of studentType class Student { private: string name; // private local variables int rollNo; studentType level; public: Student(string studentName, int studentRollNo, studentType studentLevel); // constructor // accessor methods string getName(); int getRollNo(); studentType getstudentType(); // Q3a: Declare Friend Function changeRollNo() (1 point) // Declare a friend function named changeRollNo() which has 2 parameters and no return value. // The first parameter is a pointer to Student class, and the second is an integer which is the new roll number. // You need to define this function in hw10.cpp and call this function in case 'c' of executeAction() in hw10.cpp file virtual void displayInfo() { } }; #endif // _STUDENT_H_
hw10.cpp file #include #include #include #include "Container.h" #include "student.h" #include "grad.h" #include "undergrad.h" using namespace std; // forward declarations of functions already implemented: void executeAction(char c); Student* searchStudent(string name_input); // forward declarations of functions that need implementation: void addStudent(string name_input, int rollNo_input, studentType type); // 7 points void displayList(); // 4 points void save(string fileName); // 7 points void load(string fileName); // 7 points Container* list = NULL; // global list int main() { char c = 'i'; // initialized to a dummy value load("list.txt"); // During first execution, there will be no list.txt in source directory. list.txt is generated by save() while exiting the program. do { cout > c; cin.ignore(); executeAction(c); } while (c != 'q'); save("list.txt"); list = NULL; return 0; } // Ask for details from user for the given selection and perform that action // Read the function case by case void executeAction(char c) { string name_input; int rollNo_input; int type_input = 2; studentType type; Student* studentFound = NULL; switch (c) { case 'a': // add student // input student details from user cout > rollNo_input; cin.ignore(); while (!(type_input == 0 || type_input == 1)) { cout > type_input; cin.ignore(); } type = (studentType)type_input; // searchStudent() will return the student node if found, else returns NULL studentFound = searchStudent(name_input); if (studentFound == NULL) { addStudent(name_input, rollNo_input, type); cout > rollNo_input; cin.ignore(); // Q3c Call changeRollNo() here (1 point) // 'studentFound' contains the student whose roll number is to be changed. // 'rollNo_input' contains the new roll number of the student. // Call the function with appropriate function arguments. cout student->getName() == name_input) { return tempList->student; // return the student if found } tempList = tempList->next; // parse the list } return NULL; // return NULL if student not found in list } // Q3b: Define Friend Function changeRollNo() (3 points) // Define the function changeRollNo()that is declared in student.h file. // This function sets the new roll number of the student. The student and new roll number is to be passed as function arguments. // Call this function in case 'c' of executeAction(). While testing, use 'd' display option after using 'c' option to verify whether the new roll number is set. // You will need to implement add() and displayList() before you test this function. // Q4: addStudent (7 points) // This function is used to add a new student to the global linked list 'list'. You may add the new student to head or tail of the list. (Sample solution adds to tail) // New student can be either undergrad or grad. You will need to use the function argument type to determine which constructor to use to create new student node. // For example, if the user enters undergrad student, then you need to use Undergrad class and constructor to create new student node and add it to the list. // NOTE: In executeAction(), searchStudent() is called before this function. Therefore no need to check here if the student exists in the list. // See how this fucntion is called in case 'a' of executeAction() void addStudent(string name_input, int rollNo_input, studentType type) { Container* tempList = list; // work on a copy of 'list' } // Q5: displayList (4 points) // This function displays the list of student and their details (roll number, student type) // Parse the list and use the class member function to display the student info. // See expected output in the question file. // No implementation needed here, however it may be helpful to review this function void displayList() { Container *tempList = list; // work on a copy of 'list' } // Q6: save (7 points) // Save the linked list of students to a file list.txt using ofstream. // You will need to save the number of students in linked list. That will help in load() when reading the file. // One format to store is: // // // // // // // // ... // You may store the list in another format if you wish. You need to read the file in same way in load(). // Hint: You may want to cast the enum studentType to an int before writing it to the file. See example in question file. // This function is called when exiting the program (end of main() ). void save(string fileName) { } // Q7: load (7 points) // Load the linked list of students from the file using ifstream. // You will need to create the linked list in the same order that is was saved to the file in save(). // First, read the number of students saved in the file. // Then, for every student you will need to create a new Student node depending on student type. You may add the student to head or tail of the list. // Hint: If you casted the enum studentType to an int, you will need to cast it back to studentType when making the student node. // This function is called at the beginning of main(). void load(string fileName) { }
undergrad.cpp file // Q2a: Define displayInfo() for Undergrad class (5 points) // Define the fucntion displayInfo() that you declared within the Undergrad class in the header file // See expected output in question file.
// (displayList() function in hw10.cpp calls this function.) // Include necessary header file
grad.cpp file // Q2b: Define displayInfo() for Grad class (5 points) // Define the function displayInfo() that you declared within the Grad class in the header file // See expected output in question file.
// (displayList() function in hw10.cpp calls this function.) // Include necessary header files
student.cpp file #include "student.h" Student::Student(string studentName, int studentRollNo, studentType type) { name = studentName; rollNo = studentRollNo; level = type; } string Student::getName() { return name; } int Student::getRollNo() { return rollNo; } studentType Student::getstudentType() { return level; }
Container.cpp file #include "Container.h" // Constructor for Container class Container::Container() { student = NULL; next = NULL; }
4 header files: Container.h student.h undergrad.h grad.h 5 C++ files: Container.cpp student.cpp grad.cpp undergrad.cpp hw10.cpp The diagram shows the inheritance and relationships between the classes and the linked list formed. list Container student student student next next next null Student name name name inheritence rollNo rollNo rollNo level level level Undergrad Grad Student() getnamel) getRollNol0 Student) getname) getRollNo() Student) getname() Undergrad() displayinfo Grad) displayinfo getStudentType() displaylnfo) displayinfo) displayinfo 4 header files: Container.h student.h undergrad.h grad.h 5 C++ files: Container.cpp student.cpp grad.cpp undergrad.cpp hw10.cpp The diagram shows the inheritance and relationships between the classes and the linked list formed. list Container student student student next next next null Student name name name inheritence rollNo rollNo rollNo level level level Undergrad Grad Student() getnamel) getRollNol0 Student) getname) getRollNo() Student) getname() Undergrad() displayinfo Grad) displayinfo getStudentType() displaylnfo) displayinfo) displayinfo