C++ was developed by Bjarne Stroustrup of AT&T Bell Laboratories in the early 1980's, and is based on the C language. The name is a pun - "++" is a syntactic construct used in C (to increment a variable), and C++ is intended as an incremental improvement of C. Most of C is a subset of C++, so that most C programs can be compiled (i.e. converted into a series of low-level instructions that the computer can execute directly) using a C++ compiler. C is in many ways hard to categorise. Compared to assembly language it is high-level, but it nevertheless includes many low-level facilities to directly manipulate the computer's memory. It is therefore an excellent language for writing efficient "systems" programs. But for other types of programs, C code can be hard to understand, and C programs can therefore be particularly prone to certain types of error. The extra object-oriented facilities in C++ are partly included to overcome these shortcomings.
2K12/ESWE/11
An editor with which to write and modify the C++ program components or source code, A compiler with which to convert the source code into machine instructions which can be executed by the computer directly, A linking program with which to link the compiled program components with each other and with a selection of routines from existing libraries of computer code, in order to form the complete machine-executable object program, A debugger to help diagnose problems, either in compiling programs in the first place, or if the object program runs but gives unintended results.
There are several editors available for UNIX-based systems. Two of the most popular editors are emacs and vi. For the compiler and linker, we will be using the GNU g++ compiler/linker, and for the debugger we will be using the GNU debugger gdb. For those that prefer an integrated development environment (IDE) that combines an editor, a compiler, a linking program and a debugger in a single programming environment (in a similar way to Microsoft Developer Studio under Windows NT), there are also IDEs available for UNIX (e.g. Eclipse, xcode, kdevelop etc.)
Illustrate many important features of C++ Each analyzed one statement at a time
2K12/ESWE/11
cout
Standard output stream Normally computer screen
cerr
Standard error stream Display error messages
2K12/ESWE/11
Comments
Document programs Improve program readability Ignored by compiler Single-line comment Begin with // Preprocessor directives Processed by preprocessor before compiling Begin with # Standard output stream object cout
Connected to screen <<
Stream insertion operator Value to right (right operand) inserted into output stream Escape characters \ Indicates special character output
2K12/ESWE/11
Escape Sequence \n \t \r
Description Newline. Position the screen cursor to the beginning of the next line. Horizontal tab. Move the screen cursor to the next tab stop. Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. Alert. Sound the system bell. Backslash. Used to print a backslash character. Double quote. Used to print a double quote character.
\a \\ \"
Arithmetic
Arithmetic calculations
* Multiplication
/ Division Integer division truncates remainder 7 / 5 evaluates to 1 % Modulus operator returns remainder 7 % 5 evaluates to 2
2K12/ESWE/11
Rules of operator precedence Operators in parentheses evaluated first Nested/embedded parentheses Operators in innermost pair first Multiplication, division, modulus applied next Operators applied from left to right Addition, subtraction applied last Operators applied from left to right
Operator(s) () Operation(s) Parentheses Order of evaluation (precedence) Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses on the same level (i.e., not nested), they are evaluated left to right.
*, /, or % + or -
Multiplication Division Evaluated second. If there are several, they re Modulus evaluated left to right. Addition Subtraction Evaluated last. If there are several, they are evaluated left to right.
2K12/ESWE/11
Equality and relational operators Equality operators Same level of precedence Relational operators Same level of precedence Associate left to right
x is greater than y x is less than y x is greater than or equal to y x is less than or equal to y
Equality operators =
== !=
x == y x != y
2K12/ESWE/11