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:
PART (A)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; };
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.
PART (B)Code:Employee :: Employee(string first, string last) { first=new string; last=new string; first=*firstname; last=*lastname; empCount++; }
Write the definition of the function ~Employee() which deallocates the dynamic memory and
decrease the value of empCount by 1.
PART (C)Code:Employee :: ~ Employee() { delete firstname; delete lastname; empCount--; }
Write the definition the function getEmpCount() which returns the value of empCount.
PART (D)Code:int Employee:: getempCount() { return empCount; }
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.
· Increment the value of counting in xEmp by 1.Code:Employee xEmp("Hilary","Sam");
· Print the value of empCount.Code:xEmp.counting++;
Code:cout<<xEmp.getempCount;



LinkBack URL
About LinkBacks



