Note 1: Reviews the basic C++ code statements from CSCI 504
Text Book:
Programming in C++ (Chapter 1 to Chapter 13)
Data Type Table:
Data
Type |
Byte |
Digits |
Range
Value |
char |
1 |
|
-128 ~
127 |
unsigned
char |
1 |
|
0 ~ 255 |
short |
2 |
5 |
-32,768
~ 32,767 |
unsigned
short |
2 |
5 |
0 ~
65,535 |
int |
2 / 4 |
5 |
-32,768
~ 32,767 |
unsigned
int |
2 / 4 |
5 |
0 ~
65,535 |
long |
4 |
10 |
-2,147,483,648
~ 2,147,483,647 |
unsigned
long |
4 |
10 |
0 ~
4294967295 |
float |
4 |
¡Ö 7 / 8 |
3.4 x 10-38
~ 3.4 x 1038 |
double |
8 |
¡Ö 15 |
1.7 x 10-308
~ 1.7 x 10308 |
Long
double |
10 |
¡Ö 19 |
3.4 x 10-4932
~ 1.1 x 104932 |
Input Data and Output Data:
Input data:
cin >> variable;
//Read input value for variable
cin.get(ch);
//Read input value (one character) for ch
cin.ignore(100, ¡®\n¡¯);
//Skip characters in the input stream
cin.getline(inputStr, 100);
//Read and stores an entire input line
getline(cin, inputStr,100);
//Read and stores an entire input line
Output data:
cout << ¡°Variable is ¡°
<< variable; //Print out ¡°Variable is ¡° and value of variable
File Input and Output:
#include<fstream>
//Header file for file stream
Input to file:
ifstream inData;
//Declaring file stream for input to file
inData.open(¡°walk.dat¡±);
//Open file walk.dat in current directory
inData.close(
);
//Close the file
Output to file:
ofstream outData;
//Declaring file stream for output to file
outData.open(¡°a:\\results.dat¡±); //Open file results.dat in a drive (a:\\) directory
outData.close( ); //Close the file
Conditions Control Statements:
If statements:
if (expression) //Expression controls the program to enter statement1
//or statemetn2
statement1;
else
statement2;
Nested if statements:
if (expression1) //Expression1 controls the program to enter statement1
statement1;
else if (expression2) //Expression2 controls the program to enter statement2
statement2;
else if (expression3) //Expression3 controls the program to enter statement3
statement3;
.
.
else //Other conditions the program enter statementN;
statementN;
Switch statements:
switch(variable) //Using variable¡¯s value to choose which statements for
//the program to enter
{
case 1: statement1;
break;
case 2: statement2;
break;
.
.
default: statementN;
}
Looping:
While statement:
//Condition expression is true do the statemets until it is fault to end the loop
while (condition expression)
{
statements;
}
Do ¡ while statement:
//Do statements once then check condition expression; if condition expression is
//true do the statements again until it is fault to end the loop
do
{
statements;
}while (condition expression);
For statement:
//Enter the for loop first initilaize variables, then check condition expression; if it
//is true enter the for loop do the statements, then do expression; until it is fault to
//end the loop
for(initialize; condition expression; expression)
{
statements;
}
Functions:
Function Prototype:
void FunctionName (Argument List); //void funciton which is not return value
datatype FunctionName(Argument List); //datatype function which is return same
//datatype value
Fuction Call:
FunctionName(Argument List);
Variable = FunctionName(Argument List); //variable must same data type as
//FunctionName
Function:
void FunctionName(parameter1, parameter2, parameter3) //funciton heading
{
statements;
}
short FunctionName(parameter1, parameter2, parameter3) //function heading
{
statements;
return value;
}
void Example ( short & parameter1, //a reference parameter
int parameter2, //a value parameter
float parameter3) //another value parameter
The usage of arguments and parameters table
Item |
Usage |
Argument |
Appears in the function call. The corresponding parameter may be either a reference or a value parameter. |
Value parameter |
Appears in a function heading. Receives a copy of the value of the corresponding argument. |
Reference parameter |
Appears in a function heading. Receives the address of the corresponding argument. |
C++ Operators:
Combined assignment operators:
+ = Add and assign
- = Subtract and assign
* = Multiply and assign
/ = Divide and assign
Increment and decrement operators:
++ Pre-increment Ex: ++variable
++ Post-increment Ex: variable++
- - Pre-decrement Ex: - -variable
- - Post-decrement Ex: variable - -
Bitwise operators (integer operands only):
<< Left shift
>> Right shift
& Bitwise AND
| Bitwise OR
^ Bitwise EXCLUSIVE OR
~ Complement (invert all bits)
More combined assignment operators (integer operands only):
%= Modulus and assign
<<= Shift left and assign
>>= Shift right and assign
&= Bitwise AND and assign
| = Bitwise OR and assign
^ = Bitwis EXCLUSIVE OR and assign
Other operators:
( ) Cast
sizeof Size of operand in bytes Form: sizeof Expr or sizeof (Type)
? : Conditional operator Form: Expr1 ? Expr2 : Expr3
Operator Precedence (highest to lowest)
Operator |
Associativity |
Note |
( ) ++ - - |
Left to right Right to left |
Function call and function-styple cast ++ and - - as postfix operators |
++ - - ! Unary + Unary ¨C (cast) sizeof |
Right to left Right to left |
++ and - - as prefix operators |
* / % |
Left to right |
|
+ - |
Left to right |
|
< <= > >= |
Left to right |
|
= = != |
Left to right |
|
&& |
Left to right |
|
| | |
Left to right |
|
? : |
Right to left |
|
= + = - = * = / = |
Right to left |
|
Structure:
Struct Declaration:
struct TypeName //Define structure name and members
{
Datatype MemberName; //Define structure member
Datatype MemberName; //Define structure member
StructType MemberName; //Define structure member
:
};
Structure Delcare and Access Data Method Table:
Structure Type Variable |
Structure Delcare |
Access Method |
Single Structure Variable |
student record |
record.Last |
Structure Array |
student record [n] |
record [i].Last |
Note: student is struct type, record is variable name, record[n] is student type array, Last is member of student structure
Array:
Declaration:
//One-dimensional array
DataType ArrayName[Constant Integer Expression] ;
//Two-dimensional array
DataType ArrayName[ConstIntExpression][ ConstIntExpression] ;
¡ü ¡ü
Rows Columns
Aggregate Operations Table for Array and Structs:
Aggregate Operation |
Arrays |
Structs |
I / O |
No(except C strings) |
No |
Assignment |
No |
Yes |
Arithmetic |
No |
No |
Comparison |
No |
No |
Argument passage |
Yes, by reference |
Yes, by value or by reference |
Return as a function¡¯s return value |
No |
Yes |
Pass by Value and Pass by Reference:
Argument Type Table:
|
Pass by Value |
Pass by Reference |
Constant |
X |
|
Variable |
X |
X |
Array Name |
|
|
Array Elements |
X |
X |
Expression |
X |
|
Function Call |
X |
|