Question
Need help with completed code! 4 header files: Container.h (unchanged) student.h (completed code) undergrad.h (completed code) grad.h (completed code) 5 C++ files: Container.cpp (unchanged) student.cpp
Need help with completed code!
4 header files: Container.h (unchanged) student.h (completed code) undergrad.h (completed code) grad.h (completed code)
5 C++ files: Container.cpp (unchanged) student.cpp (unchanged) grad.cpp (completed code) undergrad.cpp (completed code) hw10.cpp (completed code)
*------Container.cpp--------*
#include "Container.h"
// Constructor for Container class
Container::Container()
{
student = NULL;
next = NULL;
}
*------Container.h---------*
#ifndef _CONTAINER_H_
#define _CONTAINER_H_
#include "student.h"
class Container {
public:
Student *student;
Container *next;
Container(); // constructor
};
#endif // _CONTAINER_H_
*--------grad.cpp---------*
// 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 files
*------undergrad.cpp------*
// 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 files
*-----student.cpp---------*
#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;
}
*-----------grad.h--------*
#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_
*--------undergrad.h---------*
#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
*--------student.h--------*
#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-------*
//CSE240 Fall 2018 HW10
// Write your name here
// Programming environment used: VS or g++
// READ BEFORE YOU START:
// You are given a partially completed program that creates a list of students.
// Each student has the corresponding information: name, roll number and student type.
// This information is stored as an object of Student class.
// The classes Undergrad and Grad are child classes of the Stduent class.
// When adding a new student, these child classes are used to make the student node of the list.
//
// To begin, you should trace through the given code and understand how it works.
// Please read the instructions above each required function and follow the directions carefully.
// Do not modify given code.
//
// You can assume that all input is valid:
// Valid name: String containing alphabetical letters
// Valid roll number: a positive integer
#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
cout
cout
cout
cout
cout
cin >> 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
getline(cin, name_input);
cout
cin >> rollNo_input;
cin.ignore();
while (!(type_input == 0 || type_input == 1))
{
cout
cout
cout
cin >> 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
}
else
cout
break;
case 'd': // display the list
displayList();
break;
case 'c': // change roll number
cout
getline(cin, name_input);
// searchStudent() will return the student node if found, else returns NULL
studentFound = searchStudent(name_input);
if (studentFound == NULL)
{
cout
}
else
{
// if stduent exists in the list, then ask user for new roll number
cout
cin >> 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
}
break;
case 'q': // quit
break;
default: cout
}
}
// No implementation needed here, however it may be helpful to review this function
Student* searchStudent(string name_input)
{
Container* tempList = list; // work on a copy of 'list'
while (tempList != NULL) // parse till end of list
{
if (tempList->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)
{
}
Q4 - Q7 are used to implement a menu-driven program. Its implementation is in hw10.cpp. The menu gives the following options to user: a) Add a new student to the list. You need not check if the student exists in the list because that is already implemented in executeAction(). You may add the new student to head or tail of the list. (sample solution adds it to the tail) b) Display the list of students along with their details (roll number, type (grad/undergrad)). c) Change the roll number of a student. This options uses the friend function changeRollNo() to access private data member rollNo of the student and change it to the one entered by the user. You need to implement save() that writes the list of students to a file list.txt while exiting the program and load() that reads the file to form the list at the beginning of the program. During first execution of the program, there will be no list.txt, so load() would not form a list. Once list.txt is saved, load() can read it. You may remove the list.txt from your project directory if you do not want to load that list (maybe while testing your code). The following is one way to store the list in list.txt file. The format here is: student name student roll number student type (0 for undergrad, 1 for grad) You may store it in another format if you wish. Notice that the 3 at the beginning is the number of students stored in the file.
Expected output: addStudent: CSE240 Hw1e 4 Please enter your selection: a: add a new student to the list d: display list of students c: change roll number of a student q: quit 5 1 Enter student's name: Tony Stark Enter roll number: 1 0 2 4 Enter student type: 9. Undergrad 1. Grad Student added to list! Expected output: addStudent: CSE240 Hw1e 4 Please enter your selection: a: add a new student to the list d: display list of students c: change roll number of a student q: quit 5 1 Enter student's name: Tony Stark Enter roll number: 1 0 2 4 Enter student type: 9. Undergrad 1. Grad Student added to listStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started