自考新教材--p79
源程序:
#include <iostream>
#include <string>
using namespace std;
class CEmployee
{
private:
string szName;
int salary;
public:
void setName(string);
string getName();
void setSalary(int);
int getSalary();
int averageSalary(CEmployee); //计算平均工资
};
void CEmployee::setName(string name)
{
szName = name;
}
string CEmployee::getName()
{
return szName;
}
void CEmployee::setSalary(int mon)
{
salary = mon;
}
int CEmployee::getSalary()
{
return salary;
}
int CEmployee::averageSalary(CEmployee el)
{
return (salary + el.getSalary()) / 2;
}
int main()
{
CEmployee eT, eY;
//eT.szName="Tom1234567"; //错误,不能直接访问私有成员
eT.setName("Tom1234567");
//eT.setSalary = 5000; //错误,不能直接访问私有成员
eT.setSalary(5000);
cout << eT.getName() << endl;
eY.setName("Yong7654321");
eY.setSalary(3500);
cout << eY.getName() << endl;
cout << "aver=" << eT.averageSalary(eY) << endl;
system("pause");
return 0;
}
运行结果: