Online Book Reader

Home Category

Professional C__ - Marc Gregoire [19]

By Root 1271 0
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;

Code snippet from EmployeeDB\Employee.h

Finally, the data members are declared as protected so that other parts of the code cannot modify them directly. The accessors provide the only public way of modifying or querying these values.

protected:

std::string mFirstName;

std::string mLastName;

int mEmployeeNumber;

int mSalary;

bool bHired;

};

}

Code snippet from EmployeeDB\Employee.h

Employee.cpp

This section discusses the implementations for the Employee class methods. The Employee constructor sets the initial values for the Employee’s data members. By default, new employees have no name, an employee number of -1, the default starting salary, and a status of not hired.

#include

#include "Employee.h"

using namespace std;

namespace Records {

Employee::Employee()

: mFirstName("")

, mLastName("")

, mEmployeeNumber(-1)

, mSalary(kDefaultStartingSalary)

, bHired(false)

{

}

Code snippet from EmployeeDB\Employee.cpp

The promote() and demote() methods simply call the setSalary() method with a new value. Note that the default values for the integer parameters do not appear in the source file. They only need to exist in the header.

void Employee::promote(int inRaiseAmount)

{

setSalary(getSalary() + inRaiseAmount);

}

void Employee::demote(int inDemeritAmount)

{

setSalary(getSalary() - inDemeritAmount);

}

Code snippet from EmployeeDB\Employee.cpp

The hire() and fire() methods just set the bHired data member appropriately.

void Employee::hire()

{

bHired = true;

}

void Employee::fire()

{

bHired = false;

}

Code snippet from EmployeeDB\Employee.cpp

The display() method uses the console output stream to display information about the current employee. Because this code is part of the Employee class, it could access data members, such as mSalary, directly instead of using the getSalary() accessor. However, it is considered good style to make use of accessors when they exist, even within the class.

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;

}

Code snippet from EmployeeDB\Employee.cpp

A number of accessors and setters perform the task of getting and setting values. Even though these methods seem trivial, it’s better to have trivial accessors and setters than to make your data members public. In the future, you may want to perform bounds checking in the setSalary() method, for example.

// Accessors and setters

void Employee::setFirstName(string inFirstName)

{

mFirstName = inFirstName;

}

string Employee::getFirstName() const

{

return mFirstName;

}

// ... other accessors and setters omitted for brevity

}

Code snippet from EmployeeDB\Employee.cpp

EmployeeTest.cpp

As you write individual classes, it is often useful to test them in isolation. The following code includes a main() function that performs some simple operations using the Employee class. Once you are confident that the Employee class works, you should remove or comment-out this file so that you don’t attempt to compile your code with multiple main() functions.

#include

#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;

}

Code snippet from EmployeeDB\EmployeeTest.cpp

The

Return Main Page Previous Page Next Page

®Online Book Reader