Вы находитесь на странице: 1из 7

The Origins of c/C++

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.

1.3 ANSI C++


The American National Standards Institution (ANSI) provides "official" and generally accepted standard definitions of many programming languages, including C and C++. Such standards are important. A program written only in ANSI C++ is guaranteed to run on any computer whose supporting software conforms to the ANSI standard. In other words, the standard guarantees that ANSI C++ programs are portable. In practice most versions of C++ include ANSI C++ as a core language, but also include extra machine-dependent features to allow smooth interaction with different computers' operating systems. These machine dependent features should be used sparingly. Moreover, when parts of a C++ program use non-ANSI components of the language, these should be clearly marked, and as far a possible separated from the rest of the program, so as to make modification of the program for different machines and operating systems as easy as possible.

2K12/ESWE/11

The C++ Programming Environment in UNIX


The best way to learn a programming language is to try writing programs and test them on a computer! To do this, we need several pieces of software:

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.)

Introduction to C++ Programming


C++ language Facilitates structured and disciplined approach to computer program design

Following several examples

Illustrate many important features of C++ Each analyzed one statement at a time
2K12/ESWE/11

Structured programming Object-oriented programming

Basics of a Typical C++ Environment


Phases of C++ Programs: Edit Preprocess Compile Link Load Execute

Basics of a Typical C++ Environment


Input/output cin
Standard input stream Normally keyboard

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.

Decision Making: Equality and Relational Operators


if structure
Make decision based on truth or falsity of condition If condition met, body executed Else, body not executed

2K12/ESWE/11

Equality and relational operators Equality operators Same level of precedence Relational operators Same level of precedence Associate left to right

Decision Making: Equality and Relational Operators


Sta nd a rd a lg eb ra ic eq ua lity op era tor or rela tiona l op era tor C++ eq ua lity or rela tiona l op era tor Exa m p le of C++ c ond ition Mea ning of C++ c ond ition

Relational operators > <

> < >= <=

x > y x < y x >= y x <= y

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

x is equal to y x is not equal to y

2K12/ESWE/11

Вам также может понравиться