In oop pliz with headers and implementation Create a class template, called Comp
ID: 666574 • Letter: I
Question
In oop pliz with headers and implementation
Create a class template, called CompanyTemplate, which can be used to generate classes . The class Company has the following attributes: name (string) address (string), list of employees (an array of objects of the Employee class), and number of employees the company has (int). The class must implement the following functions: • A default constructor, a regular constructor, a copy constructor, a destructor • Accessing functions • A function that adds an employee to the company (an exception should be thrown if the employee already exists) • A function that removes an employee from the company (an exception should be thrown if the employee does not exist) • A function that returns the number of employees of the company • A function that returns the list (names) of the employees • A function that prints out the name and address of the company
Explanation / Answer
#include <iostream>
#include <malloc.h>
using namespace std;
class Employee{
public:
string name;
};
class CompanyTemplate{
string name, address;
public:
Employee array[100];
static int numOfEmployees;
CompanyTemplate(){
numOfEmployees = 0;
}
CompanyTemplate(string n, string a, Employee e[], int num){
name = n;
address = a;
numOfEmployees = 0;
for(int i = 0; i < numOfEmployees; i++) array[i] = e[i];
}
int search(Employee e){
for(int i = 0; i < numOfEmployees; i++){
if(e.name == array[i].name) return i;
}
return -1;
}
struct alreadyExists : public exception{
const char * what () const throw (){
return "Employee already exists";
}
};
void add(Employee e){
int index = search(e);
try{
if(index != -1) throw alreadyExists();
array[numOfEmployees++] = e;
}
catch(alreadyExists& e){
cout << e.what() << " ";
}
}
struct notExist : public exception{
const char * what () const throw (){
return "Employee doesn't exists";
}
};
void remove(Employee e){
int index = search(e);
try{
if(index != -1) throw notExist();
array[numOfEmployees++] = e;
}
catch(notExist& e){
cout << e.what() << " ";
}
}
int getNumberOfemployees(){
return numOfEmployees;
}
string* list(){
string *l = (string *) malloc(sizeof(string *) * numOfEmployees);
for(int i = 0; i < numOfEmployees; i++){
l[i] = array[i].name;
}
return l;
}
void display(){
cout << "Name: " << name << ", Address: " << address << " ";
}
};
int main(){
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.