码迷,mamicode.com
首页 > 编程语言 > 详细

第一个有用的C++程序

时间:2015-04-04 18:32:25      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:c++   封装   

这是一个雇员记录系统。
这个程序包含的功能有:
(1)添加雇员的功能
(2)解雇雇员的功能
(3)雇员晋升的功能
(4)查看所有雇员的功能,包括过去以及现在的雇员
(5)添加所有当前雇员的功能
(6)添加所有以前雇员的功能

程序分为三个部分:Employee类封装了单个雇员的信息,Database类管理公司的所有雇员,一个单独的UserInterface文件提供程序的接口。

来看代码:
Employee.h:

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string>
namespace Records {
    const int kDefaultStartingSalary = 30000;

    class Employee
    {
    public:
        Employee();

        void promote(int inRaiseAmount = 1000);
        void demote(int DemeritAmout = 1000);

        void hire();//hire or rehire the employee
        void fire();
        void display() const;//output employee info to console

        //Accessors and setters
        void setFirstName(std::string inFirstName);
        std::string getFirstName() const;
        void setLastName(std::string inLastName);
        std::string getLastName() const;

        void setEmployeeNumber(int inEmployeeNumber);
        int getEmployeeNumber() const;

        void setSalary(int inNewSalary);
        int getSalary() const;

        bool getIsHired() const;

    protected:
        std::string mFirstName;
        std::string mLastName;
        int mEmployeeNumber;
        int mSalary;
        bool bHired;
    };
}

#endif

Employee.cpp(雇员实现类):

#include <iostream>
#include "Employee.h"

using namespace std;

namespace Records{
    Employee::Employee():mFirstName("")
                        ,mLastName("")
                        ,mEmployeeNumber(-1)
                        ,mSalary(kDefaultStartingSalary)
    {

    }

    void Employee::promote(int inRaiseAmount)
    {
        setSalary(getSalary() + inRaiseAmount);
    }

    void Employee::demote(int inDemeritAmount)
    {
        setSalary(getSalary() - inDemeritAmount);
    }

    void Employee::hire()
    {
        bHired = true;
    }

    void Employee::fire()
    {
        bHired = false;
    }

    void Employee::display() const
    {
        cout<<"Employee: "<<getLastName()<<", "<<getFirstName()<<endl;
        cout<<"................................."<<endl;
        cout<<(bHired ? "Current Employee":"Former Employee")<<endl;
        cout<<"Employee Number: "<<getEmployeeNumber()<<endl;
        cout<<"Salary: $"<<getSalary()<<endl;
        cout<<endl;
    }

    void Employee::setFirstName(string inFirstName)
    {
        mFirstName = inFirstName;
    }

    string Employee::getFirstName() const
    {
        return mFirstName;
    }

    void Employee::setLastName(std::string inLastName)
    {
        mLastName = inLastName;
    }

    std::string Employee::getLastName() const
    {
        return mLastName;
    }

    void Employee::setSalary(int inNewSalary)
    {
        mSalary = inNewSalary;
    }

    int Employee::getSalary() const
    {
        return mSalary;
    }

    void Employee::setEmployeeNumber(int inEmployeeNumber)
    {
        mEmployeeNumber = inEmployeeNumber;
    }

    int Employee::getEmployeeNumber() const
    {
        return mEmployeeNumber;
    }

    bool Employee::getIsHired() const
    {
        return bHired;
    }

}

到这短暂测试一下,来个测试代码:
EmployeeTest.cpp:

#include <iostream>
#include "Employee.h"

using namespace std;
using namespace Records;

int main()
{
    cout<<"Testing the Employee class"<<endl;
    Employee emp;
    emp.setFirstName("Marni");
    emp.setLastName("Kleper");
    emp.setEmployeeNumber(71);
    emp.setSalary(50000);
    emp.promote();
    emp.promote(50);
    emp.hire();
    emp.display();
    return 0;
}

测试一下:(注意这里是g++)
g++ -o a Employee.cpp EmployeeTest.cpp

结果:
Testing the Employee class
Employee: Kleper, Marni
………………………
Current Employee
Employee Number: 71
Salary: $51050

测试成功。

再看Database.h:

//数据库会自动新雇员一个雇员号,因此定义了了一个常量作为编号的开始

#include <iostream>
#include <vector>
#include "Employee.h"

namespace Records{
    const int kFirstEmployeeNumber = 1000;

    class Database
    {
    public:
        Database();
        ~Database();

        Employee& addEmployee(std::string inFirstName,std::string inLastName);
        Employee& getEmployee(int inEmployeeNumber);
        Employee& getEmployee(std::string inFirstName,std::string inLastName);

        void displayAll() const;
        void displayCurrent() const;
        void displayFormer() const;

    protected:
        std::vector<Employee> mEmployees;
        int mNextEmployeeNumber;
    };
}

Database.cpp:

#include <iostream>
#include <stdexcept>
#include "Database.h"

using namespace std;

namespace Records{
    Database::Database()
    {
        mNextEmployeeNumber = kFirstEmployeeNumber;
    }

    Database::~Database()
    {
    }

    Employee& Database::addEmployee(std::string inFirstName,std::string inLastName)
    {
        Employee theEmployee;
        theEmployee.setFirstName(inFirstName);
        theEmployee.setLastName(inLastName);
        theEmployee.setEmployeeNumber(mNextEmployeeNumber++);
        theEmployee.hire();

        mEmployees.push_back(theEmployee);
        return mEmployees[mEmployees.size()-1];
    }

    Employee& Database::getEmployee(int inEmployeeNumber)
    {
    //如果不支持auto 用vector<Employee>::iterator代替auto
        //for(auto iter = mEmployee.begin();iter != mEmployee.end();++iter)
        for(vector<Employee>::iterator iter = mEmployees.begin();iter != mEmployees.end();++iter)
        {
            if(iter->getEmployeeNumber() == inEmployeeNumber)
                return *iter;
        }
        cerr<<"No Employee with number "<< inEmployeeNumber <<endl;
        throw exception();
    }

    Employee& Database::getEmployee(std::string inFirstName,std::string inLastName)
    {
        for(vector<Employee>::iterator iter = mEmployees.begin();iter != mEmployees.end();++iter)
        {
            if(iter->getFirstName() == inFirstName && iter->getLastName() == inLastName)
                return *iter;
        }
        cerr<<"No Employee with firstname named"<< inFirstName <<"and lastname named"<<inLastName<<endl;
        throw exception();
    }

    void Database::displayAll() const
    {
        //如果不支持auto 用vector<Employee>::const_iterator代替auto
        //                              (原因在于前者是const成员函数)
        for(vector<Employee>::const_iterator iter = mEmployees.begin();iter != mEmployees.end();++iter)
        {
            iter->display();
        }
    }
    void Database::displayCurrent() const
    {
        for(vector<Employee>::const_iterator iter = mEmployees.begin();iter != mEmployees.end();++iter)
        {
            if(iter->getIsHired())
                iter->display();
        }
    }
    void Database::displayFormer() const
    {
        for(vector<Employee>::const_iterator iter = mEmployees.begin();iter != mEmployees.end();++iter)
        {
            if(!iter->getIsHired())
                iter->display();
        }
    }
}

同样的道理,测试一下,
DatabaseTest.cpp:

#include <iostream>
#include "Database.h"

using namespace std;
using namespace Records;

int main()
{
    Database myDB;
    Employee& emp1 = myDB.addEmployee("Grep","Wallis");
    emp1.fire();
    Employee& emp2 = myDB.addEmployee("Scott","Kleper");
    emp2.setSalary(100000);
    Employee& emp3 = myDB.addEmployee("Nick","Solter");
    emp3.setSalary(100000);
    emp2.promote();

    cout<<"all employees: "<<endl;
    cout<<endl;
    myDB.displayAll();
    cout<<endl;

    cout<<"current employees: "<<endl;
    cout<<endl;
    myDB.displayCurrent();
    cout<<endl;

    cout<<"former employees: "<<endl;
    cout<<endl;
    myDB.displayFormer();
    cout<<endl;

    return 0;
}

测试一下:
g++ DatabaseTest.cpp Database.cpp Employee.cpp

结果:
all employees:

Employee: Wallis, Grep
……………………………
Former Employee
Employee Number: 1000
Salary: $30000

Employee: Kleper, Scott
……………………………
Current Employee
Employee Number: 1001
Salary: $100000

Employee: Solter, Nick
……………………………
Current Employee
Employee Number: 1002
Salary: $100000

current employees:

Employee: Kleper, Scott
……………………………
Current Employee
Employee Number: 1001
Salary: $100000

Employee: Solter, Nick
……………………………
Current Employee
Employee Number: 1002
Salary: $100000

former employees:

Employee: Wallis, Grep
……………………………
Former Employee
Employee Number: 1000
Salary: $30000

测试成功。

最后的界面类,
UserInterface.cpp:

#include<iostream>
#include<stdexcept>
#include "Database.h"

using namespace std;
using namespace Records;

int displayMenu();
void doHire(Database& inDB);
void doFire(Database& inDB);
void doPromote(Database& inDB);
void doDemote(Database& inDB);

int main()
{
    Database employeeDB;
    bool done = false;
    while(!done)
    {
        int selection = displayMenu();
        switch(selection)
        {
            case 1:
                doHire(employeeDB);
                break;
            case 2:
                doFire(employeeDB);
                break;
            case 3:
                doPromote(employeeDB);
                break;
            case 4:
                employeeDB.displayAll();
                break;
            case 5:
                employeeDB.displayCurrent();
                break;
            case 6:
                employeeDB.displayFormer();
                break;
            case 0:
                done = true;
                break;
            default:
                cerr<<"Unknown command."<<endl;
        }
    }

    return 0;
}


int displayMenu()
{
    int selection;
    cout<<endl;
    cout<<"Employee Database"<<endl;
    cout<<"................."<<endl;
    cout<<"(1) Hire a new employee"<<endl;
    cout<<"(2) Fire a new employee"<<endl;
    cout<<"(3) Promote a new employee"<<endl;
    cout<<"(4) List all employees"<<endl;
    cout<<"(5) List all current employee"<<endl;
    cout<<"(6) Hire all previous employee"<<endl;
    cout<<"(0) Quit "<<endl;
    cout<<endl;
    cout<<".....>";
    cin>>selection;
    return selection;
}

void doHire(Database& inDB)
{
    string firstName;
    string lastName;
    cout<<"First Name? ";
    cin>>firstName;
    cout<<"Last Name? ";
    cin>>lastName;

    try
    {
        inDB.addEmployee(firstName,lastName);
    }
    catch(const std::exception& )
    {
        cerr<<"Unable to add new emploee!"<<endl;
    }
}

void doFire(Database& inDB)
{
    int employeeNumber;
    cout<<"Employee nuumber:";
    cin>>employeeNumber;

    try
    {
        Employee& emp = inDB.getEmployee(employeeNumber);
        emp.fire();
        cout<<"Employee "<<employeeNumber<<" terminated."<<endl;
    }
    catch(const std::exception& )
    {
        cerr<<"Unable to terminate employee!"<<endl;
    }
}

void doPromote(Database& inDB)
{
    int employeeNumber;
    int raiseAmount;
    cout<<"Employee nuumber?";
    cin>>employeeNumber;

    cout<<"How much of a raise?";
    cin>>raiseAmount;

    try
    {
        Employee& emp = inDB.getEmployee(employeeNumber);
        emp.promote(raiseAmount);
    }
    catch(const std::exception& )
    {
        cerr<<"Unable to promote employee!"<<endl;
    }
}

最终测试一下:
g++ UserInterface.cpp Database.cpp Employee.cpp

结果:
Employee Database
……………..
(1) Hire a new employee
(2) Fire a new employee
(3) Promote a new employee
(4) List all employees
(5) List all current employee
(6) Hire all previous employee
(0) Quit

…..>

这里没有将最后的测试展示出来,大家可以自己做成文件测试一下。

总的来说,整个程序逻辑较为简单,较为完全的展示了面向对象的概念。不算太难,只要有点基础,应该就能理解。不过,整个代码还是较多的,需要有点耐心。

第一个有用的C++程序

标签:c++   封装   

原文地址:http://blog.csdn.net/tomjohnson/article/details/44873831

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!