plz help me with this

Hi,
One thing I would like to know you is, it is not part of my project or assignment. It is just a exercise from my text book.
Problem is I can't find a error in my code. Could someone help me???
If so, here is the question.
Implement a class PEmployee that is just like the Employee class except that it stores an object type Person as developed in the preceding exercise.

My code is
[code]
#include "stdafx.h"
#include<iostream>
#include<string>

using namespace std;

class Person
{
public:
Person();
Person(string pname, int page);
void geta_name() const;
void geta_age() const;
private:
string name;
int age;
};

Person::Person()
{
}

Person::Person(string pname, int page)
{
name = pname;
age = page;
}

void Person::geta_name() const
{
cout << name;
}

void Person::geta_age() const
{
cout << age;
}

class PEmployee
{
public:
PEmployee();
PEmployee(string e_name, double ini_salary);
void set_salary (double new_salary);
double get_salary() const;
string get_name() const;
private:
Person p_data;
double salary;
};

PEmployee::PEmployee()
{
}

PEmployee::PEmployee(string e_name, double ini_salary)
{
Person p_data ("Min", 27);
}

void PEmployee::set_salary (double new_salary)
{
cout << "new salary: ";
cin >> new_salary;
salary = new_salary;
}

double PEmployee::get_salary() const
{
return salary;
}

string PEmployee::get_name() const
{
return p_data; //I think that I am wrong here
} //I don't know how to fix it


int main()
{
double new_s;
string name;
int sal;

PEmployee emp(name, sal);
emp.set_salary(new_s);
cout << endl;

emp.get_name();
cout << "\n";
emp.get_salary();
cout << "\n";

return 0;
}
[code]

Plz check it for me and explain me why should I do this and shouldn't do that.
Thanks!
Whats the compiling problem? There isn't a "right way" to code, but there are better ways once you learn them. In other words, if the program works as required by the psuedocode or the problem parameters, then your fine.

One thing I would recommend is using ADT till you get a better grip on classes. Separating the implementation and the class declaration.
1
2
3
4
5
6
7
8
9
return type dont match:
p_data is Person type

this must be  corrent:

string PEmployee::get_name() const
{
return p_data.get_name(); //I think that I am wrong here              
} //I don't know how to fix it 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
this is wrong,
if you declare here p_data, it will be local variable, not global
PEmployee::PEmployee(string e_name, double ini_salary)
{
Person p_data ("Min", 27);
}

that is why 
use like this:

if you declare here p_data, it will be local variable, not global
PEmployee::PEmployee(string e_name, double ini_salary)
{
Person p_data_temp ("Min", 27);                            
p_data = p_data_temp;
}



1
2
3
4
5
6
7
8
9

u have to change geta_name(();

string Person::geta_name() const
{
	cout << name;
	return name;
}
Last edited on
this is last modified version, just try to compile
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
97
98
99
100
101
102
103
104
105
106
#include<iostream>                                                                      
#include<string>

using namespace std;

class Person
{
public:
	Person();
	Person(string pname, int page);
	string geta_name() const;
	void geta_age() const;
private:
	string name;
	int age;
};


Person::Person(string pname, int page)
{
	name = pname;
	age = page;
}
Person::Person()
{
}

string Person::geta_name() const
{
	cout << name;
	return name;
}

void Person::geta_age() const
{
	cout << age;
}

class PEmployee
{
public:
	PEmployee();
	PEmployee(string e_name, double ini_salary);
	void set_salary (double new_salary);
	double get_salary() const;
	string get_name() const;
private:
	Person p_data;
	double salary;
};

PEmployee::PEmployee()
{
}

PEmployee::PEmployee(string e_name, double ini_salary)
{
	Person p_data_temp(e_name, ini_salary);
	p_data = p_data_temp;
}

void PEmployee::set_salary (double new_salary)
{
	cout << "new salary: ";
	cin >> new_salary;
	salary = new_salary;
}

double PEmployee::get_salary() const
{
	return salary;
}

string PEmployee::get_name() const
{
	// yeah, you were wrong here, 
	// why are you returing p_data(person) to String?
	//return p_data; //I think that I am wrong here

	string str = this->p_data.geta_name();
	return str;
} //I don't know how to fix it


int main()
{
	double new_s;
	string name;
	int sal;

	cout<<"Input name and salary"<<endl;
	cin>>name>>sal;

	PEmployee emp(name, sal);
	cout<<"Enter new salary"<<endl;
	cin>>new_s;
	emp.set_salary(new_s);
	cout << endl;

	emp.get_name();
	cout << "\n";
	emp.get_salary();
	cout << "\n";
	system("pause");
	return 0;
}
Topic archived. No new replies allowed.