View
223
Download
1
Category
Preview:
DESCRIPTION
intro to c
Citation preview
Brief ContentsFeatures of the Book ivPreface viiDetailed Contents xii
1. Introduction to Object Oriented Programming 1
2. Basics of C++ Programming 27
3. Decision Control and Looping Statements 84
4. Functions 143
5. Arrays 190
6. Strings 242
7. Pointers 280
8. Structure, Union, and Enumerated Data Types 331
9. Classes and Objects 368
10. Constructors and Destructors 428
11. Operator Overloading and Type Conversions 472
12. Inheritance and Run-Time Polymorphism 527
13. File Handling 590
14. Templates 629
15. Exception Handling 669
16. STL and New Features in C++ 710
17. Object-Oriented System Analysis, Design, and Development 754
Appendix A C++ Standard Library Functions 769Appendix B C++ Interview Questions 772Index 778About the Author 781
Detailed ContentsFeatures of the Book ivPreface viiBrief Contents xi
1. Introduction to Object Oriented Programming 1
Introduction 1 1.1 Generation of Programming
Languages 2 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 10
1.2.1 Monolithic Programming 10 1.2.2 Procedural Programming 10 1.2.3 Structured Programming 11 1.2.4 Object Oriented Programming 12
1.3 Features of Object Oriented Programming 14
1.3.1 Classes 14 1.3.2 Objects 14 1.3.3 Method and Message Passing 15 1.3.4 Inheritance 16 1.3.5 Polymorphism: Static Binding and
Dynamic Binding 17 1.3.6 Containership 17 1.3.7 Genericity 17 1.3.8 Delegation 18 1.3.9 Data Abstraction and Encapsulation 18
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
1.7 Compilers 22
2. Basics of C++ Programming 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
2.4.1 Source Code File 31 2.4.2 Header Files 31 2.4.3 Object Files 32 2.4.4 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 Identifi er 36 2.11 Data Types in C++ 36 2.12 Variables 39
2.12.1 Declaring Variables 39 2.12.2 Initializing Variables 40 2.12.3 Reference Variables 40
2.13 Constants 41 2.13.1 Integer Constant 41 2.13.2 Floating Point Constant 42 2.13.3 Character Constant 42 2.13.4 String Constant 42 2.13.5 Declaring Constants 42
2.14 Input and Output Statements in C++ 43 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
xiiiDetailed Contents
2.15.2 Relational Operators 54 2.15.3 Equality Operators 55 2.15.4 Logical Operators 56 2.15.5 Unary Operators 57 2.15.6 Conditional Operators 59 2.15.7 Bitwise Operators 60 2.15.8 Assignment Operators 62 2.15.9 Comma Operator 63 2.15.10 Sizeof Operator 64 2.15.11 Operator Precedence and
Associativity 64 2.16 Type Conversion and Type
Casting 71 2.16.1 Type Conversion 71 2.16.2 Type Casting 73
Annexure 1 User Defi ned Header Files 82
3. Decision Control and Looping Statements 84
Introduction to Decision Control Statements 84
3.1 Conditional Branching Statements 84
3.1.1 If Statement 85 3.1.2 If-Else Statement 86 3.1.3 If-Else-If Statement 89 3.1.4 Switch Case Statement 95
3.2 Iterative Statements 100 3.2.1 While Loop 100 3.2.2 Do-While Loop 104 3.2.3 For Loop 107 3.2.4 Selecting an Appropriate Loop 111
3.3 Nested Loops 111 3.4 Break Statement 123 3.5 Continue Statement 125 3.6 Goto Statement 126
3.6.1 Key Points about Goto Statement 127 3.7 Avoiding Usage of Break, Continue,
and Goto Statements 128Case Study 1 139
4. Functions 143
Introduction 143 4.1 Need for Functions 144 4.2 Using Functions 145
4.3 Function Declaration or Function Prototype 145
4.4 Function Defi nition 147 4.5 Function Call 148 4.6 Return Statement 149 4.7 Passing Parameters to the Function 151
4.7.1 Call-by-Value 151 4.7.2 Call-by-Address 153 4.7.3 Call-by-Reference 154
4.8 Default Arguments 159 4.9 Return by Reference 161 4.10 Passing Constants as Arguments 162 4.11 Variables Scope 163
4.11.1 Block Scope 163 4.11.2 Function Scope 163 4.11.3 Scope of the Program 164 4.11.4 File Scope 165
4.12 Storage Classes 165 4.12.1 Auto Storage Class 165 4.12.2 Register Storage Class 166 4.12.3 Extern Storage Class 167 4.12.4 Static Storage Class 168 4.12.5 Comparison of Storage Classes 169
4.13 Inline Functions 170 4.13.1 Advantages and Disadvantages of Inline
Functions 171 4.13.2 Comparison of Inline Functions with
Macros 171 4.14 Function Overloading 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 177 4.15.1 Greatest Common Divisor 178 4.15.2 Finding Exponents 179 4.15.3 Fibonacci Series 180
4.16 Recursion Versus Iteration 181 4.17 Functions with Variable Number of
Arguments 182
5. Arrays 190
Introduction 190 5.1 Declaration of Arrays 191 5.2 Accessing Elements of the Array 192
xiv Detailed Contents
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 Traversal 197 5.5.2 Insertion 203 5.5.3 Deletion 204 5.5.4 Merging 205 5.5.5 Searching the Array Elements 207
5.6 One-Dimensional Arrays for Inter Function Communication 212
5.6.1 Passing Individual Elements 212 5.6.2 Passing the Entire Array 213
5.7 Two-Dimensional Arrays 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
5.8 Operations on Two-dimensional Arrays 223
5.9 Two-dimensional Arrays for Inter-Function Communication 226
5.9.1 Passing a Row 226 5.9.2 Passing the Entire Two-dimensional
Array 226 5.10 Multi-dimensional Arrays 230Case Study 2 237
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 String Taxonomy 246 6.3 Strings Operations 247 6.4 Character Manipulation Functions 256 6.5 String Functions Defi ned in string.h
Header File 257 6.6 Array of Strings 265
7. Pointers 280
Understanding Computers Memory 280
7.1 Defi ning Pointers 281 7.2 Declaring Pointer Variables 282 7.3 Pointer Expressions and Pointer
Arithmetic 286 7.4 Null Pointers 288 7.5 Generic Pointers 289 7.6 Passing Arguments to Function Using
Pointers 289 7.7 Pointers and Arrays 290 7.8 Passing Array to Function 295 7.9 Differences Between Array Name and
Pointers 296 7.10 Pointers and Strings 298 7.11 Array of Pointers 300 7.12 Pointers and 2D Arrays 302 7.13 Pointers and 3D Arrays 306 7.14 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 7.15 Array of Function Pointers 310 7.16 Pointers to Pointers 311 7.17 Constant Pointer 312 7.18 Pointer to Constants 312 7.19 Constant Pointer to a Constant 312 7.20 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
xvDetailed 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 335 8.6 Nested Structures 337 8.7 Arrays of Structures 339 8.8 Structures and Functions 342
8.8.1 Passing Individual Members 342 8.8.2 Passing the Entire Structure 343 8.8.3 Returning Structures 344 8.8.4 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 405 8.11.3 Initializing Unions 405
8.12 Unions Inside Structures 354 8.13 Enumerated Data Types 355
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 358Annexure 3 Bit-Fields in Structures 365Case Study 3 367
9. Classes and Objects 368
Introduction 368 9.1 Specifying a Class 368
9.1.1 Class Declaration 369 9.1.2 Function Defi nition 370
9.2 Creating Objects 371 9.3 Accessing Object Members 372 9.4 Nested Member Functions 372 9.5 Making a Member Function Inline 373 9.6 Memory Allocation for Class and
Objects 378
9.6.1 Memory Allocation for Static Data Members 379
9.6.2 Static Member Functions 381 9.6.3 Static Object 382
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
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 10.6 Object Copy 459 10.7 Constant Objects 460
10.7.1 Key Features of Constant Object 460 10.8 Anonymous Objects 461
xvi Detailed Contents
10.8.1 Scope of Anonymous Objects 462 10.8.2 Advantages of Anonymous Objects 462
10.9 Anonymous Classes 462Case Study 4 469
11. Operator Overloading and Type Conversions 472
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 Prefi x 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
11.8 Type Conversions 507 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 525
12. Inheritance and Run-Time Polymorphism 527
Introduction 527
12.1 Defi ning Derived Classes 528 12.2 Access Specifi ers 528
12.2.1 Inheriting Protected Members 529 12.2.2 Inheriting the Class in Protected
Mode 530 12.3 Types of Inheritance 530 12.4 Single Inheritance 530 12.5 Constructors and Destructors in
Derived Classes 534 12.5.1 Invoking Constructors With
Arguments 536 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.14.1 Problem in Multi-Path Inheritance or Diamond Problem 562
12.15 Virtual Base Classes 562 12.16 Object Slicing 564 12.17 Pointers to Derived Class 565
12.17.1 Upcasting, Downcasting, and Cross-Casting 567
12.18 Run-time Polymorphism 567 12.19 Virtual Functions 568
12.19.1 Run-Time Polymorphism through Virtual Functions 568
12.19.2 Rules for Virtual Functions 570 12.20 Pure Virtual Functions 571 12.21 Abstract Base Classes 572 12.22 Concept of Vtables 575 12.23 Virtual Constructor and
Destructors 577 12.23.1 Virtual Destructor 577
12.24 Pros and Cons of Inheritance 578
xviiDetailed 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 592
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 596 13.5 File Modes 598 13.6 File Pointers and their
Manipulators 599 13.6.1 Manipulating File Pointers 600 13.6.2 Specifying the offset 601
13.7 Types of Files 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 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
15. Exception Handling 669
Introduction 669 15.1 Exception Handling 670
15.1.1 Multiple Catch Statements 672 15.1.2 Catch all Exceptions 673 15.1.3 Exceptions in Invoked Function 674 15.1.4 Stack Unwinding 676 15.1.5 Rethrowing Exception 676 15.1.6 Restricting the Exceptions that can be
Thrown 677 15.1.7 Catching Class Type as
Exceptions 679 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
16. STL and New Features in C++ 710
Introduction 710 16.1 Containers 710 16.2 Algorithms 712 16.3 Iterators 714 16.4 Using Containers 715
16.4.1 Vector 715 16.4.2 Dequeue 716 16.4.3 Lists 717 16.4.4 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 728
16.8.1 Static_cast Operator 728 16.8.2 Const_cast 729 16.8.3 Reinterpret_cast 730 16.8.4 Dynamic_cast Operator 731 16.8.5 Typeid Operator 732
xviii Detailed Contents
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
16.12 Operator Keywords 741 16.13 Specifying Header Files 741Annexure 4 Volatile and Restrict
Qualifi ers 750Annexure 5 Smart Pointers 752
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
17.3 Object Oriented Software Development Methodology 756
17.4 Object-Oriented Systems Development 756
17.4.1 Object-Oriented Analysis 757 17.4.2 Object-Oriented Design 758 17.4.3 Prototyping 759 17.4.4 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 17.5 Unifi ed Modelling Language 762
17.5.1 Class Diagrams 762 17.5.2 Object Diagram 763 17.5.3 Component Diagram 763 17.5.4 Deployment Diagram 763 17.5.5 Use Case Diagram 764 17.5.6 Sequence Diagram 764 17.5.7 State Chart Diagram 764 17.5.8 Activity Diagram 765
Appendix A C++ Standard Library Functions 769Appendix B C++ Interview Questions 772Index 778About the Author 781
Recommended