『代码』··By/蜜汁炒酸奶

friend关键字使用之雇员与雇主类-C++

通过 friend关键字实现的雇员类与雇主类

/*
功能:雇员与雇主类.
日期:2013-11-07
*/
#include <iostream>
#include <string>
using namespace std;
class Employer;
//雇员
class Employee
{
public:
	Employee(int aNum=0,string aName=NULL,int aPay=0,int aGrade=0,string aPosition=NULL);
	~Employee();
	 void Display(void);
	 void setEmployee(void);
	 string getName(void);
	 friend Employer;
private:
	int m_aNumber;
	string m_aName;
	int m_aPay;
	int m_aGrade;
    string m_aPosition;
};
//雇主
class Employer
{
public:
	Employer(int iNumber=0,string iName=NULL);
    ~Employer();
	void resetEmployee(Employee&);
	string getName();
    void Display(Employee &);
private:
   int m_iNumber;
   string m_iName;

};

Employee::Employee(int aNum,string aName,int aPay,int aGrade,string aPosition)
{
     m_aNumber=aNum;
	 m_aName=aName;
	 m_aPay=aPay;
	 m_aGrade=aGrade;
	 m_aPosition=aPosition;
}
Employee::~Employee()
{

}
Employer::Employer(int iNumber,string iName)
{
        m_iNumber=iNumber;
		m_iName=iName;
}
Employer::~Employer()
{

};

string Employee::getName(void)
{
	return m_aName;
}
void Employee::Display(void)
{
	 cout<<m_aNumber<<" "<<m_aName<<" "<<m_aPay<<" "<<m_aGrade<<" "<<m_aPosition;
}

void Employer::resetEmployee(Employee&obj)
{
	 cout<<"请输入新的薪水、等级和职位:";
     cin>>obj.m_aPay>>obj.m_aGrade>>obj.m_aPosition;
}

void Employer::Display(Employee &obj)
{
	cout<<m_iName<<"老板调整了"<<obj.getName()<<"的信息:";
	obj.Display();
}

int main(void)
{

	int number;
	string name;
	int pay;
	int grade;
	string position;
	int i;
	cout<<"请输入员工的编号、姓名、薪水、等级和职位:";
	cin>>number>>name>>pay>>grade>>position;
    Employee employee1(number,name,pay,grade,position);
	cout<<"请输入员工的编号、姓名、薪水、等级和职位:";
	cin>>number>>name>>pay>>grade>>position;
	Employee employee2(number,name,pay,grade,position);
	cout<<"请输入老板的编号和姓名:";
	cin>>number>>name;
    Employer employer1(number,name);
	cout<<"请输入要修改人的序号:"<<endl;
	cout<<"1."<<employee1.getName()<<endl;
    cout<<"2."<<employee2.getName()<<endl;

	cin>>i;
	if (i==1)
	{
		employer1.resetEmployee(employee1);
		employer1.Display(employee1);
	}
	else if (i==2)
	{
		employer1.resetEmployee(employee2);
		employer1.Display(employee2);
	}
	else
	{
      cout<<"输入有误";
	}
	cout<<endl;
	system("pause");
	return 0;
}
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

预览
Loading comments...
0 条评论

暂无数据

example
预览