(I NEED HELP GENERATING THIS CODE IN C++ PLEASE) The following problem should be
ID: 3703607 • Letter: #
Question
(I NEED HELP GENERATING THIS CODE IN C++ PLEASE)
The following problem should be created as a project in Visual Studio, compiled and debugged. Be sure to add a header to your code and properly document the code. Copy your code into a Word document named with your last name included such as: CIS247_Lab4_Smith. Also include screen prints of your test runs. Be sure to run enough tests to show the full operation of your code. Submit this Word document and the project file (zipped).
This is a problem using composition. We will have two classes: AccountingDept and Accountant; AccountingDept will contain an array of Accountants:
Here are the UML class diagrams for your two classes:
Accountant
-fullName : string
-phoneExt: int
+Accountant(string name=””, int ext=0) //constructor with default parameters
+setName(in name : string) : void
+getName() : string
+setExt(in ext : int) : void
+geExt() : int
+displayInfo() : void //Report the full name and 3-digit phone extension on one line.
AccountingDept
-supervisorName : string
-empCount : int //count of accountants in the Department
+empList[25] : Accountant //allocate an array for 25 accountants
+AccountingDept() //initialize supervisorName to empty string; empCount to 0; array needs no initialization
+AccountingDept(in name : string, in count : int, in list[] : Accountant) //see Note 1
+displayDept() : void //display supervisor’s name and loop through array using displayInfo() method from Accountant class
+setSupervisor(in name : string) : void
+getSupervisor() : string
+setCount(in count : int) : void
+getCount() : int
+setList(in list []: Accountant) : void //pass array name; see Note 1
+getList() : Accountant * // returns pointer to array (array name); see Note 2
In your main function:
Declare an array of type Accountant with size of 25 elements.
Greet the user and explain that you are setting up the department organization.
Ask the user for the supervisor’s full name and number of accountants that report to him/her.
Use a for loop based on the number of accountants and ask the user for each person’s full name and phone extension (three digits); use the setters of Accountant to enter the information into the array. Example – if you were using a for loop based on the counter x and your array was named workers:
workers[x].setName(inputName);
Create an AccountingDept object, passing the supervisor’s name, employee count and array (pass array name)
Use the AccountingDept displayDept() to list your final results.
NOTE 1: When using arrays, you cannot set one array equal to another array like this:
ILLEGAL: array1[] = array2[];
You have to copy the arrays, element by element, using a for loop.
NOTE 2: Notice the asterisk after the datatype Accountant in getList() above; use this in coding; getList() returns a pointer to the array:
Accountant * getList() { return &empList[0]; }
Test your program entering at least 3 different accountants.
Accountant
-fullName : string
-phoneExt: int
+Accountant(string name=””, int ext=0) //constructor with default parameters
+setName(in name : string) : void
+getName() : string
+setExt(in ext : int) : void
+geExt() : int
+displayInfo() : void //Report the full name and 3-digit phone extension on one line.
Explanation / Answer
//All the code is in here. Paste it, compile it, zip it as necessary.
// AccountingDepartment.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include <string>
using namespace std;
class Accountant
{
public:
string fullName;
int phoneExt;
Accountant(string name="", int ext=0) //constructor with default parameters
{
fullName = name;
phoneExt = ext;
}
void setName(string name );
string getName();
void setExt(int ext );
int getExt();
void displayInfo(); //Report the full name and 3-digit phone extension on one line.
};
class AccountingDept
{
public:
string supervisorName;
int empCount; //count of accountants in the Department
Accountant empList[25]; //allocate an array for 25 accountants
AccountingDept() //initialize supervisorName to empty string; empCount to 0; array needs no initialization
{
}
AccountingDept(string name , int count, Accountant list[]) //see Note 1
{
supervisorName = name;
empCount = count;
setList(list);
}
void displayDept(); //display supervisor’s name and loop through array using displayInfo() method from Accountant class
void setSupervisor(string name);
string getSupervisor();
void setCount(int count);
int getCount();
void setList(Accountant list []); //pass array name; see Note 1
Accountant * getList(); // returns pointer to array (array name); see Note 2
};
int _tmain(int argc, _TCHAR* argv[])
{
Accountant workers[25];
string strSupervisorFullName;
string strEmployeeFullName;
int strEmployeeExtension;
int iNumberofEmployees;
cout<<""<<endl;
cout<<"Welcome Dear Supervisor"<< endl;
cout<<""<<endl;
cout<<"We will now setup your department"<< endl;
cout<<""<<endl;
cout<<"Please provide your (Supervisor) name:";
cin>>strSupervisorFullName;
cout<<""<<endl;
cout<<"That's great " + strSupervisorFullName<< endl;
cout<<""<<endl;
cout<<"Please provide the Number of employees in your department:";
cin>>iNumberofEmployees;
cout<<""<<endl;
cout<<"That's great. Please provide name and 3 digit extension of each employee now " << endl;
for( int x = 0; x < iNumberofEmployees; x = x + 1 ) {
cout<<""<<endl;
cout << "Employee number:" << x+1 << endl;
cout<< "Employee Name: ";
cin>>strEmployeeFullName;
cout<< "Employee Extension: ";
cin>>strEmployeeExtension;
workers[x] = Accountant(strEmployeeFullName,strEmployeeExtension);
//workers[x].setName(strEmployeeFullName);
//workers[x].setExt(strEmployeeExtension);
}
AccountingDept dept = AccountingDept(strSupervisorFullName,iNumberofEmployees,workers);
dept.displayDept();
return 0;
}
/************ AccountingDept class implementation **********/
void AccountingDept::displayDept()
{
cout<<""<<endl;
cout<<""<<endl;
cout << "Let's display your dept" << endl;
cout << "------------------------" << endl;
cout<<""<<endl;
cout << "Supervisor:" << supervisorName << " ( Total " << empCount << " employees )" << endl;
cout<<""<<endl;
cout << "LIST OF EMPLOYEES" << endl;
cout << "-----------------" << endl;
Accountant *list = getList();
for( int x = 0; x < empCount ; x = x + 1 ) {
//empList[x].displayInfo();
list[x].displayInfo();
}
cout<<""<<endl;
}
void AccountingDept::setSupervisor(string name)
{
supervisorName = name;
}
string AccountingDept::getSupervisor()
{
return supervisorName;
}
void AccountingDept::setCount(int count)
{
empCount =count;
}
int AccountingDept::getCount()
{
return empCount;
}
void AccountingDept::setList(Accountant list [])
{
for( int x = 0; x < empCount ; x = x + 1 ) {
empList[x] = list[x];
}
} //pass array name; see Note 1
Accountant * AccountingDept::getList()
{
return empList;
} // returns pointer to array (array name); see Note 2
/************* Accountant class implementation *************/
void Accountant::setName(string name )
{
fullName = name;
}
string Accountant::getName()
{
return fullName;
}
void Accountant::setExt(int ext )
{
phoneExt = ext;
}
int Accountant::getExt()
{
return phoneExt;
}
void Accountant::displayInfo(){
cout << fullName << " (Extension: " << phoneExt << ")" << endl;
} //Report the full name and 3-digit phone extension on one line.
/************************ TEST RUN OUTPUT ****************************/
/*
D:UsersjdsDocumentsVisual Studio 2010ProjectsAccountingDepartmentDebug>Ac
countingDepartment
Welcome Dear Supervisor
We will now setup your department
Please provide your (Supervisor) name:JamesDSouza
That's great JamesDSouza
Please provide the Number of employees in your department:3
That's great. Please provide name and 3 digit extension of each employee now
Employee number:1
Employee Name: RogerMoore
Employee Extension: 323
Employee number:2
Employee Name: JamesBond
Employee Extension: 454
Employee number:3
Employee Name: RockyBalboa
Employee Extension: 456
Let's display your dept
------------------------
Supervisor:JamesDSouza ( Total 3 employees )
LIST OF EMPLOYEES
-----------------
RogerMoore (Extension: 323)
JamesBond (Extension: 454)
RockyBalboa (Extension: 456)
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.