当前位置:编程学习 > C/C++ >>

C++代码 帮忙注释一下

#include <iostream>

#include <string>

using namespace std;

class EmpSalary

{public:

       float Wage;                 

       float Subsidy;               

       float Rent;                   

       float CostOfElec;          

       float CostOfWater;        

public:

       float RealSum()                          

       {return Wage + Subsidy- Rent - CostOfElec - CostOfWater;};

};

enum Position // 定义职务类型

{     MANAGER,                                    

       ENGINEER,                                    

       EMPLOYEE,                                   

       WORKER                                               

};

class Date      

{     int day,month,year;

public:

       void init(int,int,int);

       void print_ymd();

};

class Employee

{     string             Department;          

       string             Name;                 

       Date        Birthdate;             

       Position EmpPosition;              

       Date        DateOfWork;        

       EmpSalary     Salary;                 

public:

       void Register(string Depart, string Name, Date tBirthdate,

              Position nPosition, Date tDateOfWork);

       void SetSalary(float wage, float subsidy, float rent, float elec,

              float water);

       float GetSalary();

       void ShowMessage();                         

};

void Date::init(int yy, int mm, int dd)

{     month = ( mm >= 1 && mm <= 12 ) ? mm : 1;

       year = ( yy >= 1900 && yy <= 2100 ) ? yy : 1900;

       day = ( dd >= 1 && dd <= 31 ) ? dd : 1;

}

void Date::print_ymd()

{     cout << year << "-" << month << "-" << day << endl;}

void Employee::Register(string Depart, string Name, Date tBirthdate,

                                          Position nPosition, Date tDateOfWork)

{     Department     = Depart;

       Name             = Name;

       Birthdate = tBirthdate;

       EmpPosition   = nPosition;

       DateOfWork   = tDateOfWork;

}

void Employee::SetSalary(float wage, float subsidy, float rent, float elec, float water)

{     Salary.Wage=wage;

       Salary.Subsidy=subsidy;

       Salary.Rent=rent;

       Salary.CostOfElec=elec;

       Salary.CostOfWater=water;

}

float Employee::GetSalary()

{     return Salary.RealSum();}

void Employee::ShowMessage()

{     cout << "Depart: " << Department << endl;

       cout << "Name: " << Name << endl;

       cout << "Birthdate: " ;

       Birthdate.print_ymd();

       switch(EmpPosition)

       {

       case MANAGER:

              cout << "Position: " << "MANAGER" <<endl;   break;

       case ENGINEER:

              cout << "Position: " << "ENGINEER" <<endl;   break;

       case EMPLOYEE:

              cout << "Position: " << "EMPLOYEE" <<endl;  break;

       case WORKER:

              cout << "Position: " << "WORKER" <<endl;      break;

       }

       cout << "Date of Work: " ;

       DateOfWork.print_ymd();

       cout << "Salary: " << GetSalary() <<endl;

       cout<<"----------------------------------"<<endl;

}

#define MAX_EMPLOYEE    1000

int main()

{     Employee EmployeeList[MAX_EMPLOYEE];   // 定义职工档案数组

       int EmpCount=0;

       Date birthdate,workdate;

       //输入第一个职工数据

       birthdate.init(1980,5,3);

       workdate.init(1999,7,20);

       EmployeeList[EmpCount].Register("销售处",

              "张弓长",birthdate,ENGINEER,workdate);

       EmployeeList[EmpCount].SetSalary(1000,200,100,50,20);

       EmpCount++;

              birthdate.init(1979,4,8);

       workdate.init(2002,3,1);

       EmployeeList[EmpCount].Register("项目部",

              "李木子",birthdate,MANAGER,workdate);

       EmployeeList[EmpCount].SetSalary(1500,200,150,50,20);

       EmpCount++;

       for(int i=0;i<EmpCount;i++)

              EmployeeList[i].ShowMessage();

       return 0;

}

 

答案:

#include <iostream>

#include <string>

using namespace std;

class EmpSalary   //定义一个EmpSalary 类

{public:   // 公共的部分(相当于接口)

float Wage;         //单精度   Wage     

float Subsidy;       //同上        

float Rent;   //同上        

float CostOfElec;     //同上     

float CostOfWater;      //同上  

public:    
 
 float RealSum()      //定义一个单精度 的函数 RealSum()                   
  
 {return Wage + Subsidy- Rent - CostOfElec - CostOfWater;};// 返回一个计算后的值给RealSum()函数
 
};

enum Position // 定义职务类型

{     MANAGER,                                    

ENGINEER,                                    

EMPLOYEE,                                   

WORKER                                               

};

class Date       //定义一个Date类

{     int day,month,year;  // 年月日

public:       
 
 void init(int,int,int);  // 定义一个 无返回值 可以传递3个参数 都是int 型
 
 void print_ymd();      //定一个 print_ymd()函数  应该是用来输出年月日的
 
};
  
class Employee           // Employee   类

{     string             Department;    //string(字符串类型)        

string             Name;            //同上     

Date        Birthdate;              //定义一个 Date 对象 Birthdate

Position EmpPosition;               //同上

Date        DateOfWork;             //同上

EmpSalary     Salary;                //同上  一共定义了  4个对象了  

public:                         
 
 void Register(string Depart, string Name, Date tBirthdate,
  
  Position nPosition, Date tDateOfWork);   // 将众多参数传递进去
 
 void SetSalary(float wage, float subsidy, float rent, float elec,
  
  float water);      //同上
 
 float GetSalary();      // 调用这个函数
 
 void ShowMessage();                      //同上   
 
};

void Date::init(int yy, int mm, int dd)  //  init的作用域在Date里面 且 传递进去3个参数

{     month = ( mm >= 1 && mm <= 12 ) ? mm : 1;

year = ( yy >= 1900 && yy <= 2100 ) ? yy : 1900;

day = ( dd >= 1 && dd <= 31 ) ? dd : 1;        // 一些简单的算法

}

void Date::print_ymd()                //printf_ymd 的作用域在 Date里面

{     cout << year << "-" << month << "-" << day << endl;}  // 输出 年月日

void Employee::Register(string Depart, string Name, Date tBirthdate,
      
      Position nPosition, Date tDateOfWork)  // 同上 不过传递了 5个参数
      
{     Department     = Depart;    

Name             = Name;

Birthdate = tBirthdate;

EmpPosition   = nPosition;

DateOfWork   = tDateOfWork;      // 将5个参数 的值赋值给 前面定义的5个参数

}

void Employee::SetSalary(float wage, float subsidy, float rent, float elec, float water)

{     Salary.Wage=wage;   // 前面定义了EmpSalary  Salary 故Salary.Wage = wage 也是一个赋值

      // 对象.方法  的一些基本用法
Salary.Subsidy=subsidy; 

Salary.Rent=rent;

Salary.CostOfElec=elec;

Salary.CostOfWater=water;// 以上全部同理

}

float Employee::GetSalary() 

{     return Salary.RealSum();}

void Employee::ShowMessage()

{     cout << "Depart: " << Department << endl;

cout << "Name: " << Name << endl;

cout << "Birthdate: " ;

Birthdate.print_ymd();    // 以上为一些输出 前面解释过了

switch(EmpPosition)  // switch case 语句的用法 根据名字去判断 进行那一项输出

{
 
case MANAGER:
 
 cout << "Position: " << "MANAGER" <<endl;   break;
 
case ENGINEER:
 
 cout << "Position: " << "ENGINEER" <<endl;   break;
 
case EMPLOYEE:
 
 cout << "Position: " << "EMPLOYEE" <<endl;  break;
 
case WORKER:
 
 cout << "Position: " << "WORKER" <<endl;      break;
 
}

cout << "Date of Work: " ; // 输出Date of Work

DateOfWork.print_ymd();   // DateOfWork(对象)调用print_ymd()的输出方法

cout << "Salary: " << GetSalary() <<endl;

cout<<"----------------------------------"<<endl;

}

#define MAX_EMPLOYEE    1000     //  宏定义

int main()                      //主函数 从main开始执行

{     Employee EmployeeList[MAX_EMPLOYEE];   // 定义职工档案数组

上一个:求C++学习资料啊!
下一个:关于C++学习的问题

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,