Make Program Able To Read Name

greetings all,

I am still a beginner in this, I apologize if I sound a little..dumb...

Okay...


My code is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#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;
		
	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;
		}


	

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.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;
	}
	

	
	
	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;
	return 0;
}



I need to get 'Patrick earns a salary of 1000.00'.

But with the code, what I get is: 'earns a salary of 1000.00'

What I would like to know is if:

1. Am I required to add something, in order for Person able to read Patrick even though in int main() it's under PEmployee?

2. even though I put double salary, why does it come out '1000' instead of '1000.00' ?


Thanks in advance...


Sincerely,
Liana Zaman
1
2
3
4
5
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:
1
2
3
4
5
6

PEmployee::PEmployee (string employee_name, double initial_salary)
	{
		 person_data = new Person(employee_name, 10); // defaulting to page = 10
		salary = initial_salary;
	}


not sure about the access but otherwise this may work:

1
2
3
4
5
6

PEmployee::PEmployee (string employee_name, double initial_salary)
	{
		 person_data.name = employee_name;
		salary = initial_salary;
	}


hope it helps.
Hi purefans,

First of all, thanks for replying! :D

When I tried:


1
2
3
4
5
PEmployee::PEmployee (string employee_name, double initial_salary)
	{
		 person_data = new Person(employee_name, 10); // defaulting to page = 10
		salary = initial_salary;
	}



it gave out:

lab10-2.cpp: In constructor 'PEmployee::PEmployee(std::string, double)':
lab10-2.cpp:65:59: error: invalid conversion from 'Person*' to 'char'
lab10-2.cpp:65:59: error:   initializing argument 1 of 'std::basic_string<_CharT
, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT
) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator
<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]'



When I tried:
1
2
3
4
PEmployee::PEmployee (string employee_name, double initial_salary)
	{
		 person_data.name = employee_name;
		salary = initial_salary;


it came out:
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*/
};

Then you could (or I think you could):
1
2
3
4
5
PEmployee::PEmployee (string employee_name, double initial_salary)
{
	person_data.setName(employee_name);
	salary = initial_salary;
}


Again, hope it helps :)
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 ^_^.


Thank you!!! :D
Topic archived. No new replies allowed.