Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. C++ - Write a phone book program that uses the following classes: Individual

ID: 3763873 • Letter: 1

Question

1. C++ - Write a phone book program that uses the following classes:

   Individual - base class.

      Private data members: last name, first name, phone number.

   Student - derived from Individual.

      Private data members: gpa, hours(current enrollment).

   Staff - derived from Individual.

      Private data members: position(string), year employed(integer).

2. The program should be menu driven, and should allow the user to do the following:

   a. Create a new phone book entry and place it in an array

   b. Retrieve a selected phone book entry and display it on the screen. Display all entries with "requested" last name

   c. Retrieve and display all phone book entries on the screen and write them to a file.

3. Your program will contain two arrays: an array of Student objects and an array of Staff objects. Arrays of 10 positions are sufficient for this program. You are not required to maintain the entries in any particular sequence.

4. When phone book entries are displayed all data members will be displayed. Create friend functions to overload the << operator to output phone book entries.

Explanation / Answer

#include<iostream>

#include<string.h>

#include<fstream>

using namespace std;

class Individual{

private:

string lastName;

string firstName;

string phoneNumber;

  

public:

Individual(string l, string f, string p) {

lastName=l;

firstName=f;

phoneNumber=p;

}

  

string getFirstName() {

return firstName;

}

string getLastName() {

return lastName;

}

string getPhoneNumber() {

return phoneNumber;

}

};

class Student : public Individual{

private:

double gpa;

double hours;

  

public:

Student(string l, string f, string p, double g, double h) : Individual(l,f,p) {

gpa=g;

hours=h;

}

  

friend ostream& operator<<(ostream &o, Student &s) {

o<<"Name: "<<s.getFirstName()<<" "<<s.getLastName()<<" ";

o<<"Phone Number: "<<s.getPhoneNumber()<<" ";

o<<"GPA: "<<s.gpa<<" ";

o<<"Hours: "<<s.hours<<" ";

return o;

}

};

class Staff : public Individual {

private:

string position;

int yearEmployed;

  

public:

Staff(string l, string f, string p, string pos, int y) : Individual(l,f,p) {

position=pos;

yearEmployed=y;

}

  

friend ostream& operator<<(ostream &o, Staff &s) {

o<<"Name: "<<s.getFirstName()<<" "<<s.getLastName()<<" ";

o<<"Phone Number: "<<s.getPhoneNumber()<<" ";

o<<"Position: "<<s.position<<" ";

o<<"Year Employed: "<<s.yearEmployed<<" ";

return o;

}

};

int main() {

Student *students[10];

Staff *staffs[10];

int noStudents=0;

int noStaffs=0;

  

while (true) {

cout<<"1. to make an entry ";

cout<<"2. to retreive data based on lastname ";

cout<<"3. to display and print into file ";

cout<<"4. to exit: ";

cout<<"Enter: ";

int ch;

cin>>ch;

  

if(ch==1) {

cout<<"Enter 1 for student or 2 for staff: ";

int sel;

cin>>sel;

string fn, ln, ph;

cout<<"Enter first name: ";

cin>>fn;

cout<<"Enter last name: ";

cin>>ln;

cout<<"Enter phone numnber: ";

cin>>ph;

  

if (sel == 1) {

double gpa, hours;

cout<<"Enter gpa: ";

cin>>gpa;

cout<<"Enter hours: ";

cin>>hours;

students[noStudents++]=new Student(ln,fn,ph,gpa,hours);

} else {

string position;

int yearEmployed;

cout<<"Enter position: ";

cin>>position;

cout<<"Enter year employed: ";

cin>>yearEmployed;

staffs[noStaffs++]=new Staff(ln,fn,ph,position,yearEmployed);

}

} else if (ch==2) {

cout<<"Enter last name: ";

string ln;

cin>>ln;

  

for (int i=0; i<noStudents; i++) {

if (students[i]->getLastName() == ln) {

cout<<students[i];

break;

}

}

for (int i=0; i<noStaffs; i++) {

if (staffs[i]->getLastName() == ln) {

cout<<staffs[i];

break;

}

}

cout<<"Not found!! ";

} else if(ch==3) {

cout<<"Enter file name: ";

string fn;

cin>>fn;

ofstream file;

file.open(fn);

  

file<<"Students ";

for (int i=0; i<noStudents; i++) {

file<<"Student: "<<i+1<<" ";

file<<students[i];

}

file<<"Staffs ";

for (int i=0; i<noStaffs; i++) {

file<<"Staffs: "<<i+1<<" ";

file<<staffs[i];

}

} else {

cout<<"Good bye !! ";

return 0;

}

}

  

cout<<" ";

return 0;

}