C++ DEMYSTIFIED (3rd edition)

by Julijan Šribar and Boris Motik

Table of Contents

0. To C++ or not to C++? 1

0.1. A Brief History of Programming Languages ... 1
0.2. Basic Properties of C++ ... 3
0.3. A Comparison to C ... 6
0.4. A Comparison toJava ... 8
    0.4.1. Java Is Completely Object-oriented Programming Language ... 8
    0.4.2. Java Is Running on a Virtual Machine ... 9
    0.4.3. Java Has No Pointers ... 10
    0.4.4. Java Doesn't Support Multiple Inheritance ... 10
    0.4.5. Java Has Garbage Collection Built-in ... 11
    0.4.6. Java Supports Multithreading ... 11
    0.4.7. Java Doesn't Support Operator Overloading ... 12
    0.4.8. Floating Point Operations Are Not Supported in Java Well ... 12
    0.4.9. Java Has a Large Class Library Available ... 12
    0.4.10. Java Is a Proprietary Platform ... 12
0.5. Comparison to C♯ ... 13
0.6. Does it Make Any Sense to Learn C++? ... 13
0.7. Why Doesn't the Example from This Book Work on My Computer? ... 11
0.8. Literature ... 11
0.9. Ackowledgments ... 14
    0.9.1. Ackowledgments with Second and Third Editions ... 15

1. Time to Go... 17

1.1. What Is a Program And How to Write It ... 17
1.2. Everything You Need Besides Will ... 21
    1.2.1. Compiler ... 21
    1.2.2. Editor ... 22
    1.2.3. Debugger ... 22
1.3. My First and My Second C++ Programs ... 22
1.4. My Third C++ Program ... 28
1.5. About Comments ... 30
1.6. Splitting Expressions ... 32

2. Basic Data Types 35

2.1. Identifiers ... 35
2.2. Variables, Objects and Types ... 37
2.3. Assignment Operators ... 40
2.4. Data Types and Operators ... 41
    2.4.1. Numerals ... 41
    2.4.2. Arithmetic Operators ... 47
    2.4.3. Type Casting ... 53
    2.4.4. Numerals Type Casting ... 55
    2.4.5. Constant Types ... 56
    2.4.6. volatile Objects ... 58
    2.4.7. Enumerations ... 59
    2.4.8. Boolean Types and Operators ... 61
    2.4.9. Comparison Operators ... 64
    2.4.10. Characters ... 65
    2.4.11. Bitwise operators ... 69
    2.4.12. Assignment Operators (2 ½) ... 76
    2.4.13. Alternative Operator Representation ... 77
    2.4.14. User Defined Data Types and Operators ... 78
    2.4.15. typedef Names ... 79
2.5. sizeof Operator ... 80
2.6. Comma Operator ... 81
2.7. Operator Precedence and Associativity ... 82

3. Program Flow Statements 87

3.1. Statement Block ... 87
3.2. The if Statement... 89
3.3. Conditional Operator ? : ... 94
3.4. The switch Statement ... 95
3.5. The for Loop ... 99
3.6. The while Statement ... 104
3.7. The do-while Statement Block ... 106
3.8. break and continue Statements ... 108
3.9. Other Jump Statements ... 112
3.10. On Source Code Structuring ... 112
3.11. For Prospective C++ “Gurus” ... 114

4. Arrays, Pointers and References 119

4.1. Data Arrays ... 119
    4.1.1. One-Dimensional Arrays ... 120
    4.1.2. Multidimensional Arrays ... 126
4.2. Pointers ... 133
    4.2.1. What's Use of Pointers if They Cause Such Problems? ... 139
    4.2.1. Null-Pointer ... 140
    4.2.2. Where to Place the Asterisk ... 143
    4.2.3. The Secret Relation between Pointers and Arrays ... 144
    4.2.4. Pointer Arithmetic ... 147
    4.2.6. Dynamic Alocation of Objects ... 151
    4.2.7. Difference Between Automatically and Dynamically Allocated Objects ... 155
    4.2.8. Simple Examples of Memory Leak and How to Avoid It ... 159
    4.2.9. Dynamic Allocation of Constant Objects ... 161
    4.2.10. Dynamic Allocation of Arrays ... 161
    4.2.11. Dynamic Allocation of Multidimensional Arrays ... 166
    4.2.12. Pointers to Pointers ... 167
    4.2.13. Using vector Class for Data Arrays ... 169
4.3. Constant Pointers and Pointers to Constants ... 170
4.4. Strings ... 172
    4.4.1. String Arrays ... 177
    4.4.2. string Class ... 178
4.5. References ... 179
    4.5.1. References for Hackers ... 180
4.6. Headaches with Pointers ... 181
4.7. Shortening the Predefined Type Names ... 185

5. Functions 187

5.1. What Functions Are and Why to Use Them ... 187
5.2. Function Declaration and Definition ... 189
    5.2.1. Function Declaration in Header Files ... 192
5.3. Function Type ... 195
5.4. Argument List ... 198
    5.4.1. Functions With Empty Argument List ... 198
    5.4.2. Passing Arguments by Value ... 198
    5.4.3. Reference As Arguments ... 201
    5.4.4. Pointers As Arguments ... 204
    5.4.5. Pointer Redirection Inside the Function ... 207
    5.4.6. Constant Arguments ... 205
    5.4.7. Arrays As Arguments ... 213
    5.4.8. Default Arguments ... 216
    5.4.9. Functions With Variable Number and Type of Arguments ... 219
5.5. Pointers and References As Return Values ... 224
5.6. A Life of An Object ... 227
    5.6.1. Local Objects ... 228
    5.6.2. Global Objects ... 229
    5.6.3. Static Objects in Functions ... 232
5.7. Inline Functions ... 234
5.8. Function Overloading ... 235
5.9. Recursion ... 241
5.10. Pointers to Functions ... 244
5.11. main() Function ... 250
5.12. Standard Functions ... 252
    5.12.1. String Functions ... 254
    5.12.2. exit() Function ... 260
5.13. Function Templates ... 261
5.14. Functions “Under the Hub” ... 262
5.15. How to Write Functional Functions ... 264
    5.15.1. Functions Contribute to Modularity and Make Testing Easier ... 264
    5.15.2. This Function Contains Only n Statements ... 265
    5.15.3. Use Already Proved Functions ... 265
    5.15.4. Function and Argumants Naming ... 266
    5.15.5. Arguments and Return Value ... 266
    5.15.6. Argument Validation and Error Signalization ... 266
    5.15.7. Design by Contract Principle ... 267
    5.15.8. Use Automated Testing ... 270

6. Classes and Objects 273

6.1. How to Recognize a Class? ... 273
6.2. How to Use a Class ... 276
6.3. Class Definition ... 278
    6.3.1. Class Data Members ... 279
    6.3.2. Accessing Class Members ... 281
    6.3.3. Class Member Functions ... 281
    6.3.4. this Pointer ... 285
    6.3.5. Inline Class Member Functions ... 287
    6.3.6. Access Rights ... 288
    6.3.7. Creating Public Interface by Access Rights ... 290
    6.3.8. Friends to a Class ... 292
6.4. Class Members Declaration ... 294
6.5. Creating and Destroying an Object ... 295
    6.5.1. Constructor ... 295
    6.5.2. Default Constructor ... 303
    6.5.3. Constructor Calling in Object Definition ... 305
    6.5.4. Copy Constructor ... 305
    6.5.5. Move Constructor ... 308
    6.5.6. Reference and Constant Member Initialization ... 310
    6.5.7. Constructors and Access Rights ... 312
    6.5.8. Destructor ... 314
    6.5.9. Global and Static Objects ... 317
6.6. Arrays of Objects ... 318
6.7. Constant Member Functions ... 321
6.8. volatile Member Functions ... 326
6.9. Static Class Members ... 326
    6.9.1. Static Data Members ... 327
    6.9.2. Static Member Functions ... 330
6.10. Class Scope ... 332
    6.10.1. Scope Resolution ... 334
6.11. Nested Classes ... 335
6.12. Local Classes ... 340
6.13. Pointers to Class Members ... 341
    6.13.1. Pointers to Data Members ... 342
    6.13.2. Pointers to Member Functions ... 347
6.14. Temporary Objects ... 350
    6.14.1. Explicitly Created Temporary Objects ... 350
    6.14.2. Temporary Objects as Arguments to Functions ... 354
    6.14.3. Temporary Objects as Return Values of Functions ... 358
6.15. string Standard Class ... 361
6.16. Why My Class Is Not Classy? ... 366
    6.16.1. Class Is Not a Structure Encapsulating Data ... 366
    6.16.2. Differentiate Interface from Implementation ... 366
    6.16.3. Class Must Have Responsibility Clearly Defined ... 367
    6.16.4. Beware Data Hiding ... 367
    6.16.5. Friends May Become Enemies ... 367
    6.16.6. Minimal Interface ... 367
    6.16.7. Quality of Abstraction ... 368

7. Structures and Unions 371

7.1. Structure or Class? ... 371
7.2. Unions ... 373
    7.3. Bit Fields ... 376

8. Operator Overloading 379

8.1. User-Defined Conversions ... 379
    8.1.1. Constructor Conversion ... 380
    8.1.2. Explicit Constructors ... 381
    8.1.3. Conversion Operators ... 383
8.2. Basics of Operator Overloading ... 388
8.3. Operator Function Definition ... 390
    8.3.1. Overloading Operator = ... 394
    8.3.2. Overloading Operator [] ... 396
    8.3.3. Overloading Operator () ... 399
    8.3.4. Overloading Operator -> ... 401
    8.3.5. Prefix and Postfix operators ++ and -- ... 404
    8.3.6. Overloading Operators new and delete ... 408
8.4. Overall Suggestions on Operator Overloading ... 416

9. Inheritance and Class Hierarchy 419

9.1. Does a Class Have a Rich Uncle Across the Ocean? ... 419
9.2. Definition of a Derived Class ... 424
9.3. Inherited Member Access ... 430
9.4. Inheritance and Member Access Rights ... 433
    9.4.1. Protected Members ... 433
    9.4.2. Public Base Class ... 434
    9.4.3. Private Base Class ... 436
    9.4.4. Protected Base Class ... 438
    9.4.5. Additional Notes on Access Rights ... 440
    9.4.6. Exempting Individual Members ... 442
9.5. Inheritance and Assignment ... 443
9.6. Inherited Class Initialization and Destruction ... 445
9.7. Standard Conversions and Inheritance ... 448
9.8. Class Scope and Inheritance ... 451
    9.8.1. Difference between Inheritance and Overloading ... 452
    9.8.2. Nested Types and Inheritance ... 453
9.9. Class as a Function Argument ... 454
    9.9.1. Exact Type Matching ... 455
    9.9.2. Standard Conversions ... 455
    9.9.3. User-Defined Conversions ... 457
9.10. Inheriting Operator Functions ... 459
9.11. Principles of Polymorphism ... 462
    9.11.1. Virtual Function Members ... 465
    9.11.2. Virtual Function Call ... 469
    9.11.3. Pure Virtual Function Members ... 472
    9.11.4. Virtual Destructors ... 473
9.12. Virtual Base Class ... 475
    9.12.1. Declaration of Virtual Base Class ... 477
    9.12.2. Virtual Base Class Member Access ... 478
    9.12.3. Initialization of Virtual Base Class ... 480
9.13. How to Inherit Efficiently ... 482
    9.13.1. To Inherit or Not? ... 482
    9.13.2. Use Inheritance Correctly ... 492
    9.13.3. Creating Inheritance Hierarchy ... 492
    9.13.4. Use of Polymorphism And Virtual Function Members ... 493
    9.13.5. Data Members Hiding ... 494

10. Templates 495

10.1. What's the Use of Templates? ... 495
10.2. Function Templates ... 469
    10.2.1. Function Template Definition ... 497
    10.2.2. Function Template Arguments ... 499
    10.2.3. Function Template Instantiation ... 502
    10.2.4. Explicit Function Template Instantiation ... 506
    10.2.5. Function Template Overloading ... 508
    10.2.6. Specializing a Template Function ... 509
    10.2.7. An Example of Function Template for Bubble Sort ... 511
10.3. Class Templates ... 512
    10.3.1. Class Template Definition ... 513
    10.3.2. Class Template Instantiation ... 517
    10.3.3. Explicit Class Template Instantiation ... 520
    10.3.4. Specializing a Class Template ... 521
    10.3.5. Class Template Static Members ... 523
    10.3.6. Templates Constant Expression Parameters ... 525
    10.3.7. Class Templates and Nested Types ... 526
    10.3.8. Nested Templates ... 528
    10.3.9. Templates and Class Friends ... 530
    10.3.10. Templates and Inheritance ... 533
10.4. Point of Instantiation ... 538
10.5. A List Class Using Class Template ... 541
10.6. Classes in Standard Template Library ... 544
    10.6.1. complex Template Class ... 544
    10.6.2. valarray Template Class ... 548
10.7. Templates Metaprogramming ... 558
10.8. A Few Final Words On Templates ... 561

11. Exception Handling 563

11.1. What Exceptions Are? ... 563
11.2. Try and Catch Blocks ... 566
11.3. Order of Exception Handling ... 569
11.4. catch Keyword More Closely ... 572
    11.4.1. Exception Re-Throw ... 574
    11.4.2. Memory Allocation and Deallocation ... 575
    11.4.3. Pointers and References as Exception Objects ... 577
11.5. Function Throw List ... 578
11.6. Exceptions During Memory Allocation ... 581
11.7. Constructor Exception Handling ... 584
11.8. Standard Exceptions ... 587

12. Run-Time Type Identification 589

12.1. Static and Dynamic Types ... 589
12.2. Operator typeid ... 591
12.3. Safe Conversion ... 594
12.4. Other Conversion Operators ... 598
    12.4.1. Modifying Object Constness ... 598
    12.4.2. Static Cast ... 598

13. Namespaces 603

13.1. Problem of a Namespace ... 603
13.2. Namespace Declaration ... 604
13.3. Accessing Items in a Namespace ... 607
    13.3.1. Declaration using ... 608
    13.3.2. Directive using ... 613
    13.3.3. Standard Namespace std ... 616
13.4. Namespaces and Existing Code ... 617

14. Managing Multiple Files for Large Projects 619

14.1. Why Split the Code into Several Files? ... 619
14.2. Linkage ... 620
    14.2.1. External and Internal Linkage ... 622
    14.2.2. Declaring Internal Linkage ... 625
    14.2.3. extern Keyword ... 627
14.3. The One-Definition Rule ... 629
14.4. Header Files ... 630
14.5. Organizing Templates ... 637
14.6. An Example of Distributing Declarations and Definitions in Several Files ... 640
14.7. General Rules on Header Files Inclusion ... 648
14.8. Linkage with Other Programming Languages ... 652
    14.8.1. Invoking C Functions from C++ code ... 653
    14.8.2. Inclusion of Assembly Language Instructions ... 655

15. Input/Output Streams 657

15.1. What Streams Are? ... 657
15.2. iostream Library ... 659
15.3. State of the Stream ... 661
15.4. Output with cout ... 662
    15.4.1. Insertion Operator << ... 662
    15.4.2. Output of User-Defined Types ... 664
    15.4.3. Other Members of ostream Class ... 666
15.5. Input with cin ... 666
    15.5.1. Extraction Operator >> ... 667
    15.5.2. Inputting User-Defined Types ... 668
    15.5.3. Getting Strings from Standard Input ... 669
    15.5.4. Other Members of istream Class ... 672
15.6. Input and Output Control ... 675
    15.6.1. Stream Concatenation ... 676
    15.6.2. Output Width ... 676
    15.6.3. Filling Blanks ... 677
    15.6.4. Formatting Flags ... 677
    15.6.5. Formatted Output of Floating Point Constants ... 681
    15.6.6. Manipulators ... 683
15.7. File Input and Output ... 687
    15.7.1. ifstream and ofstream Classes ... 687
    15.7.2. File Opening and Closing ... 690
    15.7.3. fstream Class... 692
    15.7.4. Getting and Setting the Position inside a File ... 694
    15.7.5. Binary Output and Input ... 696
15.8. String Streams ... 699
15.9. Do All Streams Flow into the Sea? ... 700

16. Standard Template Library 701

16.1. Standard Library Structure ... 701
    16.1.1. Language Support ... 702
    16.1.2. Diagnostics ... 703
    16.1.3. General Utilities ... 703
    16.1.4. Strings ... 704
    16.1.5. Localization ... 704
    16.1.6. Numerics ... 705
    16.1.7. Input/Output ... 705
    16.1.8. Containers, Iterators and Algorithms ... 705
16.2. Standard Template Library from Antique Times ... 706
16.3. Library Structure ... 707
16.4. Containers ... 708
    16.4.1. Vector ... 709
    16.4.2. List ... 716
    16.4.3. Deque ... 717
16.5. Iterators ... 717
    16.5.1. What's the Meaning of Iterator? ... 717
    16.5.2. Properties of Iterators ... 721
    16.5.3. Input and Output Iterators ... 728
    16.5.4. Forward Iterators ... 729
    16.5.5. Bidirectional Iterators ... 730
    16.5.6. Random-Access Iterators ... 731
16.6. Algorithms ... 732
    16.6.1. How to Write a Generic Algorithm ... 733
    16.6.2. Types of Algorithms in Standard Library ... 735
    16.6.3. Non-Modifying Sequence Algorithms ... 735
    16.6.4. Modifying Sequence Algorithms ... 737
    16.6.5. Sortong Sequences ... 739
    16.6.6. Algorithms Inherited from C Language ... 741
16.7. Function Objects ... 741
    16.7.1. What Function Objects Are ... 741
    16.7.2. Predicates ... 743
    16.7.3. Arithmetic Function Objects ... 745
    16.7.4. Binders, Adapters and Negaters ... 746
16.8. Lambda Expressions ... 749
16.9. Assocative Containers ... 753
    16.9.1. Set and Multiset ... 754
    16.9.2. Map and Multimap ... 761
16.10. Special Containers ... 764
    16.10.1. Stacks ... 764
    16.10.2. Queues ... 765
    16.10.3. Priority Queues ... 766
    16.10.4. Bitsets ... 767
16.11. General Notes on STL Use ... 770

17. The Preprocessor 773

17.1. In the Beginning There Was a Preprocessor ... 773
17.2. Directive #include ... 774
17.3. Directive #define ... 775
    17.3.1. Scope of Definition ... 776
    17.3.2. Predefined Macro Names ... 777
    17.3.3. Macro Functions ... 779
    17.3.4. String Manipulation ... 779
17.4. Conditional Compilation ... 780
    17.4.1. Debugging with Conditional Compilation ... 782
17.5. Other Preprocessor Directives ... 783
17.6. Preprocessor, What Is It Good for? ... 784

18. Principles of Object-Oriented Design 785

18.1. Why C++ at All? ... 785
18.2. Object Paradigm ... 786
18.3. Code Reusability ... 788
18.4. Step 1: Finding Appropriate Abstraction ... 789
18.5. Step 2: Abstraction Definition ... 791
    18.5.1. Screen Definition ... 791
    18.5.2. Window Definition ... 792
    18.5.3. Menu Definition ... 792
18.6. Step 3: Defining a Relationship and Link between Classes ... 793
    18.6.1. Relationship of User-Interface Objects ... 796
18.7. Step 4: Definition of Implementation-Dependant Abstractions ... 800
18.8. Step 5: Interface Definition ... 801
18.9. Step 6: Implementation ... 809

Appendices
A. Standard Library 813

A.1. Standard Macro Functions and Makro Names ... 815
A.2. Standard Values ... 822
A.3. Standard Types ... 822
A.4. Standard Classes and Structures ... 822

B. Standard Functions 825

B.1. Character and String Manipulation Functions ... 826
B.2. Number and String Conversion Functions ... 832
B.3. Time and Date Functions ... 834
B.4. Mathematical Functions ... 837
B.5. Input/Output Functions for Standard Streams ... 845
B.6. File Functions ... 848
B.7. Miscallenous Functions ... 854

C. Standard Template Library 863

C.1. Generic Algorithms ... 863

D. Using Compilers 769

D.1. Dev-C++ ... 877
D.2. Microsoft Visual C++ ... 880
D.3. Displaying Localized Characters in Windows Console ... 883

Glossary 887
piramidica Literature 897

C++ Programming Language... 897
Design ... 900

Index 901


"...I would like to emphasize that I personally enjoyed reviewing this book. Of all the literature from this area I have the opportunity to read, this is the only one for which I can say that is as much amusing as it is instructive."

Vedran Mornar, Ph.D., assistant professor, Faculty of Electrical Engineering and Computing, Zagreb