PEmployee::PEmployee (string employee_name, double initial_salary)
{
person_data.get_name() = 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;
}
get_name() is a getter function, I dont know why its not giving an error when you compile but I would change that for something like:
lab10-2.cpp: In constructor 'PEmployee::PEmployee(std::string, double)':
lab10-2.cpp:16:10: error: 'std::string Person::name' is private
lab10-2.cpp:65:16: error: within this context
What I could understand is:
1. name declared in Person is private, so member function under PEmployee could not access. Question is, what should I do in order for the PEmployee could access name in Person?
2. In int main, if I'm not wrong, PEmployee f("Patrick", "1000.00"), means it could be used for class PEmployee. What should I do in order for the name in class Person can read that?
I imagined there would be errors.. :$ Im also a noob at this but a seasoned programmer in other languages.
You could fix it by changing the private access in Person to public but that beats the purpose of the exercise Im sure.
Another workaround is to make a setter function in Person:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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){name = sName;}
private: //data fields//
string name;
int age; /*0 if unknown*/
};
to purefan: ahaha, you helping me is more than enough :). You volunteered, I imagine full professional solution and help would require payment.
It works! I changed it a bit though.
Because our lecturer discourage us on writing member function straight inside the class( even warned us of getting zero marks if we ever do our lab sessions like that), so, instead of writing
'void setName (string sName) {name= sName} straight inside the class like yours, I am required to declare the member function like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class Person
{
public: // contstructor 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*/
};
then below it I add a member function set like this:
1 2 3 4 5
void Person::setName(string sName)
{
name = sName;
}
Then, below PEmployee, I wrote like yours.
And now, the output is:
Patrick earns a salary of 1000
Now, I can rest a bit from programming and focus on other subjects for the night. I still need to practice my Calculus and Database System @.@ Can't try to excel in Programming but fail in other subject ^_^.