class Employee{private: string firstName; string * lastName; int hourWorked; dou
ID: 3830329 • Letter: C
Question
class Employee{private: string firstName; string * lastName; int hourWorked; double PayRate; double Salary; public://1. write separate default and explicit constructors. The memory allocation for//pointer lastName is done inside the constructor.//2. Write a copy constructor that makes deep copy.//3. Write an assignment operator that makes deep copy and deletes the memory//allocated to the pointer which is being given a new pointee.//4. Write a virtual destructor that de-allocates the memory allocated by//constructors.Explanation / Answer
class Employee
{
private:
string firstName;
string *lastName;
int hourWorked;
double PayRate;
double Salary;
public:
Employee(Employee &emp); // copy constructor
explicit Employee(void) = default;//default constructor
explicit Employee(String *lnam = NULL)
{
lastName = lnam;
}
Employee() // Memeory allocation
{
lastName string[2];
}
~Employee() //virtual constructor that deallocates memory
{
delete[] ln;
}
};
//copy constructor
Employee::Employeee(Employee &emp)
{
*this = emp;
}
Employee lastName(new string());
*lastName = 'xxx';
cout<<*lastName;
}
//assignment operator with no memory allocation
Employee::Employee(Employee const ©Employee)
{
ptr = new string(*(copyEmployee.ptr));
}
int main()
{
Employee e();
Employee lastName(new string());//default and explicit constructor.
//pointer lastName is dont inside the constructor
*lastName = 'xxx';
cout<<*lastName;
//assignment operator, makes deep copy and deletes the memory
Employee operator=(Employee const& othEmployee)
{
this->p = *(othEmployee.ptr)
return *this;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.