Published online by Cambridge University Press: 05 June 2012
Before discussing the C++ language in detail, we summarize its conceptual foundations. Since C++ consistently adheres to a small number of basic principles, acquiring a working understanding of these greatly hastens learning of C++ syntax.
Overview of program structure
The smallest unit of a C++ program is a token, which is a letter or symbol that the compiler can process. Appropriate groups of tokens yield words. Of these, identifiers (variable names) form atomic (single-element) expressions. Operators combine expressions to form new expressions. Terminating a valid expression with a semicolon yields a statement, which is equivalent to a sentence. A block, which is analogous to a paragraph, unifies and isolates one or more statements from the remainder of the program. Control structures determine the program flow according to the outcome of logical operations, while functions and possibly subroutines modularize compound statements by associating a label with a frequently occurring sequence of statements. Finally, classes and objects structure related variables and functions into generalized arrays. These, like a chapter, describe a single topic, namely the properties of a related group of objects.
Tokens, names and keywords
The C++ compiler processes source-code lines in order of appearance and text within a line from left to right. A program is read as a sequence of tokens separated by non-printing whitespace characters, which include tabs, carriage returns and spaces. Valid tokens are a–z, A–Z, 0–9 and certain punctuation characters. Upper- and lower-case letters represent different tokens such that myVariable and myvariable are unrelated names. A word is a sequence of tokens terminated by whitespace. A reserved keyword is a word such as int or if for which the complier has a special interpretation.
To save this book to your Kindle, first ensure no-reply@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.
Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.
Find out more about the Kindle Personal Document Service.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.