MIterms
MIterms
Terminology:
C++ Programs
C++ Program Basics:
Symbols
Special Symbols:
Identifiers
Using Identifiers:
Here is a list of all the reserved words in Standard C++, and a few predefined identifiers for the
sake of comparison.
There is a distinction between reserved words and predefined identifiers, which are sometimes
collectively referred to as keywords. Nevertheless, be aware that the terminology is nonstandard. As
an illustration, some authors use keyword in the same sense that others use reserved word.
The reserved words of C++ may be conveniently placed into several groups. In the first group, we
put those that were also present in the C programming language and have been carried over into
C++. There are 32 such reserved words:
auto const double float int short struct unsigned
break continue else for long signed switch void
case default enum goto register sizeof typedef volatile
char do extern if return static union while
There are another 30 reserved words that were not in C, are therefore new to C++:
asm dynamic_cast namespace reinterpret_cast try
bool explicit new static_cast typeid
catch false operator template typename
class friend private this using
const_cast inline public throw virtual
delete mutable protected true wchar_t
The following 11 C++ reserved words are not essential when the standard ASCII character set is
being used, but they have been added to provide more readable alternatives for some of the C++
operators, and also to facilitate programming with character sets that lack characters needed by C+
+.
and bitand compl not_eq or_eq xor_eq
and_eq bitor not or xor
Note that your particular compiler may not be completely up-to-date, which means that some (and
possibly many) of the reserved words in the preceding two groups may not yet be implemented.
Beginning C++ programmers are sometimes confused by the difference between the two
terms reserved word and predefined identifier, and some potential for confusion.
One of the difficulties is that some keywords that one might "expect" to be reserved words are not.
The keyword main is a prime example, and others include things like the endl manipulator and
other keywords from the vast collection of C++ libraries.
For example, you could declare a variable called main inside your main function, initialize it, and
then print out its value (but ONLY do that to verify that you can!). On the other hand, you
could not do this with a variable named else. The difference is that else is a reserved word,
while main is "only" a predefined identifier.
Data Types
Using Data Types:
1. char
2. short
3. int
4. long
5. bool
6. unsigned char
7. unsigned short
8. unsigned int
9. unsigned long
1. Two values: true and false, called logical (or Boolean) values
2. An expression that evaluates to true or false called logical (Boolean) expression
3. In C++, bool, true, and false are reserved words
4. Older compilers do not include bool data type
1. float
2. double
3. long double
string Type:
Expressions
Using Expressions:
Implicit type coercion: value of one type is automatically (implicitly) changed to another
type
Explicit type conversion: use cast operator (two forms)
1. static_cast<dataTypeName>(expression): more stable
2. dataType(expression): C-style typecasting
Name and define data type to store data for each memory location
Declaration statement used to allocate memory
Variables: memory cells whose contents can be modified during program execution
Possible to declare multiple variables in same statement (must be same type)
Named Constant: memory location whose data cannot change during program execution
Using named constant simplifies code modification - change in declaration statement affects
code globally
In C++, const is reserved word
1. Name
2. Type
3. Size
4. Value
Syntax for declaring variables to allocate memory:
dataType identifier;
Examples:
int myVar; //declares one int variable
float myFloat, myFloat2, myFloat3; //declares three float variables
Syntax for declaring named constants to allocate memory (uses keyword const and must assign
value):
const dataType identifier = value;
Example:
const double PI = 3.14;
Storing Data into Variables (declaring and initializing variables):
int myVar = 10; //allocates memory for variable myVar, and assigns value of 10 to myVar
//declares two double variables (myDouble and myDouble3), initializes myDouble2 with value
of 25.5
double myDouble, myDouble2=25.5, myDouble3;
Input (Read) Statement:
cin >> feet >> inches; //input multiple values into multiple memory locations with single
statement
Problems Using Mixed Values:
//either way, x = 1
++x;
x++;
Output
Using Output:
Output statement written with cout and stream insertion operator (<<)
Standard output device normally monitor screen
To move cursor to beginning of next line requires either predefined word endl, with insertion
operator (<<), or escape sequence '\n'
In C++, \ called escape character and \n called newline escape sequence
To include cin and cout, appropriate header file must be included:
#include <iostream>
Escape Sequence Examples:
\n //Newline: cursor moves to beginning of next line
\t //Tab: cursor moves to next tab stop
\b //Backspace: cursor moves one space left
\r //Return: cursor moves to beginning of current line (not next line)
\\ //Backslash: backslash printed
\' //Single quotation: single quotation mark printed
\" //Double quotation: double quotation mark printed
Preprocessor Directives:
In C++, most operations and functions must be imported from collection of libraries
For example, for input/output, header file iostream must be included
Preprocessor directives (i.e., names of header files) tell computer where to locate required
libraries
Preprocessor directives processed by program called preprocessor--before program goes
through compiler
Preprocessor commands begin with #--must appear at beginning of program
General syntax:
#include<headerFileName>
Using cin/cout and namespace:
Two Parts:
1. Preprocessor directives (include files)
2. The program
Preprocessor directives and program statements constitute C++ source code
Source code saved in file with file extension .cpp
Compiler generates object code (saved in file with .obj file extension)
Executable code produced (saved in file with .exe file extension)
Every C++ program MUST have one and ONLY one function main()
Basic parts of function main() (as with any other C++ function):
1. Function Heading
2. Function Body
Syntax errors found in compilation (i.e., by compiler)
Use of Blanks:
o Program may not run, or run improperly, even with syntax errors removed
o For example, 2 + 3 * 5 and (2 + 3) * 5
both are syntactically correct expressions, with different meanings
Documentation:
User Prompts:
x *= y; //equivalent to x = x * y;
Simple Program:
Begin program with comments for documentation
Include header files
Declare named constants, if any
Write definition of function main()
Summary: