Cheatography
https://cheatography.com
CPP Cheat Sheet for ENGN 150 Midterm
Basic Operators
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
= |
Assign to Variable/Set equal to |
% |
Modulus (Int repersentation of remainder) |
++var |
increase before evaluation |
var++ |
increase after evaluation |
condition ? result : alternative; |
short form of if-like structure |
Input/Output
Cin |
cin >> number; |
Cout |
cout << "Enter an integer: "; |
Legal C++ Identifiers (The Rules for Var Names)
- Consists of letters (A-Z, Upper and Lowercase)
- Digits (NO DECIMALS)
- Underscores
- No Special Characters or Spaces
- No C++ Keywords by itself
- Must be Unique within it's namespace
- Case Sensitive |
Example Switch Case
switch (operation) {
case 0:
// Things
break;
case 1:
// Things
break;
case 2:
// Things
break;
default:
// Things
break;
}
|
Operation must be an Int, Char, or Boolean. Case: must reflect that datatype.
Example If Else
if ( conditional ) {
// do something
}
else if ( another_conditional ) {
// do something else
}
else {
// do something as default
}
|
Example For Sentence
for ( init; test; command ) {
// do something
}
|
"break;" and "continue;" identical effects.
Example Do While Sentence
do {
//do something
} while (bool expression);
|
Pointers
int *ptr; |
Pointer definition |
ptr = &var1; |
ptr set to address of var1 |
var2 = *ptr; |
set var2 to value of var1 |
Struct Example
// Declare Structure
struct Order {
int modelNumber;
string phone;
float cost;
};
// Main Function, define the structure variable
int main( ) {
Order item1;
// Give values to structure variables
item1.modelNumber = 10;
item1.phone = "Samsung Galaxy S";
item1.cost = 1099.99;
// Display/Output structure members
cout << "Order, " << item1.phone << item1.modelNumber;
cout << "This item costs: $" << part1.cost << endl;
}
// Output:
// Order, Samsung Galaxy S10
// This item costs: $1099.99
|
|
|
Boolean Logic
== |
Test of Equality |
!= |
Test of Non-Equality |
< |
Less Than |
> |
Greater Than |
<= |
Less Than or Equal |
>= |
Greater Than or Equal |
! |
NOT |
&& |
AND |
|| |
OR |
Boolean expressions in C++ are evaluated left-to-right!
Data Types
int |
3 |
float |
3.0f |
double |
3.0 |
char |
'a' |
string |
"Hello World!" |
bool |
true/false |
File Input / Output
#include <fstream.h>
ifstream file; //read buffer
ofstream file; //write buffer
file.open ("filename", [file mode constant]);
//Test if the file was created
if(fs.is_open()) if(fs)
//Reads/Writes like cin and cout
file >> var; //Read
file << ''Text: "<< var << endl; //Write
//Read Entire line
getline (file,String);
//Read until it arrives at the end of file
while(file.eof())
//Detect if the read/write fail
if(file.fail())
//Close File
file.close();
|
Procedures
//Declaration
void ProcedureName()
{
// do something
}
//Call to procedure
ProcedureName();
|
In the procedures we don't receive variables and don't return other variable.
Functions
//Declaration
[returnType] functionName ( [input1Type input1Name, input2Type input2Name, ....] )
{
// do something
return value; // value must be of type returnType
}
//Call to function
[returntype var =] functionName ([input1Type input1Name, input2Type input2Name, ....])
|
We have two methods to create and call functions:
passed with values and passed for reference.
Pass by reference: we put & before variable in the declaration.
ReturnType must be a Datatype
Cannot return more than one thing
Dynamic Array Template
dataType *nameOfTheDynamicArray;
nameOfTheDynamicArray = new dataType[numofelements];
nameOfTheDynamicArray[0] = value;
delete [] nameOfTheDynamicArray;
|
"new" function allows dynamic array to change it size, since they are parameters.
"delete" will delete everything stored in the dynamic array.
enum example
enum gasType {gas, diesel};
gasType gasT;
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets