Chapter 2 intro to c++ Flashcards

1
Q

parts of a c++ program messy

A
//comment
# include  (#preprocessor directive(
using namespace std; (which namespace to use)
int main() ( beginning of function called main)
{      (beginning of block for main)
      cout  << "hello"; (output statement)
      return 0; (send 0 to operating system)
}      (end of block for main)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

parts of a c++ program

A
//comment
#beginning of preprocessor directive
< > include file name in #include
( ) used when naming a function
{ } encloses a group of statements
" " encloses a string of characters
; end of a programming statement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

cout object

A

display output on computer screen
You use the stream insertion operator &laquo_space;to send output to cout:
cout &laquo_space;“Programming is fun!”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

cout object2

A
Can be used to send more than one item to cout:
	cout << "Hello " << "there!";
Or:
	cout << "Hello ";
	cout << "there!";
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

endl manipulator

cout “hello” «< “world”;

A

«< “hello \n”

cout &laquo_space;“world”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

the #include directive

A

Inserts the contents of another file into the program
This is a preprocessor directive, not part of C++ language
#include lines not seen by compiler
Do not place a semicolon at end of #include line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

variable

A

a storage location in memory
Has a name and a type of data it can hold
Must be defined before it can be used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

literal

A

a value that is written in to a program’s code
such as a string literal”hello world”
or simply an integer literal like 11

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

identifier

A

An identifier is a programmer-defined name for some part of a program: variables, functions, etc.
C++ keywords cannot be used as identifiers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

identifier rules

A

variable names should be meaningful
first character must be alphabetic character or underscore_
after first character you may use both cases, numbers, or underscores
NO WHITE SPACE
upper and lowers cases are distinct from each other.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Integer data types

A

integers can hold just that, integers: positive and negative whole numbers.
variables of same type may be defined on same( separated by commas, or different lines
different types must be on different lines

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

integer literals

A

An integer literal is an integer value that is typed into a program’s code
itemsOrdered = 15;
15 is an integer literal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

integer literals 2

A

Integer literals are stored in memory as ints by default
To store an integer constant in a long memory location, put ‘L’ at the end of the number: 1234L
Constants that begin with ‘0’ (zero) are base 8: 075
Constants that begin with ‘0x’ are base 16: 0x75A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

char data type

A

Used to hold characters or very small integer values
Usually 1 byte of memory
Numeric value of character from the character set is stored in memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

char literals

A

must be enclosed in single quotation marks

‘A’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

character string

A

A series of characters in consecutive memory locations:
“Hello”
Stored with the null terminator, \0, at the end

Comprised of the characters between the “ “

17
Q

string class

A
Special data type supports working with strings
 #include 
Can define string variables in programs:
string firstName, lastName;
Can receive values with assignment operator:
firstName = "George";
lastName = "Washington";
Can be displayed via cout
cout << firstName << " " << lastName;
18
Q

floating point data types

A

The floating-point data types are:floatdoublelong double
They can hold real numbers such as:
12.45 -3.8
Stored in a form similar to scientific notation
All floating-point numbers are signed

19
Q

types of floating point

A

single precision: 4 bytes
double: 8 bytes
long double: 8 bytes

20
Q

floating point literal

A
Can be represented in
Fixed point (decimal) notation:
	31.4159			0.0000625
E notation:
	3.14159E1			6.25e-5
Are double by default
Can be forced to be float (3.14159f) or long double (0.0000625L)
21
Q

bool data type

A

boolean
represents value of true or false
bool variables are stored as small integers
false is represented by 0, true by 1

22
Q

determining size of data type

A
The sizeof operator gives the size of any data type or variable:
	double amount;
	cout << "A double is stored in "
  		  << sizeof(double) << "bytes\n";
	cout << "Variable amount is stored in "
  		  << sizeof(amount) 
       << "bytes\n";
23
Q

variable assignment

A

An assignment statement uses the = operator to store a value in a variable.
boxes = 15
this stores the value of 15 to the boxes variables
variable must be left side

24
Q

variable initialization

A

To initialize a variable means to assign it a value when it is defined:
int boxes = 15
may initialize some, none or all variables

25
scope of a variable
The scope of a variable: the part of the program in which the variable can be accessed A variable cannot be used before it is defined
26
arithmetic operations
``` Used for performing numeric calculations C++ has unary, binary, and ternary operators: unary (1 operand) -5 binary (2 operands) 13 - 7 ternary (3 operands) exp1 ? exp2 : exp3 ```
27
binary operators
``` addition + subtraction - multiplication * division / modulus(remainder) % ```
28
division
division) operator performs integer division if both operands are integers If either operand is floating point, the result is floating point
29
modulus
% (modulus) operator computes the remainder resulting from integer division % requires integers for both operands
30
comments
``` Used to document parts of the program Intended for persons reading the source code of the program: Indicate the purpose of the program Describe the use of variables Explain complex sections of code Are ignored by the compiler ```
31
single line comment
line begins with //
32
multiple line comment
begins with /* end with */ same line or spanning multiple lines
33
Named constants
constant variable): variable whose content cannot be changed during program execution Used for representing constant values with descriptive names: const double TAX_RATE = 0.0675; const int NUM_STATES = 50; Often named in uppercase letters
34
programming style
The visual organization of the source code Includes the use of spaces, tabs, and blank lines Does not affect the syntax of the program Affects the readability of the source code
35
programming style 2
Common elements to improve readability: Braces { } aligned vertically Indentation of statements within a set of braces Blank lines between declaration and other statements Long statements wrapped over multiple lines with aligned operators
36
prestandard c++
Older-style C++ programs: Use .h at end of header files: #include Use #define preprocessor directive instead of const definitions Do not use using namespace convention May not compile with a standard C++ compiler