BASIC ELEMENTS OF C++ Flashcards
A sequence of statements whose objective is to accomplish a task
Computer Program
The process of planning and creating a program
Programming
collection of statements
Subprogram or Function
rules that specify which statements (instructions) are legal or valid
Syntax rules
determine the meaning of the instructions
Semantic rules
a set of rules, symbols, and special words
Programming language
Two types of Comment Line
Single line and Multiple line
A single line begins with what symbol
//
Multiple line is enclosed with a symbol
/* and */
the smallest individual unit of a program written in any language
token
C++ tokens include
special symbols, word symbols, and identifiers
Special symbols in C++ include:
+ - * /
. ; ? ,
<= != == >=
Cannot be redefined within a program
Reserved word symbols or keywords
Cannot be used for anything other than their intended use
Reserved word symbols or keywords
Examples of Reserved Words
int
float
double
char
const
void
return
name of something that appears in a program
identifier
Consists of letters, digits, and the underscore character (_)
identifier
identifiers must begin with
a letter or underscore
TRUE OR FALSE
C++ is case sensitive
TRUE
TRUE OR FALSE
NUMBER is the same as number
FALSE
Two predefined identifiers
cout and cin
TRUE OR FALSE
Unlike reserved words, predefined identifiers may be redefined, but it is not a good idea
TRUE
3 Legal identifiers in C++
first
conversion
payRate
Include blanks, tabs, and newline characters
Whitespaces
it separates special symbols, reserved words, and identifiers
whitespaces
set of values together with a set of allowed operations
data Type
C++ data types fall into three categories:
Simple data type
Structured data type
Pointers
numbers without a decimal
integers
Simple Data Types
Integral
Floating-point
Enumeration
Integral can further be categorized:
char, short, int, long, bool, unsigned char, unsigned short, unsigned int, unsigned long
simple data with decimal numbers
Floating-point
a user-defined data type
Enumeration
to manipulate logical expressions
bool Data Type
smallest integral data type
Data type char
It is used for single characters: letters, digits, and special symbols
char Data Type
C++ uses scientific notation to represent real numbers
Floating-Point Data Types
ordering of characters based on the character set code
Collating sequence
are only used for separating items in a list
Commas
represents any real number
float and double
Maximum number of significant digits (decimal places) for float values
6 or 7
Maximum number of significant digits for double
15
maximum number of significant digits
Precision
float values are called
single precision
double values are called
double precision
TRUE OR FALSE
A comma cannot be used within an integer
TRUE
Two values of bool type or 2 Logical values
true or false
TRUE OR FALSE
In Data Type char, each character is enclosed in single quotes
TRUE
example:
‘A’, ‘a’, ‘0’, ‘*’, ‘+’, ‘$’, ‘&’
TRUE OR FALSE
A blank space is not a character
FALSE
IT IS A CHARACTER
Written ‘ ‘, with a space left between the single quotes
ASCII
American Standard Code for Information Interchange
TRUE OR FALSE
Characters have a predefined ordering based on the ASCII numeric value
TRUE
Syntax rule to declare a variable is:
dataType identifier;
C++ arithmetic operators include:
+ addition
- subtraction (or negation)
* multiplication
/ division
% mod (modulus or remainder)
TRUE OR FALSE
Modulus (%) can only be used with integral data types
TRUE
TRUE OR FALSE
When you use / with integral data types, the integral result is rounded off
FALSE
the result is truncated (no rounding)
contain values and arithmetic operators
Arithmetic expressions
the numbers appearing in the expressions
Operands
can be unary (one operand) or binary (two operands)
Operators
have the same level of precedence and are evaluated last
*, /, and %
+ and -
TRUE OR FALSE
When operators are on the same level
Operations are performed from left to right (associativity)
TRUE
all operands are integers
Integral expression
all operands are floating-point
Floating-point expression
2 types of expression
Integral expression and Floating-point expression
Has operands of different data types
Mixed expression
If operator has same types of operands
- The operator is evaluated according to the type of the operands
- If operator has both types of operands
…
Integer is changed to floating-point
- Operator is evaluated
- Result is floating-point
…
Type Conversion (Casting)
Implicit type coercion
Cast operator (also called type conversion or type casting)
when the value of one type is automatically changed to another type
Implicit type coercion
provides explicit type conversion
Cast operator (also called type conversion or type casting)
a programmer-defined type supplied in ANSI/ISO Standard C++ library
Data type string
a sequence of zero or more characters enclosed in double quotation marks
string
a string with no characters
null (or empty) string
memory location whose content cannot change during execution
Named constant
Syntax to declare a named constant
const dataType identifier = value;
TRUE OR FALSE
In C++, const is not a reserved word
FALSE
bcs const is a reserved word
memory location whose content may change during execution
Variable
Syntax to declare one or multiple variables
dataType identifier, identifier, … ;
Ways to place data into a variable
Use C++’s assignment statement
Use input (read) statements
The assignment statement takes the form:
variable = expression;
In C++, = is called the
assignment operator
The assignment operator is evaluated from
right to left
is used with»_space; to gather one or more inputs
cin
The stream extraction operator
> >
increase variable by 1
Increment operator (++)
Pre-increment:
++variable
Post-increment:
variable++
decrease variable by 1
Decrement operator: (–)
Pre-decrement:
–variable
Post-decrement:
variable–
The syntax of cout and «_space;is:
cout «_space;expression or manipulator «_space;expression or manipulator … ;
used to format the output
manipulator
The new line character (new line escape sequence)
‘\n’
cursor moves to the beginning of the next line
Newline \n
cursor moves to the next tab stop
Tab \t
cursor moves one space to the left
Backspace \b
cursor moves to the beginning of the current line (not the next line)
Return \r
Backslash
\
Singe quotation
'
double quotation
"
All preprocessor commands begin with
#
Every library has a name and is referred to by
a header file
are processed by the preprocessor program
Preprocessor directives
Syntax to include a header file
include <headerFileName></headerFileName>
a collection of functions, one of which is the function main
C++ program
The syntax of the function main used in this book has this form:
int main ()
{
statement_1
.
.
.
statement_n
return 0;
}
The first line of the function main is called
the heading of the function: int main ()
The statements enclosed between the curly braces ({ and }) form the
body of the function
Declaration statements declare things, such as
variables
statements that perform calculations, manipulate data, create output, accept input, etc.
Executable statements
will identify the syntax errors
compiler
indicate what is legal and what is not legal
syntax rules
are also used to separate reserved words and identifiers from each other and from other symbols
Blanks
TRUE OR FALSE
Blanks must NEVER appear within a reserved word or identifier
TRUE
All C++ statements end with a semicolon also called a
statement terminator
{ and } are not C++ statements but can be regarded as
delimiters
set of rules that gives meaning to a language
semantics
can be self-documenting
indentifiers
executable statements that inform the user what to do
Prompt lines
Two forms of assignment
Simple and compound
Compound operators
+=, -=, *=, /=, %=
is a collection of functions, one of which is always called main
C++ program
consist of letters, digits, and underscores, and begin with a letter or an underscore
Identifiers
The arithmetic operators in C++ are
addition (+), subtraction (-), multiplication (*), division (/), and modulus (%)
are evaluated using the precedence associativity rules
Arithmetic expressions
All operands in an integral expression are
integers
All operands in a floating-point expression are
decimal numbers
expression contains both integers and decimal numbers
mixed expression
Use the ________ to explicitly convert values from one data type to another
cast operator
TRUE OR FALSE
A named constant is initialized when declared
TRUE
TRUE OR FALSE
All variables must be declared before used
TRUE
Use ___ and the ______________»_space; to input from the standard input device
cin and the stream extraction operator
> >
Use ___ and the ______________ «_space;to output from the standard output device
cout and the stream insertion operator
«
are processed before the program goes through the compiler
Preprocessor commands
A file containing a C++ program usually ends with
the extension .cpp
TRUE OR FALSE
A semicolon is placed at the end of these commands
FALSE
No semicolon is placed at the end of these commands
comprised of preprocessor directives and program statements
Source code
results when object code is linked with the system resources
Executable code
Blanks must never appear within a
reserved word or identifier
All C++ statements end with
a semicolon
TRUE OR FALSE
{ and } are not C++ statements
TRUE
Avoid run-together words
Solutions may include:
Capitalizing the beginning of each new word: annualSale
Inserting an underscore just before a new word: annual_sale
Comments should appear in a program to:
Explain the purpose of the program
Identify who wrote it
Explain the purpose of particular statements
causes the insertion point to move to beginning of next line
endl
The stream insertion operator is
«
The stream extraction operator is
> >