سؤال من امتحان سابق
ممكن تشوفون لي اذا عندي اخطاء او لا
Consider the following definition of the class Employee:
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) [8 Points]
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.
كود:
Employee::Employee(string first, string
last)
{
firstname=new string first;
lastname=new string last;
empCount++;
}
PART (B) [4 Points]
Write the definition of the function ~Employee() which deallocates the dynamic memory and
decrease the value of empCount by 1.
كود:
Employee::~Employee()
{
delete firstname;
delete lastname;
empCount--;
}
PART (C) [3 Points]
Write the definition the function getEmpCount() which returns the value of empCount.
كود:
Employee::static int getEmpCount(){
return empCount;
}
PART (D) [3 Points]
In the main function add statements to do the following:
· Using the following declarations: string first=”Sami”, last=”Noor”, create an object of
type class Employee named xEmp.
· Increment the value of counting in xEmp by 1.
· Print the value of empCount.
كود:
Employee xEmp;("Sami","Noor");
xEmp.counting++;
xEmp.getEmpCount();