Table of Contents
Preface v
About the Author xiii
1 Introduction to Computers and Programming 1
1.1 History of computers 1
1.2 Introduction to computer and computer science 1
1.3 Introduction to software 3
1.4 Programming languages 6
1.5 Types of programming languages 7
1.6 Review questions 9
2 Introduction to Object-Oriented Programming and C++ 11
2.1 Introduction 11
2.2 Object-oriented concepts 11
2.3 Introduction to C++ 13
2.4 Versions of C++ 14
2.5 Writing the first C++ program 14
2.6 Compiling and running C++ Programs 17
2.7 Running C++ programs online 18
2.8 Running C++ programs in visual studio 2017 23
2.9 Running C++ programs in microsoft visual C++ 28
2.10 Running C++ programs in codeblocks IDE 32
2.11 Writing the first C++ program in CodeBlocks IDE 38
2.12 Comprehensive understanding of object-oriented concepts in C++ 41
2.13 Review questions 45
3 Programming Basics 47
3.1 Introduction 47
3.2 Variables and identifiers 47
3.3 C++ Keywords 48
3.4 Data types 49
3.5 C++ Literals and constants 52
3.6 Type casting 60
3.7 Input and output manipulators 62
3.8 Storage duration and scope 63
3.9 Review questions 69
4 Operators and Expressions 71
4.1 Introduction 71
4.2 Relational and equality operators 72
4.3 Arithmetic operators 72
4.4 Bitwise operators 73
4.5 Assignment operators 77
4.6 Increment and decrement operators 78
4.7 Logical operators 80
4.8 Conditional operator 80
4.9 Operator precedence 81
4.10 Review questions 83
5 Selection Statements 85
5.1 Introduction 85
5.2 The if statement 85
5.3 The if-else statement 87
5.4 The if-else-if statement 89
5.5 Nested if-else statement 90
5.6 The switch statement 93
5.7 The ternary operator 97
5.8 Review questions 99
6 Looping Statements 101
6.1 Introduction 101
6.2 The while loop 101
6.3 The do-while loop 104
6.4 The for loop 107
6.5 Nesting of loops 111
6.6 The break statement 112
6.7 The continue statement 116
6.8 Review questions 117
7 Arrays 121
7.1 Introduction 121
7.2 One-dimensional arrays 122
7.3 Multidimensional arrays 125
7.4 Applications of two-dimensional arrays 127
7.5 Review questions 135
8 Strings and Pointers 137
8.1 Introduction 137
8.2 String library functions 141
8.3 Array of strings 143
8.4 Introduction to pointers 144
8.5 Pointer to pointer 148
8.6 Pointers and arrays 150
8.7 Array of pointers 152
8.8 Review questions 153
9 Searching and Sorting 155
9.1 Introduction 155
9.2 Searching 155
9.3 Linear search 155
9.4 Binary search 157
9.5 Sorting 159
9.6 Insertion sort 160
9.7 Selection sort 162
9.8 Bubble sort 164
9.9 Sorting characters and strings 167
9.10 Review questions 172
10 Functions 173
10.1 Introduction 173
10.2 Defining a function 174
10.3 Arguments and parameters 177
10.4 Scope of function variables 180
10.5 Static variables 183
10.6 Scope resolution operator 185
10.7 Functions and pointers 187
10.8 Recursive functions 190
10.9 Inline functions 197
10.10 Built-in functions 198
10.11 Review questions 200
11 Structures and Unions 203
11.1 Introduction 203
11.2 Defining structures 204
11.3 Initializing structures 205
11.4 Accessing structure members 206
11.5 Using typedef keyword 208
11.6 Nested structures 209
11.7 Structures containing arrays 211
11.8 Arrays of structures 215
11.9 Structures and pointers 218
11.10 Structures and functions 220
11.11 Unions 223
11.12 Review questions 226
12 Exception Handling 227
12.1 Introduction 227
12.2 Handling exceptions 230
12.3 Multiple catch statements 231
12.4 Exceptions within functions 234
12.5 C++ standard exceptions 235
12.6 Review questions 237
13 Basic I/O and File Handling 239
13.1 Introduction 239
13.2 Standard input and output 239
13.3 File I/O 242
13.4 Review questions 251
14 Classes and Objects 253
14.1 Introduction 253
14.2 Object definition 253
14.3 Class definition 254
14.4 Overloaded methods 259
14.5 Multiple objects 260
14.6 Array of objects 261
14.7 Access modifiers 264
14.8 Review questions 267
15 Constructors and Destructors 269
15.1 Introduction 269
15.2 Default constructors 271
15.3 Constructors with parameters 273
15.4 Multiple constructors 277
15.5 Copy constructor 278
15.6 Destructors 280
15.7 Review questions 281
16 Inheritance 283
16.1 Introduction 283
16.2 Single inheritance 283
16.3 Multiple inheritance 288
16.4 Multilevel inheritance 289
16.5 The protected keyword 291
16.6 Overriding data and methods 293
16.7 Constructors and inheritance 294
16.8 Review questions 298
17 Polymorphism 299
17.1 Introduction 299
17.2 Static polymorphism 299
17.3 Dynamic polymorphism 305
17.4 Pure virtual function and abstract classes 312
17.5 Review questions 313
18 Templates 315
18.1 Introduction 315
18.2 Function template 317
18.3 Class template 320
18.4 Standard template library 322
18.5 Review questions 325
Appendix A 327
Appendix B 335
Appendix C 337
Appendix D 343
Appendix E 345
Index 347