Object Oriented Programming Using C
Object Oriented Programming Using C
10987654321
06 05 04 03 02 01 00 99 98 97
Contents
Preface ix
1 Programming Concepts 1
1.1 Computer programs 1
1.2 The way a program uses the computer's memory 2
1.3 Input and output of data 4
1.4 Computer program development 6
1.5 The user requirements specification 6
1.6 The design stages 7
1.7 Detailed design using pseudo-code 7
1.8 The programming stage 8
1.9 Compiling, running and testing a program 8
1. 10 Documenting and maintaining pro grams 9
1.11 Writing high-quality computer programs 10
1.12 Exercises 10
3 A First Program 21
3.1 The model for our first program 21
3.2 The C++ code for our first program 22
3.3 Coding style 26
3.4 Pseudo-code for a sequence 27
3.5 Exercises 28
v
vi Contents
4 An Introduction to Inheritance 29
4.1 Using a header file 29
4.2 Inheriting code from our first program into a second
program 31
4.3 Exercises 34
5 Arithmetic 36
5.1 Integers 36
5.2 Assignment statements and integer arithmetic 37
5.3 Real arithmetic 38
5.4 A model for a student's assessment in a subject 38
5.5 Using integer and real numbers, the ini tialise
function 42
5.6 The display functions 44
5.7 Exercises 46
6 An Introduction to Selection 49
6.1 Further development of a model for a student's
assessment in a subject 49
6.2 The i f statement 49
6.3 The if-else statement 52
6.4 Relational operators 53
6.5 Nested selections 54
6.6 Compound statements 56
6.7 Exercises 57
8 Repetition 74
8.1 Refining the input processes for the student's marks 74
8.2 The whi le statement 77
8.3 The for statement 80
8.4 The do-while statement 82
8.5 Exercises 85
9 Functions 88
9.1 The function call 88
9.2 The function definition 89
9.3 A revised version of the student marks object class 90
9.4 Call by value parameters 91
9.5 Function results 93
9.6 Call by reference parameters 96
9.7 When to use call by value and call by reference 101
9.8 Exercises 101
Index 249
Preface
ix
x Preface
Chapter I covers basic concepts in order that the reader can appreciate what
a computer program is. The development of a program is then put in context by
describing briefly, in general terms, the various stages involved. Chapter 2
introduces the basic object oriented terminology and illustrates how object classes
are used to model a very simple system.
Having introduced a C++ program in chapter 3, the important concept of
inheritance and the use of a header file are covered in chapter 4.
Chapters 5 to 8 cover, within an object oriented context, facilities found in
most procedural languages, such as basic data types, arithmetic and control
constructs for selection and repetition. The need for, and the difference between,
the constructs for both selection and repetition are emphasised.
Chapter 9 concentrates on the use of programmer-defined functions; con-
cepts are introduced by referring to functions already used including those found
in standard C++ libraries. The next chapter on the use of constructors and
destructors is a natural development.
The need for arrays and their use is described in chapters 11 to 13. The
concept of an array of objects enables the difference between inheritance and
aggregation to be explained.
Scope rules and object lifetime are emphasised in chapter 14 before introduc-
ing the concept of pointers for dynamic variables and objects. Then chapter 15
covers the use of file streams in general and how they may be used to facilitate
object persistence.
Finally, in chapter 16, we introduce some of the fundamental concepts of
polymorphism and use appropriate C++ language constructs in straightforward
illustrative examples.
Acknowledgement
The authors are grateful to Neil Hepworth of South Bank University for his
helpful and constructive comments made while reviewing this book.