Brief Contents
Features of the Book iv
Preface vii
Detailed Contents xii
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
Introduction to Object Oriented Programming
Basics of C++ Programming
Decision Control and Looping Statements
Functions
Arrays
Strings
Pointers
Structure, Union, and Enumerated Data Types
Classes and Objects
Constructors and Destructors
Operator Overloading and Type Conversions
Inheritance and Run-Time Polymorphism
File Handling
Templates
Exception Handling
STL and New Features in C++
Object-Oriented System Analysis, Design, and Development
Appendix A C++ Standard Library Functions
Appendix B C++ Interview Questions 772
Index 778
About the Author 781
769
1
27
84
143
190
242
280
331
368
428
472
527
590
629
669
710
754
Detailed Contents
Features of the Book
Preface vii
Brief Contents xi
iv
1. Introduction to Object Oriented
Programming
1.7 Compilers
Introduction 1
1.1 Generation of Programming
Languages 2
2. Basics of C++ Programming
1.2.1
1.2.2
1.2.3
1.2.4
2.4.1
2.4.2
2.4.3
2.4.4
10
Monolithic Programming 10
Procedural Programming 10
Structured Programming 11
Object Oriented Programming 12
1.3.6
1.3.7
1.3.8
1.3.9
Classes 14
Objects 14
Method and Message Passing 15
Inheritance 16
Polymorphism: Static Binding and
Dynamic Binding 17
Containership 17
Genericity 17
Delegation 18
Data Abstraction and Encapsulation
1.4 Merits and Demerits of
Object Oriented Programming
Language 19
1.5 Applications of Object Oriented
Programming 20
1.6 Differences Between Programming
Languages 20
Source Code File 31
Header Files 31
Object Files 32
Binary Executable File
32
2.5 Compiling and Executing C++
Programs 32
2.6 Using Comments 34
2.7 Tokens in C++ 35
2.8 Character Set 35
2.9 Keywords 35
2.10 Identifier 36
2.11 Data Types in C++ 36
2.12 Variables 39
1.3 Features of Object Oriented
Programming 14
1.3.1
1.3.2
1.3.3
1.3.4
1.3.5
27
Introduction to C++ 27
2.1 History of C++ 27
2.2 Structure of a C++ Program 28
2.3 Writing the First C++ Program 28
2.4 Files used in a C++ Program 30
1.1.1 First Generation: Machine
Language 2
1.1.2 Second GenerationAssembly
Language 3
1.1.3 Third Generation: High-Level
Language 5
1.1.4 Fourth Generation: Very High-Level
Languages 8
1.1.5 Fifth Generation Programming
Language 9
1.2 Programming Paradigms
22
2.12.1 Declaring Variables 39
2.12.2 Initializing Variables 40
2.12.3 Reference Variables 40
2.13 Constants
18
2.13.1
2.13.2
2.13.3
2.13.4
2.13.5
41
Integer Constant 41
Floating Point Constant 42
Character Constant 42
String Constant 42
Declaring Constants 42
2.14 Input and Output Statements in C++
2.14.1 Streams 43
2.14.2 Reading and Writing Characters and
Strings 45
2.14.3 Formatted Input and Output
Operations 46
2.15 Operators in C++
52
2.15.1 Arithmetic Operators
52
43
xiii
Detailed Contents
2.15.2
2.15.3
2.15.4
2.15.5
2.15.6
2.15.7
2.15.8
2.15.9
2.15.10
2.15.11
Relational Operators 54
Equality Operators 55
Logical Operators 56
Unary Operators 57
Conditional Operators 59
Bitwise Operators 60
Assignment Operators 62
Comma Operator 63
Sizeof Operator 64
Operator Precedence and
Associativity 64
4.3 Function Declaration or Function
Prototype 145
4.4 Function Definition 147
4.5 Function Call 148
4.6 Return Statement 149
4.7 Passing Parameters to the Function
4.8
4.9
4.10
4.11
2.16 Type Conversion and Type
Casting 71
2.16.1 Type Conversion 71
2.16.2 Type Casting 73
Annexure 1 User Defined Header Files
82
84
Introduction to Decision Control
Statements 84
3.1 Conditional Branching
Statements 84
3.2.1
3.2.2
3.2.3
3.2.4
3.3
3.4
3.5
3.6
111
3.6.1 Key Points about Goto Statement
Introduction 143
4.1 Need for Functions 144
4.2 Using Functions 145
165
Auto Storage Class 165
Register Storage Class 166
Extern Storage Class 167
Static Storage Class 168
Comparison of Storage Classes
169
170
127
172
4.14.1 Matching Function Calls with Overloaded
Functions 173
4.14.2 Key Points about Function
Overloading 174
4.14.3 Functions that Cannot be
Overloaded 176
4.15 Recursive Functions
3.7 Avoiding Usage of Break, Continue,
and Goto Statements 128
Case Study 1 139
143
Block Scope 163
Function Scope 163
Scope of the Program 164
File Scope 165
4.14 Function Overloading
Nested Loops 111
Break Statement 123
Continue Statement 125
Goto Statement 126
4. Functions
4.12.1
4.12.2
4.12.3
4.12.4
4.12.5
162
4.13.1 Advantages and Disadvantages of Inline
Functions 171
4.13.2 Comparison of Inline Functions with
Macros 171
100
While Loop 100
Do-While Loop 104
For Loop 107
Selecting an Appropriate Loop
4.11.1
4.11.2
4.11.3
4.11.4
4.13 Inline Functions
If Statement 85
If-Else Statement 86
If-Else-If Statement 89
Switch Case Statement 95
3.2 Iterative Statements
Default Arguments 159
Return by Reference 161
Passing Constants as Arguments
Variables Scope 163
4.12 Storage Classes
3. Decision Control and Looping
Statements
3.1.1
3.1.2
3.1.3
3.1.4
151
4.7.1 Call-by-Value 151
4.7.2 Call-by-Address 153
4.7.3 Call-by-Reference 154
177
4.15.1 Greatest Common Divisor
4.15.2 Finding Exponents 179
4.15.3 Fibonacci Series 180
178
4.16 Recursion Versus Iteration 181
4.17 Functions with Variable Number of
Arguments 182
5. Arrays
Introduction 190
5.1 Declaration of Arrays 191
5.2 Accessing Elements of the Array
190
192
xiv
Detailed Contents
7. Pointers
5.2.1 Calculating the Address of Array
Elements 193
5.3 Storing Values in Arrays
194
5.3.1 Initialization of Arrays 194
5.3.2 Inputting Values 195
5.3.3 Assigning Values 195
5.4 Calculating the Length of Array 196
5.5 Operations that can be Performed on
Arrays 196
5.5.1
5.5.2
5.5.3
5.5.4
5.5.5
Traversal 197
Insertion 203
Deletion 204
Merging 205
Searching the Array Elements
5.6.1 Passing Individual Elements 212
5.6.2 Passing the Entire Array 213
7.10
7.11
7.12
7.13
7.14
216
5.7.1 Declaration of Two-dimensional
Arrays 217
5.7.2 Initialization of Two-dimensional
Arrays 219
5.7.3 Accessing the Elements 219
7.15
7.16
7.17
7.18
7.19
7.20
5.9.1 Passing a Row 226
5.9.2 Passing the Entire Two-dimensional
Array 226
230
6. Strings
242
Introduction 242
6.1 Representation and Declaration of
Strings 242
6.1.1 Reading Strings 244
6.1.2 Writing Strings 245
6.2
6.3
6.4
6.5
Understanding Computers
Memory 280
Defining Pointers 281
Declaring Pointer Variables 282
Pointer Expressions and Pointer
Arithmetic 286
Null Pointers 288
Generic Pointers 289
Passing Arguments to Function Using
Pointers 289
Pointers and Arrays 290
Passing Array to Function 295
Differences Between Array Name and
Pointers 296
Pointers and Strings 298
Array of Pointers 300
Pointers and 2D Arrays 302
Pointers and 3D Arrays 306
Pointers to Functions 307
7.14.1 Initializing Function Pointer 307
7.14.2 Calling a Function using a Function
Pointer 308
7.14.3 Comparing Function Pointers 309
7.14.4 Passing a Function Pointer as an
Argument to a Function 309
5.8 Operations on Two-dimensional
Arrays 223
5.9 Two-dimensional Arrays for InterFunction Communication 226
5.10 Multi-dimensional Arrays
Case Study 2 237
7.4
7.5
7.6
7.7
7.8
7.9
207
5.6 One-Dimensional Arrays for Inter
Function Communication 212
5.7 Two-Dimensional Arrays
7.1
7.2
7.3
280
String Taxonomy 246
Strings Operations 247
Character Manipulation Functions 256
String Functions Defined in string.h
Header File 257
6.6 Array of Strings 265
Array of Function Pointers 310
Pointers to Pointers 311
Constant Pointer 312
Pointer to Constants 312
Constant Pointer to a Constant 312
Memory Allocation in C++
Programs 313
7.21 Memory Usage 313
7.22 Dynamic Memory Allocation 314
7.22.1 Memory Allocation Process 314
7.22.2 Allocating Memory Using the New
Operator 314
7.22.3 Releasing the Used Space using the
delete Operator 315
7.22.4 Alter the Size of Allocated Memory 316
7.22.5 Advantages of new/delete Operators
over malloc()/free() 317
7.22.6 Dynamically Allocating 2D Arrays 319
Annexure 2 Deciphering Pointer
Declarations 327
xv
Detailed Contents
8. Structure, Union, and
Enumerated Data Types
331
Introduction 331
8.1 Structure Declaration 331
8.2 Typedef Declarations 332
8.3 Initialization of Structures 333
8.4 Accessing the Members of a
Structure 334
8.5 Copying and Comparing Structures
8.6 Nested Structures 337
8.7 Arrays of Structures 339
8.8 Structures and Functions 342
8.8.1
8.8.2
8.8.3
8.8.4
335
Passing Individual Members 342
Passing the Entire Structure 343
Returning Structures 344
Passing Structures Through Pointers
346
8.9 Self-Referential Structures 351
8.10 C++ Extension to Structures 352
8.11 Union 353
8.11.1 Declaring a Union 353
8.11.2 Accessing a Member of a Union
8.11.3 Initializing Unions 405
405
8.13.1 Enum Variables 357
8.13.2 Assigning Values to Enumerated
Variables 357
8.13.3 Enumeration Type Conversion 357
8.13.4 Comparing Enumerated Types 358
8.13.5 Input or Output Operations on Enumerated
Types 358
365
9. Classes and Objects
Introduction 368
9.1 Specifying a Class
368
368
9.1.1 Class Declaration 369
9.1.2 Function Definition 370
9.2
9.3
9.4
9.5
9.6
9.7 Array of Objects 383
9.8 Dynamic Memory Allocation for Array
of Objects 383
9.9 Objects as Function Arguments 386
9.10 Returning Objects 387
9.11 this Pointer 388
9.12 Constant Parameters and Members 395
9.13 Pointers Within a Class 397
9.14 Local Classes 399
9.15 Nested Classes in C++ 400
9.16 Complex Objects (Object
Composition) 401
9.17 Empty Classes 403
9.18 Friend Function 409
9.19 Friend Class 411
9.20 Bit-Fields in Classes 416
9.21 Pointers and Class Members 418
9.21.1 Declaring and Assigning Pointer to Data
Members of a Class 418
9.21.2 Pointer to Member Functions 420
8.12 Unions Inside Structures 354
8.13 Enumerated Data Types 355
Annexure 3 Bit-Fields in Structures
Case Study 3 367
9.6.1 Memory Allocation for Static Data
Members 379
9.6.2 Static Member Functions 381
9.6.3 Static Object 382
Creating Objects 371
Accessing Object Members 372
Nested Member Functions 372
Making a Member Function Inline
Memory Allocation for Class and
Objects 378
10. Constructors and Destructors 428
Introduction 428
10.1 Constructor 429
10.2 Types of Constructors
430
10.2.1 Dummy Constructor (Do Nothing
Constructor) 430
10.2.2 Default Constructor 430
10.2.3 Parameterized Constructor 431
10.2.4 Copy Constructor 431
10.2.5 Dynamic Constructor 433
10.3 Constructor with Default
Arguments 433
10.4 Constructor Overloading 435
10.5 Destructors 450
10.5.1 Important Features 450
10.5.2 Interesting Points about Constructors and
Destructors 456
373
10.6 Object Copy 459
10.7 Constant Objects 460
10.7.1 Key Features of Constant Object
10.8 Anonymous Objects
461
460
xvi
Detailed Contents
10.8.1 Scope of Anonymous Objects 462
10.8.2 Advantages of Anonymous Objects 462
10.9 Anonymous Classes
Case Study 4 469
462
Introduction 472
11.1 Scope of Operator Overloading 472
11.2 Syntax for Operator Overloading 473
11.3 Operators that can and cannot be
Overloaded 474
11.4 Implementing Operator
Overloading 475
11.5 Overloading Unary Operators 475
11.5.1 Using a Member Function to Overload a
Unary Operator 475
11.5.2 Returning Object 476
11.5.3 Returning a Nameless Object 477
11.5.4 Using a Friend Function to Overload a
Unary Operator 477
11.5.5 Overloading the Prefix Increment and
Decrement Operators 478
11.5.6 Overloading the Post-Fix Increment and
Post-Fix Decrement Operators 479
11.6 Overloading Binary Operators 489
11.7 Overloading Special Operators 499
11.7.1 Overloading New and Delete
Operators 500
11.7.2 Overloading Subscript Operators []
and () 502
11.7.3 Overloading Class Member Access
Operator (->) 505
11.7.4 Overloading Input and Output
Operators 506
Introduction
12.6 Multi-Level Inheritance 538
12.7 Constructor in Multi-Level
Inheritance 539
12.8 Multiple Inheritance 544
12.9 Constructors and Destructors in Multiple
Inheritance 545
12.10 Ambiguity in Multiple
Inheritance 547
12.10.1 Solution for the Ambiguity
Problem 547
12.10.2 No Ambiguity in Single
Inheritance 548
12.11 Hierarchical Inheritance 548
12.12 Constructors and Destructors in
Hierarchical Inheritance 550
12.13 Hybrid Inheritance 552
12.14 Multi-path Inheritance 562
12.17.1 Upcasting, Downcasting, and
Cross-Casting 567
12.18 Run-time Polymorphism
12.19 Virtual Functions 568
527
567
12.19.1 Run-Time Polymorphism through
Virtual Functions 568
12.19.2 Rules for Virtual Functions 570
12.20
12.21
12.22
12.23
525
527
12.5.1 Invoking Constructors With
Arguments 536
12.15 Virtual Base Classes 562
12.16 Object Slicing 564
12.17 Pointers to Derived Class 565
507
12. Inheritance and Run-Time
Polymorphism
12.3 Types of Inheritance 530
12.4 Single Inheritance 530
12.5 Constructors and Destructors in
Derived Classes 534
12.14.1 Problem in Multi-Path Inheritance or
Diamond Problem 562
11.8.1 Conversion from Basic to Class Type 507
11.8.2 Conversion from Class to Basic Data
Type 507
11.8.3 Conversion from Class to Class Type 513
Case Study 5
528
12.2.1 Inheriting Protected Members 529
12.2.2 Inheriting the Class in Protected
Mode 530
11. Operator Overloading and Type
Conversions
472
11.8 Type Conversions
12.1 Defining Derived Classes
12.2 Access Specifiers 528
Pure Virtual Functions 571
Abstract Base Classes 572
Concept of Vtables 575
Virtual Constructor and
Destructors 577
12.23.1 Virtual Destructor
577
12.24 Pros and Cons of Inheritance
578
xvii
Detailed Contents
13. File Handling
590
Introduction 590
13.1 Streams in C++ 590
13.2 Classes for File Stream
Operations 591
13.3 Opening and Closing of Files
Introduction 669
15.1 Exception Handling
592
596
15.2 Exceptions in Constructors and
Destructors 684
15.3 Exceptions in Operator Overloaded
Functions 686
15.4 Exceptions and Inheritance 687
15.5 Exceptions and Templates 688
15.6 Handling Uncaught Exceptions 695
15.7 Standard Exceptions 699
15.8 Advantages of Exception
Handling 700
15.9 Word of Caution 701
602
13.7.1 ASCII Text Files 602
13.7.2 Binary Files 603
13.8 Sequential Input and Output
Functions 603
13.8.1 Get() and Put() 604
13.8.2 Read() and Write() Functions
607
13.9 Error Handling During File
Operations 615
13.10 Accepting Command Line
Arguments 616
Case Study 6 627
14. Templates
670
Multiple Catch Statements 672
Catch all Exceptions 673
Exceptions in Invoked Function 674
Stack Unwinding 676
Rethrowing Exception 676
Restricting the Exceptions that can be
Thrown 677
15.1.7 Catching Class Type as
Exceptions 679
13.6.1 Manipulating File Pointers 600
13.6.2 Specifying the offset 601
13.7 Types of Files
669
15.1.1
15.1.2
15.1.3
15.1.4
15.1.5
15.1.6
13.3.1 Opening Files Using
Constructors 593
13.3.2 Opening Files using Member
Function 595
13.3.3 Test For Errors 595
13.4 Detecting the End-of-File
13.5 File Modes 598
13.6 File Pointers and their
Manipulators 599
15. Exception Handling
16. STL and New Features in C++ 710
629
Introduction 629
14.1 Use of Templates 630
14.2 Function Templates 630
14.2.1 Templates Versus Macros 632
14.2.2 Guidelines for Using Template
Functions 632
14.3 Class Template 641
14.4 Class Templates and Friend Function 648
14.5 Templates and Static Variables in
C++ 651
14.6 Class Templates and Inheritance 653
14.7 Class Template with Operator
Overloading 660
14.8 Pros and Cons of Templates 662
Introduction 710
16.1 Containers 710
16.2 Algorithms 712
16.3 Iterators 714
16.4 Using Containers 715
16.4.1
16.4.2
16.4.3
16.4.4
Vector 715
Dequeue 716
Lists 717
Maps 719
16.5 String Class 722
16.6 Data Type Boolean
725
16.6.1 Uses of Boolean Data Type 726
16.6.2 Applications of Operators 726
16.7 Wchar_t Data Type 727
16.8 Run-Time Type Information
16.8.1
16.8.2
16.8.3
16.8.4
16.8.5
Static_cast Operator
728
728
Const_cast 729
Reinterpret_cast 730
Dynamic_cast Operator 731
Typeid Operator 732
xviii
Detailed Contents
17.3 Object Oriented Software Development
Methodology 756
17.4 Object-Oriented Systems
Development 756
16.9 Explicit Keyword 734
16.10 Mutable Keyword 736
16.11 Namespaces 737
16.11.1 Nested Namespaces 738
16.11.2 Unnamed Namespaces 739
16.11.3 Similarity and Dissimilarity with
Classes 740
16.11.4 Classes Within Namespaces 740
17.4.1
17.4.2
17.4.3
17.4.4
Object-Oriented Analysis 757
Object-Oriented Design 758
Prototyping 759
Objectives of OO Analysis and
Design 760
17.4.5 Tools in OO Analysis and OO
Design 760
17.4.6 Implementing Component-based
Development 761
16.12 Operator Keywords 741
16.13 Specifying Header Files 741
Annexure 4 Volatile and Restrict
Qualifiers 750
Annexure 5 Smart Pointers 752
17.5 Unified Modelling Language
17. Object Oriented System Analysis,
Design, and Development
754
Introduction 754
17.1 Traditional Software Development
Process 755
17.2 Building High-Quality Software 755
Appendix A C++ Standard Library Functions
Appendix B C++ Interview Questions 772
Index 778
About the Author 781
769
17.5.1
17.5.2
17.5.3
17.5.4
17.5.5
17.5.6
17.5.7
17.5.8
Class Diagrams 762
Object Diagram 763
Component Diagram 763
Deployment Diagram 763
Use Case Diagram 764
Sequence Diagram 764
State Chart Diagram 764
Activity Diagram 765
762