Question
Modify the Employer class to serve as a base class. Create child classes from the Employer class for three types of employers: Sales, Service, and
Modify the Employer class to serve as a base class.
Create child classes from the Employer class for three types of employers:
Sales,
Service, and
Manufacturing.
Modify the program to use each child class based on a choice of employer type during data input.
Ensure that the program will output properly when all three types of child classes are active after input.
//Employer.h
#ifndef EMPLOYER_INCLUDE
#define EMPLOYER_INCLUDE
class Employer
{
public:
long ph;
char name[20];
void input_data();
void show_data();
long get_phone();
char* get_name();
};
#endif
//Employer.cpp
#include
#include "Employer.h"
using namespace std;
void Employer::input_data()
{
cout << "Phone: ";
cin >> ph;
cout << "Name: ";
cin.ignore();
cin >> name;
}
void Employer::show_data()
{
cout << endl
<< "Phone number: " << ph;
cout << endl
<< "Name: " << name;
}
long Employer::get_phone()
{
return ph;
}
char* Employer::get_name()
{
return name;
}
//PersonalInfo.h
#ifndef PERSONINFO_INCLUDE
#define PERSONINFO_INCLUDE
class PersonalInfo
{
public:
char add[20];
char email[20];
void input_data();
void show_data();
char* get_add();
char* get_email();
};
#endif
//PersonalInfo.cpp
#include
#include "PersonalInfo.h"
using namespace std;
void PersonalInfo::input_data()
{
cout << "Address: ";
cin.ignore();
cin >> add;
cout << "Email address: ";
cin.ignore();
cin >> email;
cout << " ";
}
void PersonalInfo::show_data()
{
cout << endl
<< "Address: " << add;
cout << endl
<< "Email Address : " << email;
}
char* PersonalInfo::get_add()
{
return add;
}
char* PersonalInfo::get_email()
{
return email;
}
//Person.h
#ifndef PERSON_INCLUDE
#define PERSON_INCLUDE
#include "Employer.h"
#include "PersonalInfo.h"
class Person
{
public:
Employer EInfo;
PersonalInfo PInfo;
void create();
void show_person();
long get_phone();
void operator=(Person p);
char* get_name();
char* get_add();
char* get_email();
};
#endif
//Person.cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include "Person.h"
#include "Employer.h"
#include "PersonalInfo.h"
using namespace std;
void Person::create()
{
EInfo.input_data();
PInfo.input_data();
}
void Person::show_person()
{
EInfo.show_data();
PInfo.show_data();
}
void Person::operator=(Person a)
{
EInfo.ph = a.get_phone();
strcpy(EInfo.name, a.get_name());
strcpy(PInfo.add, a.get_add());
strcpy(PInfo.email, a.get_email());
}
long Person::get_phone()
{
return EInfo.get_phone();
}
char* Person::get_name()
{
return EInfo.get_name();
}
char* Person::get_add()
{
return PInfo.get_add();
}
char* Person::get_email()
{
return PInfo.get_email();
}
fstream fp;
Person per;
void write_persons(vector
{
fp.open("contactBook.dat", ios::out);
per.create();
for (int i = 0; i < data.size(); i++)
{
fp.write((char *)&data[i], sizeof(Person));
}
fp.close();
cout << endl
<< endl
<< "Customers have been saved to file!";
getchar();
}
void show_all_person(vector
{
cout << " \t\t-------------------------------- \t\t\tLIST OF CUSTOMERS \t\t-------------------------------- ";
for (int i = 0; i < data.size(); i++)
{
data[i].show_person();
cout << endl
<< "----------------------------------------------- "
<< endl;
}
}
void show_all_person()
{
//system("clear");
cout << " \t\t-------------------------------- \t\t\tLIST OF CUSTOMERS \t\t-------------------------------- ";
fp.open("contactBook.dat", ios::in);
while (fp.read((char *)&per, sizeof(Person)))
{
per.show_person();
cout << endl
<< "----------------------------------------------- "
<< endl;
}
fp.close();
}
void display_Person(long num)
{
bool found;
int ch;
found = false;
fp.open("contactBook.dat", ios::in);
while (fp.read((char *)&per, sizeof(Person)))
{
if (per.get_phone() == num)
{
system("clear");
per.show_person();
found = true;
}
}
fp.close();
if (found == false)
{
cout << " No results found. Please try again.";
}
getchar();
}
void display_Person(vector
{
for (int i = 0; i < data.size(); i++)
{
if (data[i].get_phone() == x)
{
system("clear");
data[i].show_person();
return;
}
}
cout << " No results found. Please try again. ";
}
void edit_person(vector
{
system("clear");
long num;
cout << "Edit customer ------------------------------- \tEnter the number of the customer that you wish to change:";
cin >> num;
for (int i = 0; i < data.size(); i++)
{
if (data[i].get_phone() == num)
{
data[i].show_person();
cout << " Please enter the new customer information: " << endl;
data[i].create();
cout << endl
<< endl
<< "\t Customer has been changed!";
return;
}
cout << " No results found. Please try again. ";
}
}
//Edit the customer information to update/correct information
void edit_person()
{
long num;
bool found = false;
system("clear");
cout << "Edit customer ------------------------------- \tEnter the number of the customer that you wish to change:";
cin >> num;
fp.open("contactBook.dat", ios::in | ios::out);
while (fp.read((char *)&per, sizeof(Person)) && found == false)
{
if (per.get_phone() == num)
{
per.show_person();
cout << " Please enter the new customer information: " << endl;
per.create();
int pos = -1 * sizeof(per);
fp.seekp(pos, ios::cur);
fp.write((char *)&per, sizeof(per));
cout << endl
<< endl
<< "\t Customer has been changed!";
found = true;
}
}
fp.close();
if (found == false)
cout << endl
<< endl
<< "Customer not found. Please try again.";
}
void delete_person(vector
{
long num;
system("clear");
cout << endl
<< endl
<< "Please enter the customers number: ";
cin >> num;
for (int i = 0; i < data.size(); i++)
{
if (data[i].get_phone() == num)
{
for (int j = i + 1; j < data.size(); j++)
{
data[j - 1] = data[j];
}
cout << endl
<< endl
<< "\tCustomer Deleted.";
return;
}
}
cout << endl
<< endl
<< "Customer not found. Please try again.";
}
//Delete a customer from the array
void delete_person()
{
long num;
system("clear");
cout << endl
<< endl
<< "Please enter the customers number: ";
cin >> num;
fp.open("contactBook.dat", ios::in | ios::out);
fstream fp2;
fp2.open("Temp.dat", ios::out);
fp.seekg(0, ios::beg);
while (fp.read((char *)&per, sizeof(Person)))
{
if (per.get_phone() != num)
{
fp2.write((char *)&per, sizeof(Person));
}
}
fp2.close();
fp.close();
remove("contactBook.dat");
rename("Temp.dat", "contactBook.dat");
cout << endl
<< endl
<< "\tCustomer Deleted.";
}
void read_person(vector
{
fp.open("contactBook.dat", ios::in);
if (!fp)
{
cout << "Error opening file ";
return;
}
int count = 0;
Person per;
while (fp.read((char *)&per, sizeof(Person)))
data.push_back(per);
}
void add_person(vector
{
Person per;
per.create();
data.push_back(per);
cout << endl
<< endl
<< "Customers has been added!";
}
//Main module for the menu
int main(int argc, char *argv[])
{
vector
cout << "\t\t\t\t-\t-";
cout << "\t\t\t\t--\t--";
cout << "\t\t\t\t---\t---";
cout << "\t\t\t\t----\t----";
cout << "\t\t\t\t-----\t-----";
cout << "\t\t\t\t------\t------";
cout << "\t\t\t\t-------\t-------";
cout << "\t\t\t\t------\t-------";
cout << "\t\t\t\t------\t------";
cout << "\t\t\t\t-----\t-----";
cout << "\t\t\t\t----\t----";
cout << "\t\t\t\t---\t---";
cout << "\t\t\t\t--\t--";
cout << "\t\t\t\t-\t-";
read_person(data);
for (;;)
{
int ch;
cout << " \t ---- Welcome to the Contact Management System ----";
cout << " \t\t\tMAIN MENU \t\t---------------------- \t\t[1] Add a new customer \t\t[2] List all customers \t\t[3] Search for a customer \t\t[4] Edit a customer \t\t[5] Delete a customer \t\t[0] Exit \t\t------------------ \t\t";
cout << "Enter your choice:";
cin >> ch;
switch (ch)
{
case 0:
cout << " \t\tHave a great day!";
write_persons(data);
exit(0);
break;
break;
break;
case 1:
add_person(data);
break;
case 2:
show_all_person(data);
break;
case 3:
long num;
system("clear");
cout << " \tPhone: ";
cin >> num;
display_Person(data, num);
break;
case 4:
edit_person(data);
break;
case 5:
delete_person(data);
break;
default:
break;
}
int opt;
cout << " Enter your choice: \t[1] Main Menu\t\t[0] Exit ";
cin >> opt;
switch (opt)
{
case 1:
system("clear");
continue;
case 0:
write_persons(data);
exit(0);
}
}
return 0;
}
Step 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