main.cpp

/*

*@Author:   STZG

*@Language: C++

*/

#include<iostream>

#include "worker.h"

#include "salary.h"

using namespace std;

int main()

{

    char name[100],dept[100];

    int age;

    double wage,subsidy,rent,waterfee,elecfee;



    cout<<"请输入名字:"<<endl;

    cin>>name;

    cout<<"请输入年龄:"<<endl;

    cin>>age;

    cout<<"请输入部门:"<<endl;

    cin>>dept;

    cout<<"请输入基本工资:"<<endl;

    cin>>wage;

    cout<<"请输入岗位津贴:"<<endl;

    cin>>subsidy;

    cout<<"请输入房租:"<<endl;

    cin>>rent;

    cout<<"请输入水费:"<<endl;

    cin>>waterfee;

    cout<<"请输入电费:"<<endl;

    cin>>elecfee;

    salary Sa(wage,subsidy,rent,waterfee,elecfee);

    worker Worker(name,age,dept,Sa);



    cout<<"名字:"<<endl;

    cout<<Worker.getname()<<endl;

    cout<<"年龄:"<<endl;

    cout<<Worker.getage()<<endl;

    cout<<"部门:"<<endl;

    cout<<Worker.getdept()<<endl;

    cout<<"基本工资:"<<endl;

    cout<<Worker.getsalary().getWag()<<endl;

    cout<<"岗位津贴:"<<endl;

    cout<<Worker.getsalary().getSubsidy()<<endl;

    cout<<"房租:"<<endl;

    cout<<Worker.getsalary().getRent()<<endl;

    cout<<"水费:"<<endl;

    cout<<Worker.getsalary().getWaterfee()<<endl;

    cout<<"电费:"<<endl;

    cout<<Worker.getsalary().getElecfee()<<endl;

    cout<<"工人数:"<<endl;

    cout<<Worker.getnum()<<endl;

    cout<<"实发工资:"<<endl;

    cout<<Worker.getsalary().RealSalary()<<endl;

    return 0;

}

worker.cpp

#include "worker.h"



worker::worker()

{

    num++;

    //ctor

}



worker::~worker()

{

    //dtor

}

int worker::num=0;

worker::worker(char*Name,int Age,char*Dept,salary Sa):age(Age){

    strcpy(name,Name);

    strcpy(dept,Dept);

    Salary=Sa;

    num++;

}

void worker::setname(char *Name){

    strcpy(name,Name);

}

void worker::setage(int Age){

    age=Age;

}

void worker::setdept(char *Dept){

    strcpy(dept,Dept);

}

void worker::setsalary(salary Sa){

    Salary=Sa;

}

char* worker::getname(){

    return name;

}

int worker::getage(){

    return age;

}

char* worker::getdept(){

    return dept;

}

salary worker::getsalary(){

    return Salary;

}

int worker::getnum(){

    return num;

}

worker.h

#ifndef WORKER_H

#define WORKER_H

#include <iostream>

#include "salary.h"

#include <string>

#include <string.h>

class worker

{

    public:

        worker();

        virtual ~worker();

        worker(char*Name,int Age,char*Dept,salary Sa);

        void setname(char *Name);

        void setage(int Age);

        void setdept(char *Dept);

        void setsalary(salary Sa);

        char* getname();

        int getage();

        char* getdept();

        salary getsalary();

        int getnum();

    protected:



    private:

        char name[100];

        int age;

        char dept[100];

        salary Salary;

        static int num;

};



#endif // WORKER_H

salary.cpp

#include "salary.h"



salary::salary()

{

    Wag=Subsidy=Rent=Waterfee=Elecfee=0;//ctor

}



salary::~salary()

{

    //dtor

}

salary::salary(double wag,double subsidy,double rent,double waterfee,double elecfee):Wag(wag),Subsidy(subsidy),Rent(rent),Waterfee(waterfee),Elecfee(elecfee){



}

void salary::setWag(double wag){

    Wag=wag;

}

void salary::setSubsidy(double subsidy){

    Subsidy=subsidy;

}

void salary::setRent(double rent){

    Rent=rent;

}

void salary::setWaterfee(double waterfee){

    Waterfee=waterfee;

}

void salary::setElecfee(double elecfee){

    Elecfee=elecfee;

}

double salary::getWag(){

    return Wag;

}

double salary::getSubsidy(){

    return Subsidy;

}

double salary::getRent(){

    return Rent;

}

double salary::getWaterfee(){

    return Waterfee;

}

double salary::getElecfee(){

    return Elecfee;

}

double salary::RealSalary(){

    return Wag+Subsidy-Rent-Waterfee-Elecfee;

}

salary& salary::operator=(const salary &S){

    if(this==&S){

        return *this;

    }

    Wag=S.Wag;

    Subsidy=S.Subsidy;

    Rent=S.Rent;

    Waterfee=S.Waterfee;

    Elecfee=S.Elecfee;

    return *this;



}

salary.h

#ifndef SALARY_H

#define SALARY_H





class salary

{

    public:

        salary();

        salary(double wag,double subsidy,double rent,double waterfee,double elecfee);

        virtual ~salary();

        void setWag(double wag);

        void setSubsidy(double subsidy);

        void setRent(double rent);

        void setWaterfee(double waterfee);

        void setElecfee(double elecfee);

        double getWag();

        double getSubsidy();

        double getRent();

        double getWaterfee();

        double getElecfee();

        double RealSalary();

        salary& operator=(const salary &S);

    protected:



    private:

        double Wag,Subsidy,Rent,Waterfee,Elecfee;

};



#endif // SALARY_H

输入数据:

stzg

18

zstu

5000

2000

800

200

100

运行结果: