Write a class named Employee that has the following properties. • Name: The Name property holds the employee's name. • IdNumber: The IdNumber property holds the employee's ID number. • Department: The Department property holds the name of the department in which the employee works. • Position: The Position property holds the employee's job title. The class should have the following overloaded constructors: • A constructor that accepts the following values as arguments and assigns them to the appropriate properties: employee's Name, IdNumber, Departmentand Position. • A constructor that accepts the following values as arguments and assigns them to the appropriate properties: employee's Name and IdNumber. The Departmentand Position properties should be assigned an empty string. • A parameter less constructor that assigns empty string to Name, Department and Position properties, and 0 to IdNumber.
Question:
Write a class named Employee that has the following properties. • Name: The Name property holds the employee's name. • IdNumber: The IdNumber property holds the employee's ID number. • Department: The Department property holds the name of the department in which the employee works. • Position: The Position property holds the employee's job title. The class should have the following overloaded constructors: • A constructor that accepts the following values as arguments and assigns them to the appropriate properties: employee's Name, IdNumber, Departmentand Position. • A constructor that accepts the following values as arguments and assigns them to the appropriate properties: employee's Name and IdNumber. The Departmentand Position properties should be assigned an empty string. • A parameter less constructor that assigns empty string to Name, Department and Position properties, and 0 to IdNumber.
Answer:
Since it was not specified which programming language to use I have implemented the code in C++ and python.
1) C++ Implementation:
#include<bits/stdc++.h>
using namespace std;
class Employee{
private:
char* Name;
int ID;
char* Dept;
char* Position;
public:
Employee(){
Name=Dept=Position=(char*)"";
ID=0;
}
Employee(int ID,char* Name,char* Dept,char* Position){
this->ID=ID;
this->Name=Name;
this->Dept=Dept;
this->Position=Position;
}
Employee(int ID,char* Name){
this->ID=ID;
this->Name=Name;
Dept=(char*)"";
Position=(char*)"";
}
void printAllStats(){
cout<<"ID "<<ID<<" ";
cout<<"Name "<<Name<<" ";
cout<<"Department "<<Dept<<" ";
cout<<"Position "<<Position<<"\n";
}
};
int main(){
Employee E1(47899,(char*)"Susan Meyers",(char*)"Accounting",(char*)"Vice President");
Employee E2(39119,(char*)"Mark Jones",(char*)"IT",(char*)"Programmer");
Employee E3(81774,(char*)"Joy Rogers",(char*)"Manufracturing",(char*)"Engineer");
cout<<"Employees' Info:\n";
cout<<"Employee1\t";
E1.printAllStats();
cout<<"Employee2\t";
E2.printAllStats();
cout<<"Employee3\t";
E3.printAllStats();
return 0;
}
2) Python Implementation:
class Employee:
def __init__(self):
self.ID=0
self.Name=""
self.Dept=""
self.Position=""
def __init__(self,ID,Name,Dept="",Position=""):
self.ID=ID
self.Name=Name
self.Dept=Dept
self.Position=Position
def printAllStats(self):
return " ".join(["ID",str(self.ID),"Name",self.Name,"Department",self.Dept,"Position",self.Position])
E1=Employee(47899,"Susan Meyers","Accounting","Vice President")
E2=Employee(39119,"Mark Jones","IT","Programmer");
E3=Employee(81774,"Joy Rogers","Manufracturing","Engineer")
print("Employee Info:\n")
print("Employee 1:",E1.printAllStats())
print("Employee 2:",E2.printAllStats())
print("Employee 3:",E3.printAllStats())
- Below I am attaching python code screenshot for Indentation purpose:
- Below is the C++ implementation and Output: