Basics of a Typical C++ Environment
Program is created in
Editor Disk the editor and stored
on disk.
• Phases of C++ Preprocessor Disk
Preprocessor program
processes the code.
Programs: Compiler creates
Compiler Disk object code and stores
1. Edit it on disk.
Linker links the object
code with the libraries,
2. Preprocessor Linker Disk
creates [Link] and
stores it on disk
3. Compile
Primary
Memory
Loader
4. Link Loader puts program
in memory.
5. Load Disk ..
..
..
6. Execute Primary
Memory
CPU CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
.. executes.
..
..
C++ Programs
• A C++ program is a collection of one or more
subprograms, called functions
• A subprogram or a function is a collection of
statements that, when activated (executed),
accomplishes something
• Every C++ program has a function called main
• The smallest individual unit of a program
written in any language is called a token
Names
• Used to denote language- and programmer-defined elements
within the program
• A valid name is a sequence of
– Letters (upper and lowercase)
– Digits
• A name cannot start with a digit
– Underscores
• A name should not normally start with an underscore
• Names are case sensitive
– MyObject is a different name than MYOBJECT
• There are two kinds of names
– Keywords
– Identifiers
Keywords
• Keywords are words reserved as part of the language -
syntax
Example int, return, float, double
• They cannot be used to name programmer-defined
elements
• They consist of lowercase letters only
• They have special meaning to the compiler
Declarations - Constants and Variables
Constants are data that having unchanging values.
Variables, as their name implies, are data whose values can change
during the course of execution of your program.
For both constants and variables, the name, the data type, and
value must be specified.
Any variable in your program must be defined before it can be used
Data type specifies whether data is integral, real, character or
logical.
Defining Variables
• Variables of the same type can be defined
- On separate lines:
int length;
int width;
unsigned int area;
- On the same line:
int length, width;
unsigned int area;
• Variables of different types must be in different definitions
Declaring Variables
int x; // Declare x to be an
// integer variable;
double radius; // Declare radius to
// be a double variable;
char a; // Declare a to be a
// character variable;
• Variable names correspond to locations in
the computer's memory.
– Every variable has a name, a type, a size and a
value.
– Whenever a new value is placed into a variable, it
replaces the previous value - it is destroyed
– reading variables from memory does not change them
• A visual representation
no1 45
Variable Assignments and Initialization
Assignment
• Uses the = operator
• Has a single variable on the left side and a value (constant,
variable, or expression) on the right side
• Copies (i.e. assigns) the value on the right to the variable on the
left. (An expression is first evaluated)
• Syntax
variable = expression ;
a= 10+20;
item = 12; // constant
Celsius = (Fahrenheit - 32) * 5 / 9; // expression
y = m * x + b; // expression
Assignment Statement
int NewStudents = 6; NewStudents 6
int OldStudents = 21; OldStudents 21
int TotalStudents; TotalStudents ?
TotalStudents = NewStudents + OldStudents;
Variable Assignments and Initialization
Initialization
• Initialize a variable: assign it a value when it is defined:
int length = 12;
Float Pi=3.14
• Or initialize it later via assignment
• Can initialize some or all variables:
int length = 12, width = 5, area;
• A variable cannot be used before it is defined
Increment and Decrement Operators
The “strange”effects appear when the operators are used in an
assignment statement
Operator Name Description
a=++var pre-increment The value in var is incremented by 1
and this new value of var is assigned to a.
a=var++ post-increment The value in var is assigned to
a and then the value in var is
incremented by 1
Similar effects for the decrement operator
Operator Name Description
a=--var pre-decrement The
value in var is
decremented by 1 and
this new value
of var is
assigned to a.
a=var-- post-decrement The value in var
is
assigned to a and then
the value in var is