1. Home
  2. Computing & Technology
  3. C / C++ / C#

C++ Tutorial - Learn to use References

By , About.com Guide

4 of 7

Understanding Example 8_3

If you've looked at the C Star Trek game source, you'll recognise the random() and randomize() functions. The time.h header provides access to the time() function which seeds the random number generator. The random() function generates a number betwen 1 and the parameter max . It's used to generate a random salary. The struct payrolltype holds an employee record. This consists of an id, salary, whether employed or not and their name. There are two constructors- one where the struct can be initialized through it's constructor arguments and a default constructor. Both use Initializer lists to initialize the member variables.

The variable payroll is a 100 element array. This declaration also calls the default constructor so that by the time main() executes, this array has been fully initialized. In main(), it is populated with valid ids, random salaries and names such as "Person 34".

Note that highest is also defined an a reference. This refers to the variable returned from findhighestpaid(), a function that returns a reference to whichever employee has the highest salary. If this was not guaranteed to find one employee, it would be a very bad piece of code. It would be better returning an integer index, with an error value of -1 (for not found) if there was no highest salary but it just illustrates the point.

On the next page : More on example 8_3

Explore C / C++ / C#
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C++
  5. Learn C++ Programming
  6. Explaining Example 8_3

©2009 About.com, a part of The New York Times Company.

All rights reserved.