Example: air traffic controller

C++ Essentials - PragSoft

C++ EssentialsSharam HekmatPragSoft Preliminaries1A simple C++ Program2 Compiling a simple C++ Program3 How C++ Compilation Works4 Variables5 simple Input/Output7 Comments9 Memory10 Integer Numbers11 Real Numbers12 Characters13 Strings14 Names15 Exercises162. Expressions17 Arithmetic Operators18 Relational Operators19 Logical Operators20 Bitwise Operators21 Increment/Decrement Operators22 Assignment Operator23 Conditional Operator24 Comma Operator25 The sizeof Operator26 Operator Precedence27 simple Type Conversion28 Exercises29viC++ EssentialsCopyright 2005 PragSoft3. Statements30 simple and Compound Statements31 The if Statement32 The switch Statement34 The while Statement36 The do Statement37 The for Statement38 The continue Statement40 The break Statement41 The goto Statement42 The return Statement43 Exercises444.

www.pragsoft.com Chapter 1: Preliminaries 1 1. Preliminaries This chapter introduces the basic elements of a C++ program. We will use simple examples to show the structure of C++ programs and the way they are compiled.

Tags:

  Programs, Essential, Simple, C essentials

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of C++ Essentials - PragSoft

1 C++ EssentialsSharam HekmatPragSoft Preliminaries1A simple C++ Program2 Compiling a simple C++ Program3 How C++ Compilation Works4 Variables5 simple Input/Output7 Comments9 Memory10 Integer Numbers11 Real Numbers12 Characters13 Strings14 Names15 Exercises162. Expressions17 Arithmetic Operators18 Relational Operators19 Logical Operators20 Bitwise Operators21 Increment/Decrement Operators22 Assignment Operator23 Conditional Operator24 Comma Operator25 The sizeof Operator26 Operator Precedence27 simple Type Conversion28 Exercises29viC++ EssentialsCopyright 2005 PragSoft3. Statements30 simple and Compound Statements31 The if Statement32 The switch Statement34 The while Statement36 The do Statement37 The for Statement38 The continue Statement40 The break Statement41 The goto Statement42 The return Statement43 Exercises444.

2 Functions45A simple Function46 Parameters and Arguments48 Global and Local Scope49 Scope Operator50 Auto Variables51 Register Variables52 Static Variables and Functions53 Extern Variables and Functions54 Symbolic Constants55 Enumerations56 Runtime Stack57 Inline Functions58 Recursion59 Default Arguments60 Variable Number of Arguments61 Command Line Arguments63 Exercises645. Arrays, Pointers, and References65 Arrays66 Multidimensional Arrays68 Pointers70 Dynamic Memory71 Pointer Arithmetic73 Function Classes82A simple Class83 Inline Member Functions85 Example: A Set Class86 Constructors90 Destructors92 Friends93 Default Arguments95 Implicit Member Argument96 Scope Operator97 Member Initialization List98 Constant Members99 Static Members101 Member Pointers102 References Members104 Class Object Members105 Object Arrays106 Class Scope108 Structures and Unions110 Bit Fields112 Exercises1137.

3 Overloading115 Function Overloading116 Operator Overloading117 Example: Set Operators119 Type Conversion121 Example: Binary Number Class124 Overloading << for Output127 Overloading >> for Input128 Overloading []129 Overloading ()131 Memberwise Initialization133 Memberwise Assignment135 Overloading new and delete136 Overloading ->, *, and &138 Overloading ++ and --142 Exercises1438. Derived Classes145An illustrative Class146A simple Derived Class150viiiC++ EssentialsCopyright 2005 PragSoftClass Hierarchy Notation152 Constructors and Destructors153 Protected Class Members154 Private, Public, and Protected Base Classes155 Virtual Functions156 Multiple Inheritance158 Ambiguity160 Type Conversion161 Inheritance and Class Object Members162 Virtual Base Classes165 Overloaded Operators167 Exercises1689.

4 Templates170 Function Template Definition171 Function Template Instantiation172 Example: Binary Search174 Class Template Definition176 Class Template Instantiation177 Nontype Parameters178 Class Template Specialization179 Class Template Members180 Class Template Friends181 Example: Doubly-linked Lists182 Derived Class Templates186 Exercises18710. Exception Handling188 Flow Control189 The Throw Clause190 The Try Block and Catch Clauses192 Function Throw Lists194 Exercises19511. The IO Library196 The Role of streambuf198 Stream Output with ostream199 Stream Input with istream201 Using the ios Class204 Stream Manipulators209 File IO with fstreams210 Array IO with strstreams212 Example.

5 Program The Preprocessor218 Preprocessor Directives219 Macro Definition220 Quote and Concatenation Operators222 File Inclusion223 Conditional Compilation224 Other Directives226 Predefined Identifiers227 Exercises228 Solutions to Exercises230xC++ EssentialsCopyright 2005 PragSoftPrefaceSince its introduction less than a decade ago, C++ has experienced growingacceptance as a practical object-oriented programming language suitable forteaching, research, and commercial software development. The language has alsorapidly evolved during this period and acquired a number of new features ( ,templates and exception handling) which have added to its book serves as an introduction to the C++ language.

6 It teaches how toprogram in C++ and how to properly use its features. It does not attempt to teachobject-oriented design to any depth, which I believe is best covered in a book in itsown designing this book, I have strived to achieve three goals. First, to producea concise introductory text, free from unnecessary verbosity, so that beginners candevelop a good understanding of the language in a short period of time. Second, Ihave tried to combine a tutorial style (based on explanation of concepts throughexamples) with a reference style (based on a flat structure). As a result, eachchapter consists of a list of relatively short sections (mostly one or two pages), withno further subdivision.

7 This, I hope, further simplifies the reader s task. Finally, Ihave consciously avoided trying to present an absolutely complete description ofC++. While no important topic has been omitted, descriptions of some of the minoridiosyncrasies have been avoided for the sake of clarity and to avoidoverwhelming beginners with too much information. Experience suggests that anysmall knowledge gaps left as a result, will be easily filled over time through AudienceThis book introduces C++ as an object-oriented programming language. Noprevious knowledge of C or any other programming language is assumed.

8 Have already been exposed to a high-level programming language (such as Cor Pascal) will be able to skip over some of the earlier material in this the book is primarily designed for use in undergraduate computerscience courses, it will be equally useful to professional programmers and hobbyistswho intend to learn the language on their own. The entire book can be easilycovered in 10-15 lectures, making it suitable for a one-term or one-semestercourse. It can also be used as the basis of an intensive 4-5 day industrial of the BookThe book is divided into 12 chapters.

9 Each chapter has a flat structure, consistingof an unnumbered sequence of sections, most of which are limited to one or twopages. The aim is to present each new topic in a confined space so that it can bequickly grasped. Each chapter ends with a list of exercises. Answers to all of theexercises are provided in an appendix. Readers are encouraged to attempt as manyof the exercises as feasible and to compare their solutions against the the convenience of readers, the sample programs presented in this book(including the solutions to the exercises) and provided in electronic 1: chapter introduces the basic elements of a C++ program.

10 We will use simpleexamples to show the structure of C++ programs and the way they are concepts such as constants, variables, and their storage in memory willalso be following is a cursory description of the concept of programming for thebenefit of those who are new to the digital computer is a useful tool for solving a great variety of problems. Asolution to a problem is called an algorithm; it describes the sequence of steps tobe performed for the problem to be solved. A simple example of a problem and analgorithm for it would be:Problem:Sort a list of names in ascending lexicographic :Call the given list list1; create an empty list, list2, to hold the sorted find the smallest name in list1, remove it from list1, andmake it the next entry of list2, until list1 is algorithm is expressed in abstract terms.


Related search queries