CPP Cheat Sheet for ENGN 150 Midterm
|
+
|
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
|
|
Cin
|
cin >> number;
|
|
Cout
|
cout << "Enter an integer: ";
|
switch (operation) {
case 0:
// Things
break;
case 1:
// Things
break;
case 2:
// Things
break;
default:
// Things
break;
}
if ( conditional ) {
// do something
}
else if ( another_conditional ) {
// do something else
}
else {
// do something as default
}
for ( init; test; command ) {
// do something
}
do {
//do something
} while (bool expression);
|
int *ptr;
|
Pointer definition
|
|
ptr = &var1;
|
ptr set to address of var1
|
|
var2 = *ptr;
|
set var2 to value of var1
|
// 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
|
==
|
Test of Equality
|
|
!=
|
Test of Non-Equality
|
|
<
|
Less Than
|
|
>
|
Greater Than
|
|
<=
|
Less Than or Equal
|
|
>=
|
Greater Than or Equal
|
|
!
|
NOT
|
|
&&
|
AND
|
|
||
|
OR
|
|
int
|
3
|
|
float
|
3.0f
|
|
double
|
3.0
|
|
char
|
'a'
|
|
string
|
"Hello World!"
|
|
bool
|
true/false
|
#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();
//Declaration
void ProcedureName()
{
// do something
}
//Call to procedure
ProcedureName();
//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, ....])
dataType *nameOfTheDynamicArray;
nameOfTheDynamicArray = new dataType[numofelements];
nameOfTheDynamicArray[0] = value;
delete [] nameOfTheDynamicArray;
enum gasType {gas, diesel};
gasType gasT;
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.