Programming with C++ (Zambak)

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Author(s): Osman AY, Muhammed Akif HORASANLI
Edition: 1
Publisher: Zambak Publishing
Year: 2006

Language: English
Pages: 152

1. Introduction To Programming
Understanding the Programming . . . . . . . . . . . . . 6
The First C++ Program . . . . . . . . . . . . . . . . . . . . 6
Breaking a Text into Multiple Lines . . . . . . . . . . . . 7
Basic Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Getting Data from the User (Input) . . . . . . . . . . . . 8
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . 9
Precedence of Arithmetic Operators
Fundamental C++ Variables . . . . . . . . . . . . . . . . 11
ASCII Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Reading and Printing Strings . . . . . . . . . . . . . . . . 15
Initialization of Variables . . . . . . . . . . . . . . . . . . . . 16
Using Text Files as Input and Output. . . . . . . . . . 16
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 19
Programming Problems . . . . . . . . . . . . . . . . . . . . 20
Flowchart Programming . . . . . . . . . . . . . . . . . . . . 20
Understanding Flowchart Programming . . . . 21
Flowchart Symbols . . . . . . . . . . . . . . . . . . . . . 22
2. Decision Structures
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
The if structure . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
The if/else structure . . . . . . . . . . . . . . . . . . . . . . . 25
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . 27
The Conditional Operator (?) . . . . . . . . . . . . . . . . 28
The Switch Structure . . . . . . . . . . . . . . . . . . . . . . 29
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 32
Programming Problems . . . . . . . . . . . . . . . . . . . . 34
Flowchart Programming . . . . . . . . . . . . . . . . . . . . 35
Making a Decision . . . . . . . . . . . . . . . . . . . . . 35
3. Repetition Structures
Repetition Structures (Loops) . . . . . . . . . . . . . . . 38
The "while" Loop . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Increment and Decrement Operators . . . . . . . . . 40
Counter-Controlled and Sentinel-Controlled
Repetitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
The "do/while" Loop . . . . . . . . . . . . . . . . . . . . . . . 45
The "for" Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Syntax of “for” statement . . . . . . . . . . . . . . . . 47
The "break" and "continue" Statements . . . . . . . . 53
Which loop should I use?. . . . . . . . . . . . . . . . . . . 54
Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 57
Programming Problems . . . . . . . . . . . . . . . . . . . . 58
Flowchart Programming . . . . . . . . . . . . . . . . . . . . 61
Making Loops . . . . . . . . . . . . . . . . . . . . . . . . . 61
Pre-conditional Loops. . . . . . . . . . . . . . . . . . . 61
Post-conditional Loops . . . . . . . . . . . . . . . . . . 62
4. Functions
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
The Program Flow . . . . . . . . . . . . . . . . . . . . . . . . 64
Some Pre-defined Functions . . . . . . . . . . . . . . . . 65
The Structure of a Function . . . . . . . . . . . . . . . . . 67
Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 68
The Return Statement. . . . . . . . . . . . . . . . . . . . . . 69
Passing Arguments to the Functions . . . . . . . . . . 70
Pass by Value . . . . . . . . . . . . . . . . . . . . . . . . . 71
Pass by Reference . . . . . . . . . . . . . . . . . . . . . 72
Scope and Lifetime . . . . . . . . . . . . . . . . . . . . . . . 74
Local Variables . . . . . . . . . . . . . . . . . . . . . . . . 74
Global Variables . . . . . . . . . . . . . . . . . . . . . . . 74
Static Local Variables . . . . . . . . . . . . . . . . . . . 76
Overloading Functions . . . . . . . . . . . . . . . . . . . . 77
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . 79
Programming Problems . . . . . . . . . . . . . . . . . . . . 83
5. Arrays and Strings
Arrays and Vector Class . . . . . . . . . . . . . . . . . . . . 86
Array Declaration and
Accessing Array Elements . . . . . . . . . . . . . . . 86
Vector Manipulation . . . . . . . . . . . . . . . . . . . . . . . 88
Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . 90
Passing Arrays to Functions. . . . . . . . . . . . . . . . . 93
Searching Arrays . . . . . . . . . . . . . . . . . . . . . . . . . 95
Sequential (Linear) Searching . . . . . . . . . . . . 95
Binary Searching. . . . . . . . . . . . . . . . . . . . . . . 96
Sorting Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Reading and Printing Strings. . . . . . . . . . . . . 100
String Manipulation . . . . . . . . . . . . . . . . . . . . . . . 101
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 105
Programming Problems . . . . . . . . . . . . . . . . . . . 107
Flowchart Programming . . . . . . . . . . . . . . . . . . . 110
Arrays and Strings . . . . . . . . . . . . . . . . . . . . . 110
6. Structs
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Declaring Structs and Accessing Members . . . 112
Reading and Printing Structures . . . . . . . . . . 112
Hierarchical Structures . . . . . . . . . . . . . . . . . . . . 114
Array of Structs . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 123
Programming Problems . . . . . . . . . . . . . . . . . . . 124
Flowchart Programming . . . . . . . . . . . . . . . . . . . 126
Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
7. Object-Oriented Programming
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Understanding Classes and Objects . . . . . . . . . 130
Member Accessibility. . . . . . . . . . . . . . . . . . . . . . 130
Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Reading and Printing a Class . . . . . . . . . . . . . . . 132
The Class Constructor and
Initializing Class Members . . . . . . . . . . . . . . . 135
Object-Oriented Techniques . . . . . . . . . . . . . . . . 136
a. Encapsulation and Data Hiding . . . . . . . . 136
b. Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . 136
c. Polymorphism . . . . . . . . . . . . . . . . . . . . . . 136
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Operator Overloading . . . . . . . . . . . . . . . . . . . . . 141
Overloading Equal, Assignment, and
Smaller Than Operators. . . . . . . . . . . . . . . . . 141
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . 145
Programming Problems . . . . . . . . . . . . . . . . . . . 148
Answer Key
Index