Chapter 2
Chapter 2
ü C++ IDE
ü Basic Elements
Ø Keywords (reserved words)
Ø Identifiers
Ø Literals
Ø Comments
ü Data type, Variable and Constants
ü Operators
ü Precedence of Operators
ü Simple Type Conversion
ü Statements
A C++ program has the following structure
P[Comments]
P[Preprocessor directives]
P[Global variable declarations]
P[Prototypes of functions]
P[Definitions of functions]
The complete development cycle in C++ is:
P Write the program,
P Compile the source code,
P Link the program, and
P Run it.
ü Write the program
Ø compiler may have its own built-in text editor, or you may
be using a commercial text editor or word processor that can
produce text files.
ØEx: Wins Notepad, the DOS Edit command, EMACS, &vi.
Ø Many commercial word processors, such as WordPerfect,
Word, and dozens of others, also offer a method for saving
simple text files.
ü Compile the source code
ØYour source code file can't be executed, or run, as a program
can. To turn your source code into a program, you use a
compiler. After your source code is compiled, an object file
is produced. This file is often named with the extension .OBJ.
Ø This is still not an executable program. However, to turn this
into an executable program, you must run your linker.
ü Link the program
ØC++ programs are typically created by linking together one
or more OBJ files with one or more libraries.
P Finally, Run it.
ØPress F9 or use any related command to run the program.
The steps to create an executable file are
1. Create a source code file, with a .CPP extension.
2. Compile the source code into a file with the .OBJ extension.
3. Link your OBJ file with any needed libraries to produce an
executable program.
1: #include <iostream.h>
2:
3: int main ()
4: {
5: cout << "Hello Africa! \n";
6: return 0;
7 :}
On line 1,
ü the file iostream.h is included in the file.
ü The first character is the # symbol, which is a signal to the
preprocessor.
On Line 3
ü begins the actual program with a function named main().
ü Every C++ program has a main() function.
ü When your program starts, main() is called automatically.
On line 4 and 7,
ü All functions begin with an opening brace ({) and end with a
closing brace (}).
ü Everything between the opening and closing braces is
considered a part of the function.
2.4.1. Keywords (reserved words)
P Reserved/Key words have a unique meaning within a C++
program.
P These symbols, the reserved words, must not be used for any
other purposes.
P All reserved words are in lower-case letters.
2.4.2. Identifiers
P An identifier is name associated with a function or data
object and used to refer to that function or data object.
P An identifier must:
Ø Start with a letter or underscore
Ø Consist only of letters, the digits 0-9, or the underscore
symbol _
Ø Not be a reserved word
2.4.2. Identifiers
PValid Identifiers:
Length days_in_year DataSet1 Profit95
Int _Pressure first_one first_1
PInvalid Identifiers: