Online Book Reader

Home Category

Professional C__ - Marc Gregoire [18]

By Root 1269 0
{

cout << *iterator << endl;

}

// Print the elements again using C++11 range-based for loop

for (auto& str : myVector)

cout << str << endl;

return 0;

}

Code snippet from vectortest\vectortest.cpp

As you can see, myVector is declared as vector. The angled brackets are required to specify the template parameters. A vector is a generic container. It can contain almost any kind of object; but, you have to specify the type of object you want in your vector between those brackets. Templates are discussed in more detail in Chapters 19 and 20.

To add elements to the vector, you can use the push_back() method and the C++11 uniform initialization.

The preceding example uses a new C++11 feature called auto type deduction using the auto keyword to let the compiler automatically decide the type of the variable iterator. If your compiler does not yet understand this new feature, you can write the for loop as follows:

for (vector::const_iterator iterator = myVector.begin();

iterator != myVector.end(); ++iterator) {

cout << *iterator << endl;

}

YOUR FIRST USEFUL C++ PROGRAM


The following program builds on the employee database example used earlier when discussing structs. This time, you will end up with a fully functional C++ program that uses many of the features discussed in this chapter. This real-world example includes the use of classes, exceptions, streams, vectors, iterators, namespaces, references, and other language features.

An Employee Records System

A program to manage a company’s employee records needs to be flexible and have useful features. The feature set for this program includes the following.

The ability to add an employee

The ability to fire an employee

The ability to promote an employee

The ability to view all employees, past and present

The ability to view all current employees

The ability to view all former employees

The design for this program divides the code into three parts. The Employee class encapsulates the information describing a single employee. The Database class manages all the employees of the company. A separate UserInterface file provides the interactivity of the program.

The Employee Class

The Employee class maintains all the information about an employee. Its methods provide a way to query and change that information. Employees also know how to display themselves on the console. Methods also exist to adjust the employee’s salary and employment status.

Employee.h

The Employee.h file declares the behavior of the Employee class. The sections of this file are described individually in the material that follows.

The first few lines of the file include a comment indicating the name of the file and the inclusion of the string functionality.

This code also declares that the subsequent code, contained within the curly braces, will live in the Records namespace. Records is the namespace that is used throughout this program for application-specific code.

// Employee.h

#include

namespace Records {

Code snippet from EmployeeDB\Employee.h

The following constant, representing the default starting salary for new employees, lives in the Records namespace. Other code that lives in Records can access this constant as kDefaultStartingSalary. Elsewhere, it must be referenced as Records::kDefaultStartingSalary.

const int kDefaultStartingSalary = 30000;

Code snippet from EmployeeDB\Employee.h

The Employee class is declared, along with its public methods. The promote() and demote() methods both have integer parameters that are specified with a default value. In this way, other code can omit the integer parameters and the default will automatically be used.

A number of accessors provide mechanisms to change the information about an employee or query the current information about an employee.

class Employee

{

public:

Employee();

void promote(int inRaiseAmount = 1000);

void demote(int inDemeritAmount = 1000);

void hire(); // Hires or rehires the employee

void fire(); // Dismisses the employee

void display() const;// Outputs employee info

Return Main Page Previous Page Next Page

®Online Book Reader