注册 登录
编程论坛 C++教室

关于友元类

天涯为客 发布于 2012-10-26 22:59, 574 次点击
编写一个程序,完成员工信息的录入与显示。
(1) Employ类
基本信息:编号、姓名、性别、出生日期、职位等,其中出生日期使用自定义的Date(日期)类
成员函数:GetDate( )函数用来返回出生日期,GetNum( )函数用来返回员工的编号、GetName( )用来返回员工的姓名、GetSex( )函数用来返回员工的性别,GetPost( )函数用来返回员工的职位。
其中,类的数据成员为private类型。
(2) Date类
基本信息:年、月、日
成员函数:GetYear( )、GetMonth( )、GetDay( )分别用来返回年、月、日
要求:定义Enter( )函数用来对员工信息的录入;Display( )函数用来显示员工的信息。将员工的信息保存在对象数组中,在main函数中调用Enter( )函数和Display( )函数完成员工信息的录入与显示。
3 回复
#2
超级菜鸟手2012-10-27 08:58
大致上 这样编写 就可以了

#include <iostream>
#include <string>
using namespace std;


class Date{
      protected:
      string year;
      string month;
      string day;
      public:
             void setYear(string y){
                  year = y;}
             void setMonth(string m){
                  month = m;}
             void setDay(string d){
                  day = d;}
};

class Employee:public Date{
      string num;
      string name;
      string sex;
      string date;
      string post;
      public:
            
             void setNum(string n){
                  num = n;}
             void setName(string m){
                  name = m;}
             void setSex(string s){
                  sex = s;}
             void setPost(string p){
                  post = p;}
             string getDate(){return (day + "/" + month + "/" + year);}
             string getNum(){return num;}
             string getName(){return name;}
             string getSex(){return sex;}
             string getPost(){return post;}
};

void Display(Employee &);
void Enter(string &);

int main(){
   
    Employee info;
    string num, name, sex, post, day, month, year;
   
    cout<<"Number : ";
    Enter(num);
    info.setNum(num);
   
    cout<<"Name   : ";
    Enter(name);
    info.setName(name);
   
    cout<<"Birthday Info"<<endl
        <<"Day : ";
    Enter(day);
    info.setDay(day);
    cout<<"Month : ";
    Enter(month);
    info.setMonth(month);
    cout<<"Year : ";
    Enter(year);
    info.setYear(year);
   
    cout<<"Sex    : ";
    Enter(sex);
    info.setSex(sex);
   
    cout<<"Post   : ";
    Enter(post);
    info.setPost(post);
    system("cls");

    cout<<"**** Employee Info ****"<<endl;
    Display(info);
    system("PAUSE");
    return 0;
}
   
void Enter(string &message){
     getline(cin,message);}
     
void Display(Employee &Info){

     cout<<"Number    : "<<Info.getNum()<<endl
         <<"Name      : "<<Info.getName()<<endl
         <<"Date      : "<<Info.getDate()<<endl
         <<"Sex       : "<<Info.getSex()<<endl
         <<"Post      : "<<Info.getPost()<<endl;
}
#3
天涯为客2012-10-27 13:47
谢了
#4
超级菜鸟手2012-10-27 22:19
如果没问题 就结贴吧  
1