Cheatography

Engineering 150 Midterm Cheat Sheet Cheat Sheet by sukablukagyat

CPP Cheat Sheet for ENGN 150 Midterm

Basic Operators

 + Addition - Subtra­ction * Multip­lic­ation / Division = Assign to Variab­le/Set equal to % Modulus (Int repers­ent­ation of remainder) ++var increase before evaluation var++ increase after evaluation condition ? result : altern­­ative; short form of if-like structure

Input/­Output

 Cin cin >> number; Cout cout << "­­Enter an integer: ";

Legal C++ Identi­fiers (The Rules for Var Names)

 - Consists of letters (A-Z, Upper and Lowercase) - Digits (NO DECIMALS) - Unders­cores - 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 }``````
"­­br­e­a­k;­­" and "­­co­n­t­in­­ue;­­" identical effects.

Example Do While Sentence

 ``````do {  ­ ­ //do something } while (bool expres­sion);``````

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 modelN­umber;     string phone;     float cost; }; // Main Function, define the structure variable int main( ) {     Order item1;     // Give values to structure variables  ­ ­ ­ ­ite­m1.m­od­elN­umber = 10;     item1.phone = "­Samsung Galaxy S";     item1.cost = 1099.99;     // Displa­y/O­utput structure members  ­ ­ ­ cout << "­Order, " << item1.p­hone << item1.m­od­elN­umber;     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-Eq­uality < Less Than > Greater Than <= Less Than or Equal >= Greater Than or Equal ! NOT && AND || OR
Boolean expres­­sions in C++ are evaluated left-t­­o-­r­ight!

Data Types

 int 3 float 3.0f double 3.0 char 'a' string "­­Hello World!­­" bool true/false

File Input / Output

 ``````#include 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 declar­­ation.
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 parame­ters.

"­del­ete­" will delete everything stored in the dynamic array.

enum example

 ``````enum gasType {gas, diesel}; gasType gasT;``````