Documente Academic
Documente Profesional
Documente Cultură
#include "stdafx.h" #include <iostream> #include <string> using namespace std; int main() { return 0; }
Print: cout << System Beep: cout << "\a"; User Input: cin >> Convert String to Int: intname = atio(stringname); Wait for User Input at end of program
char f; cin>>f ; return 0;
Functions:
void myfunc(int anint = 2, int int2, bool bool1); // Delares myfunc before main and allows it to pass 2 ints and and a boolean. Also creates a default value for anint. If no value is passed trhough it will = 2. int main() { int x = 5; bool alive; alive = true; myfunc(x, 0 ,
alive); //Call myfunc and pass the integer x, nothing for in2 and boolean alive
, bool alive2) //Recive x from main and calls it y, recives 0 and does nothing, revives alive and names it alive2
cout << y << endl; cout << alive2; } system("cls"); //Clears Screen
Variable Types: Bool: True or False Float: Smaller Decimal Double: Large Decimal Int: Integer Char: One single character (place char in (x) ) Unsigned or Signed Variables Const: Always same Operators: +, -, *, /, % (Mod: Remainder), ++, --, +=, -=, *=, /=, %= ?:
int a = 10; int b = 10; (a = b) ? cout << "True" : cout << "Not True";
&(plus variable name): Get the memory address of the variable. Example (cout << &x;) Logical Operators: &&: And ||: Or !: Not !=: Not Equal Use () to enclose bits of code in if statements (Order of operations) Scope and Visibility
Scope: Variables limited to code block where it was created. Visibility: {int x = 5; {cout << x; //its 5 Int x = 20; Cout << x //its 20 } Cout << x ;} //Its 5 Loops Variables work in sub-scopes but if overridden becomes new then when out of scope becomes old value.
break ; // Stops loop continue ; // Stops loop and returns to beginning For Loop
for (declare one or many variables of same type ; Boolean Evaluation ; Take an action) { } for (double counter = 0 ;counter <= 100 { cout << Hello World << endl ; } ; counter++ )
While Loop
while (x == 1) { cout << number << endl ; }
Do While
do { cout << number << endl ; } while (x == 1)
switch (choice) { case 1: //action; break; default: //do if all else fails; }
Strings
#include <string> // include for accces of class string int main() { string y = "ABCD"; // create a string with ABCD cout << y; // Print ABCD To screen }
Spread Program over Multiple Files Use header(.h) files to for all of the definitions of classes, functions etc. Use C++(.cpp) files for the actual code for the functions. However, by default, header files are not compiled therefore you must include all of the definitions of the classes, functions etc. in your .cpp file. This is done by using the include keyword then putting the name of the header in quotes. Again this should be done at the beginning of .ccp files.
#include "myheaderfilename.h"
MAIN.cpp
#include "myheader.h" #include "stdafx.h" #include <iostream> using namespace std; myclass::sayhi() myfunc();
myheader.h
include <iostream> using namespace std; class myclass { public: void sayhi(); }; void myfunc();
mycode.cpp
#include "myheader.h" #include "stdafx.h" void myclass::sayhi() { cout << "in my class"; } void myfunc() { cout << "in my func"; }
Pointers:
Creating a Pointer:
int x = 0; //Declare A variable int * ptr; //Create a pointer of same type (Use * after type) ptr = & x; //Use & symbol to get memory address of variable and put it in the pointer cout << *x; //Will print out 0 myfunc(ptr); //Call myfunc and send the pointer for x. use *x to acces the variable x in myfunc
Random:
#include <cstdlib> #include <time.h> for ( int x = 0 ; x < 100 ; x++) { srand((unsigned)time(0)); int randomint = rand(); cout << randomint << endl; }