Object Oriented Programming in C 4th Edition1
Object Oriented Programming in C 4th Edition1
Fourth Edition
Robert Lafore
EXECUTIVE EDITOR
Michael Stephens
Christina Smith
INDEXER
Rebecca Salerno
PROOFREADER
04
03 02
01
3 2 1
Trademarks
All terms mentioned in this book that are known to be trademarks or service
marks have been appropriately capitalized. Sams Publishing cannot attest to
the accuracy of this information. Use of a term in this book should not be
regarded as affecting the validity of any trademark or service mark.
ACQUISITIONS EDITOR
Michael Stephens
MANAGING EDITOR
Matt Purcell
PROJECT EDITORS
Angela Boley
Matt Wynalda
TECHNICAL EDITOR
Mark Cashman
TEAM COORDINATOR
Pamalee Nelson
MEDIA DEVELOPER
Dan Scherf
Every effort has been made to make this book as complete and as accurate as
possible, but no warranty or fitness is implied. The information provided is on
an as is basis. The author and the publisher shall have neither liability nor
responsibility to any person or entity with respect to any loss or damages
arising from the information contained in this book.
INTERIOR DESIGNER
Gary Adair
COVER DESIGNER
Alan Clements
PAGE LAYOUT
Ayanna Lacey
Overview
Introduction 1
1
Structures 131
Functions 161
Inheritance 371
10
Pointers 429
11
12
13
14
15
16
Bibliography
Index 981
977
Contents
Introduction
vi
OBJECT-ORIENTED PROGRAMMING
IN
Structures 131
Structures ............................................................................................132
A Simple Structure ........................................................................132
Defining the Structure ....................................................................133
Defining a Structure Variable ........................................................134
Accessing Structure Members........................................................136
Other Structure Features ................................................................137
A Measurement Example ..............................................................139
Structures Within Structures ..........................................................141
A Card Game Example ..................................................................145
Structures and Classes ....................................................................148
Enumerations ......................................................................................148
Days of the Week............................................................................148
One Thing or Another ....................................................................151
CONTENTS
Organizing the Cards......................................................................153
Specifying Integer Values ..............................................................155
Not Perfect......................................................................................155
Other Examples ..............................................................................155
Summary ..............................................................................................156
Questions..............................................................................................156
Exercises ..............................................................................................158
5
Functions 161
Simple Functions ................................................................................162
The Function Declaration ..............................................................164
Calling the Function ......................................................................164
The Function Definition ................................................................164
Comparison with Library Functions ..............................................166
Eliminating the Declaration............................................................166
Passing Arguments to Functions..........................................................167
Passing Constants ..........................................................................167
Passing Variables ............................................................................169
Passing by Value ............................................................................170
Structures as Arguments ................................................................171
Names in the Declaration ..............................................................176
Returning Values from Functions ........................................................176
The return Statement ....................................................................177
Returning Structure Variables ........................................................180
Reference Arguments ..........................................................................182
Passing Simple Data Types by Reference ......................................182
A More Complex Pass by Reference ............................................185
Passing Structures by Reference ....................................................186
Notes on Passing by Reference ......................................................188
Overloaded Functions ..........................................................................188
Different Numbers of Arguments ..................................................189
Different Kinds of Arguments ........................................................191
Recursion ............................................................................................193
Inline Functions ..................................................................................195
Default Arguments ..............................................................................197
Scope and Storage Class......................................................................199
Local Variables ..............................................................................199
Global Variables..............................................................................202
Static Local Variables ....................................................................204
Storage ............................................................................................205
Returning by Reference ......................................................................206
Function Calls on the Left of the Equal Sign ................................207
Dont Worry Yet..............................................................................207
vii
viii
OBJECT-ORIENTED PROGRAMMING
IN
CONTENTS
7
ix