Big C++ Exercise P21.1. In Exercise P5.2 you implemented a class PEmployee that
ID: 3537064 • Letter: B
Question
Big C++
Exercise P21.1. In Exercise P5.2 you implemented a class PEmployee that defined two constructors, a default constructor and a constructor with explicit arguments. Rewrite the class definition so that the default constructor uses constructor chaining to invoke the second constructor.
PEmployee.cpp
#include<iostream>
#include <string>
using namespace std;
class Person
{
public: // constructor and member function declarations//
Person();
Person (string pname, int page);
string get_name() const;
int get_age() const;
void setName(string sName);
private: //data fields//
string name;
int age; /*0 if unknown*/
};
Person::Person()
{ }
Person::Person ( string pname, int page)
{
name= pname;
age= page;
}
string Person::get_name() const
{
return name;
}
int Person:: get_age() const
{
return age;
}
void Person::setName (string sName)
{
name = sName;
}
class PEmployee
{
public:
PEmployee();
PEmployee(string employee_name, double initial_salary);
void set_salary(double new_salary);
double get_salary() const;
string get_name() const;
private:
Person person_data;
double salary;
};
PEmployee::PEmployee (string employee_name, double initial_salary)
{
person_data.setName(employee_name); //person _data.get_name() : as person_data is under person class
//when we put '.get_name', it stores the value in the 'get_name' of the Person//
salary = initial_salary;
}
void PEmployee::set_salary(double new_salary)
{
salary = new_salary;
}
double PEmployee::get_salary() const
{
return salary;
}
string PEmployee::get_name() const
{
return person_data.get_name();
}
int main()
{
PEmployee f("Patrick", 1000.00);
cout << f.get_name() << " earns a salary of "
<< f.get_salary() << endl;
system("pause");
return 0;
}
Explanation / Answer
#include<iostream>
#include <string>
using namespace std;
class Person
{
public: // constructor and member function declarations//
Person();
Person (string pname, int page);
string get_name() const;
int get_age() const;
void setName(string sName);
private: //data fields//
string name;
int age; /*0 if unknown*/
};
Person::Person()
{ }
Person::Person ( string pname, int page)
{
name= pname;
age= page;
}
string Person::get_name() const
{
return name;
}
int Person:: get_age() const
{
return age;
}
void Person::setName (string sName)
{
name = sName;
}
class PEmployee
{
public:
PEmployee();
PEmployee(string employee_name, double initial_salary);
void set_salary(double new_salary);
double get_salary() const;
string get_name() const;
private:
Person person_data;
double salary;
};
PEmployee::PEmployee ()
{
//chaining constructors
PEmployee("dummy_employee_name",0);
}
PEmployee::PEmployee (string employee_name, double initial_salary)
{
person_data.setName(employee_name); //person _data.get_name() : as person_data is under person class
//when we put '.get_name', it stores the value in the 'get_name' of the Person//
salary = initial_salary;
}
void PEmployee::set_salary(double new_salary)
{
salary = new_salary;
}
double PEmployee::get_salary() const
{
return salary;
}
string PEmployee::get_name() const
{
return person_data.get_name();
}
int main()
{
PEmployee f("Patrick", 1000.00);
cout << f.get_name() << " earns a salary of "
<< f.get_salary() << endl;
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.