#include <iostream>
#include <string>
#include <cstdlib>
#define MAX 2
using namespace std;
/*class Worker
{
private:
string wk_num; //工号
string name; //姓名 (此项可以公开)
int age; //年龄
char sex; //性别(此项可以公开)
float pay;
};*/ //创建一个类,很多数据由我们来输入,但是,只能不能输出。做到保密
class Worker_Base
{
protected:
string name;
char sex;
public:
void setname(string nam)
{name=nam;}
void setsex(char s){sex=s;}
void show()
{
cout<<"对于所有员工只输出这两项,其他的为保密!"<<endl;
cout<<"姓名:"<<name<<endl;
cout<<"性别: "<<sex<<endl;}
};
class Worker_Derive:public Worker_Base
{
private:
string wk_num;
int age;
float pay;
public:
void set_wk_num(string num){wk_num=num;}
void setage(int a){age=a;}
void set_pay(float p){pay=p;}
friend void display(Worker_Derive &wkd);
};
void display(Worker_Derive &wkd) //这里是全部输出信息,但这是一个非成员函数友元函数。
{
cout<<"姓名:"<<wkd.name<<endl;
cout<<"性别:"<<wkd.sex<<endl;
cout<<"工号:"<<wkd.wk_num<<endl;
cout<<"年龄:"<<wkd.age<<endl;
cout<<"工资:"<<wkd.pay<<endl;}
int main(int argc, char *argv[])
{
string name;
char sex;
string wk_num;
float pay;
int age,i=0;
Worker_Derive wk[MAX]; //子类
Worker_Base wkb[MAX]; //父类的引用使用子类来赋值
for(i=0;i<MAX;i++)
{
cout<<"请输入第"<<i+1<<"位同志的信息"<<endl;
cout<<"请您输入姓名:"<<endl;
cin>>name;
wk[i].setname(name);
cout<<"请您输入性别:"<<endl;
cin>>sex;
wk[i].setsex(sex);
cout<<"请您输入年龄:"<<endl;
cin>>age;
wk[i].setage(age);
cout<<"请您输入工号:"<<endl;
cin>>wk_num;
wk[i].set_wk_num(wk_num);
cout<<"请您输入工资:"<<endl;
cin>>pay;
wk[i].set_pay(pay);
cout<<endl;
}
system("cls");
for(i=0;i<MAX;i++)
{
wkb[i]=wk[i];
wkb[i].show();}
char chChoice;
cout<<"请问您是否想知道员工的所有信息?(Y/N)"<<endl;
cin>>chChoice;
if(chChoice=='Y'||chChoice=='y')
{
string password;
int count=0;
M:
cout<<"请输入密码:";
cin>>password;
count++;
if(password=="123")
{for(int i=0;i<MAX;i++)
display(wk[i]);}
else
{
if(count<=3)
{cout<<"密码错误!"<<endl;
goto M;}
else
exit(0);
}
}
cin>>"";
}
/*这里是尝试子类对象向父类对象赋值,可以改用指针实现,
即定义一个父类的指针,指针地址使用子类对象的地址赋值*/