C++ Create a simple \"database\" by using an array; The core of the database nee
ID: 3741030 • Letter: C
Question
C++ Create a simple "database" by using an array; The core of the database needs to be an array that will hold up to 1000 objects. The code should have a single Class Hierarchy, and at least one abstract class. The database/array needs to be able to hold students, staff, and teachers. Each with the following information:
//Base Class Person: first name, last name
//Class Student: first name, last name, GPA
//Class Staff: first name, last name, employee number
//Class Teacher: first name, last name, employee number, number of classes taught
Single class hiearchy of
Person -> student
Person->staff->teacher
The database menu should be presented to the user with the following options:
Add a Person
List the People
Sort the People
Exit
The Add a Person choice should give the options to:
Add a Student
Add a Staff Member
Add a Teacher
The Sort the People option should not display, but should sort the records by first name and last name.
When records is displayed it should look like the following:
:
: data
: data
: data
...
------------------------------------------------------------------
:
: data
: data
: data
...
------------------------------------------------------------------
Explanation / Answer
#include <iostream>
using namespace std;
int numPersons=0;
class Person {
public:
virtual void addPerson(string fnm, string lnm, float gpa, int en, int nc) = 0;
public:
string firstName;
string lastName;
float gpa;
int empNo;
int numClasses;
int rep; // value of rep for Student=1, Staff=2, Teacher=3
};
class Student : public Person {
public:
void addPerson(string fnm, string lnm, float gp){
this->firstName = fnm;
this->lastName = lnm;
this->gpa = gp;
this->rep = 1;
}
};
class Staff: public Person {
public:
void addPerson(string fnm, string lnm, int en){
this->firstName = fnm;
this->lastName = lnm;
this->empNo = en;
this->rep=2;
}
};
class Teacher: public Staff {
public:
void addPerson(string fnm, string lnm, int en, int nc){
this->firstName = fnm;
this->lastName = lnm;
this->empNo = en;
this->numClasses = nc;
this->rep=3;
}
};
Person *parr[1000];
void addStudent(string f, string l, float gpa){
Student *s ;
s->addPerson(f,l,gpa);
parr[numPersons] = s;
numPersons++;
}
void addStaff(string f, string l, int empno){
Staff *st;
st->addPerson(f,l,empno);
parr[numPersons] = st;
numPersons++;
}
void addTeacher(string f, string l, int empno, int nclasses){
Teacher *t;
t->addPerson(f,l,empno,nclasses);
parr[numPersons] = t;
numPersons++;
}
void listPeople(){
for(int i=0;i<numPersons;i++){
switch(parr[i]->rep){
case 1:
cout<<"----------------------------------"<<endl;
cout<<"Student"<<endl;
cout<<"First Name: "<<parr[i]->firstName<<endl;
cout<<"Last Name: "<<parr[i]->lastName<<endl;
cout<<"GPA: "<<parr[i]->gpa<<endl;
cout<<"----------------------------------"<<endl;
break;
case 2:
cout<<"----------------------------------"<<endl;
cout<<"Staff"<<endl;
cout<<"First Name: "<<parr[i]->firstName<<endl;
cout<<"Last Name: "<<parr[i]->lastName<<endl;
cout<<"Employee Number: "<<parr[i]->empNo<<endl;
cout<<"----------------------------------"<<endl;
break;
case 3:
cout<<"----------------------------------"<<endl;
cout<<"Student"<<endl;
cout<<"First Name: "<<parr[i]->firstName<<endl;
cout<<"Last Name: "<<parr[i]->lastName<<endl;
cout<<"Employee Number: "<<parr[i]->empNo<<endl;
cout<<"Number of classes: "<<parr[i]->numClasses<<endl;
cout<<"----------------------------------"<<endl;
break;
}
}
}
void sort(){
for(int i=0;i<numPersons;i++){
for(int j=0;j<numPersons-i;j++){
if((parr[j+1]->firstName.compare(parr[j]->firstName)) < 0) {
// swap
Person *p;
p = parr[j];
parr[j] = parr[j+1];
parr[j+1] = p;
}
}
}
}
int main()
{
int ch;
while(true){
cout<<"DATABASE MENU"<<endl;
cout<<"1.Add a Person"<<endl;
cout<<"2.List the peiple"<<endl;
cout<<"3.Sort the perople"<<endl;
cout<<"4.Exit"<<endl;
cin>>ch;
switch(ch){
case 1:
int choice;
cout<<"1.Add a Student"<<endl;
cout<<"2.Add a Staff Member"<<endl;
cout<<"3.Add a Teacher"<<endl;
cin>>choice;
switch(choice){
case 1:
string fname,lname;
float g;
cout<<"Enter first name, last name & GPA"<<endl;
cin>>fname>>lname>>g;
addStudent(fname,lname,g);
break;
case 2:
string fnam,lnam;
int eno;
cout<<"Enter first name, last name & employee number"<<endl;
cin>>fname>>lname>>eno;
addStaff(fnam,lnam,eno);
break;
case 3:
string fnme,lnme;
int enom, ncl;
cout<<"Enter first name, last name, employee number & numeber of classes"<<endl;
cin>>fnme>>lnme>>enom>>ncl;
addTeacher(fname,lname,eno, ncl);
break;
default: cout<<"Please enter a valid choice"<<endl;
}
break;
case 2: listPeople();
break;
case 3: sort();
break;
case 4: cout<<"Program is exiting now......."<<endl;
return 0;
default: cout<<"Please enter a valid choice"<<endl;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.