Hello,
I have tried to solve an exercise, because i want to make sure if i've understand what i've learned.
I hope that you can let me know if thier is any mistakes, and i will try to solve it.
Question 1
Consider the following definition of the class Employee:
Code:
class Employee
{ private:
string * firstName;
string * lastName;
static int empCount;
public:
Employee(string first, string last);
~Employee();
string getFirstName();
static int getEmpCount();
static int counting;
};
PART (A)
Write the definition of the constructor function Employee(string first, string
last); which dynamically allocates memory for firstName and lastName and copies first
and last to them. The function should increment empCount by 1. Use deep copy.
Code:
Employee :: Employee(string first, string last)
{
first=new string;
last=new string;
first=*firstname;
last=*lastname;
empCount++;
}
PART (B)
Write the definition of the function ~Employee() which deallocates the dynamic memory and
decrease the value of empCount by 1.
Code:
Employee :: ~ Employee()
{
delete firstname;
delete lastname;
empCount--;
}
PART (C)
Write the definition the function getEmpCount() which returns the value of empCount.
Code:
int Employee:: getempCount()
{
return empCount;
}
PART (D)
In the main function add statements to do the following:
· Using the following declarations: string first=”Hilary”, last=”Sam”, create an object of
type class Employee named xEmp.
Code:
Employee xEmp("Hilary","Sam");
· Increment the value of counting in xEmp by 1.
· Print the value of empCount.
Code:
cout<<xEmp.getempCount;