Question
Container.cpp ------------------------------- #include Container.h // Constructor for Container class Container::Container() { student = NULL; next = NULL; } ------------------------------- Container.h ------------------------------- #ifndef _CONTAINER_H_ #define _CONTAINER_H_
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_
-------------------------------
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
-------------------------------
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.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
-------------------------------
grad.cpp
-------------------------------
// 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.h
-------------------------------
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_
-------------------------------
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;
}
-------------------------------
hw10.cpp
-------------------------------
// 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)
{
}
-------------------------------
OUTPUT:
-------------------------------
Expected output: SE240 HW10 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 Enter student's name: Tony Stark Enter roll number: 1 Enter student type: . Undergrad 1. Grad tudent added to list! displayList: SE240 HW10 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 Undergrad student: Tony Stark Roll number: 1 Grad student: Steve Rogers Roll number: 2 Grad student: Bruce Banner Roll number: 3 Expected output: SE240 HW10 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 Enter student's name: Tony Stark Enter roll number: 1 Enter student type: . Undergrad 1. Grad tudent added to list! displayList: SE240 HW10 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 Undergrad student: Tony Stark Roll number: 1 Grad student: Steve Rogers Roll number: 2 Grad student: Bruce Banner Roll number: 3Step 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