Description Instructions Multiple Attempts Not allowed. This test can only be ta
ID: 3783210 • Letter: D
Question
Description Instructions Multiple Attempts Not allowed. This test can only be taken once. Force Completion This test can be saved and resumed later QUESTION 1 10 Implement the following class in interface and implementation files. A separate file (the main project file) shall have the main function that exercises this class. Create a class named Student that has three member variables: name - string that stores the name of the student numClasses - integer that tracks how many courses the student is currently enrolled in, this number must be between 1 and 100. classList - an array of strings of size 100 used to store the names of the classes that the student is enrolled in Write the appropriate constructor(s), mutator and accessor functions for the class along with the following: - a function that inputs all values from the user, including the list of class names - a function that outputs the name and list of all courses - a function that resets the number of classes to 0 and the classList to an empty listExplanation / Answer
This project has following four files:
1. main.cc (This is the main file containing function main())
2. student.hh (This is header file containing class - Student)
3. student.cc (This is c++ file containing the implementation of Student class)
4. global.hh (This is global header file)
Compile the project using following command (on linux):
$ g++ main.cc student.cc
----------------------------------------------------------------------------------------------------------------------------------------
1. main.cc
#include "global.hh"
#include "student.hh"
using namespace std;
int main(){
int numStudent;
int numClass;
string name;
string *listClass;
cout << "Enter number of Students. ";
cin >> numStudent;
Student *s=new Student[numStudent];
cout << "Enter details of the students one by one." << endl;
for(int i=0; i<numStudent; i++){
cout << "-----------------------------------------" << endl;
cout << "Enter name of Students "<<i+1<<" : ";
cin >> name;
cout << "Enter number of classes the student enrolled in: ";
cin >> numClass;
listClass = new string[numClass];
for(int j=0; j<numClass; j++){
cout << "Name of class " << j+1 << " : ";
cin >> listClass[j];
}
s[i].setStudentData(name, numClass, listClass);
}
cout << "-----------------------------------------" << endl;
cout << "Data Entered by you: " << endl;
// Print Details
for(int i=0; i<numStudent; i++){
cout << "" << endl;
cout << "-----------------------------------------" << endl;
s[i].printNameList();
}
cout << "" << endl;
cout << "-----------------------------------------" << endl;
// Empty the student data
for(int i=0; i<numStudent; i++){
s[i].emptyData();
}
return 0;
}
----------------------------------------------------------------------------------------------------------------------------------------
2. student.hh
#ifndef STUDENT_HH
#define STUDENT_HH
#include "global.hh"
class Student{
private:
string name;
int numClasses;
string classList[NUMCLASS];
public:
Student();
void setStudentData(string name, int numClasses, string *list);
void printNameList();
void emptyData();
};
#endif
----------------------------------------------------------------------------------------------------------------------------------------
3. student.cc
#include "global.hh"
#include "student.hh"
#include <string>
// Constructor
Student::Student(){
this->name="";
this->numClasses=0;
for(int i=0; i<NUMCLASS; i++)
classList[i]="";
}
void Student::setStudentData(string name, int numClasses, string *list){
this->name = name;
this->numClasses=numClasses;
for(int i=0; i<numClasses;i++)
classList[i].assign(list[i]);
}
void Student::printNameList(){
cout << "List of courses " << this->name << " enrolled in: ";
for(int i=0; i<this->numClasses;i++){
cout << this->classList[i] << " ";
}
}
void Student::emptyData(){
this->name="";
this->numClasses=0;
for(int i=0; i<NUMCLASS; i++)
this->classList[i]="";
}
----------------------------------------------------------------------------------------------------------------------------------------
4. global.hh
#ifndef GLOBAL_HH
#define GLOBAL_HH
#include <iostream>
#include <string>
using namespace std;
#define NUMCLASS 100
#endif
Program Output:
Enter number of Students. 2
Enter details of the students one by one.
-----------------------------------------
Enter name of Students 1 : student01
Enter number of classes the student enrolled in: 2
Name of class 1 : class01
Name of class 2 : class02
-----------------------------------------
Enter name of Students 2 : student02
Enter number of classes the student enrolled in: 3
Name of class 1 : class02
Name of class 2 : class03
Name of class 3 : class04
-----------------------------------------
Data Entered by you:
-----------------------------------------
List of courses student01 enrolled in: class01 class02
-----------------------------------------
List of courses student02 enrolled in: class02 class03 class04
-----------------------------------------