c++ quick reference guide
TRANSCRIPT
C++ Quick Reference Guide
Basics (General):
Standard Includes:
#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
char f; //returns to main
cin>>f ;
return 0;
}
void myfunc(int y, int , 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
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 ; counter++ )
{
cout << “Hello World” << endl ;
}
While Loop
while (x == 1)
{
cout << number << endl ;
}
Do While
do
{
cout << number << endl ;
}
while (x == 1)
Switch
Use for when you have a lot of if statements
int choice = 0;
cin >> choice;
Variables work in sub-scopes but if overridden becomes
new then when out of scope becomes old value.
switch (choice)
{
case 1:
//action;
break;
default:
//do if all else fails;
}
Classes
Create own variable types.
class bob
{
public:
bob() //This is the constructor function that will be run every time
time a new instance of the class is created
{
//Here you would put the inported variables from the createation
of the instance into the private variables
}
void Attack (); //Define this function
private:
string name;
int health;
};
int main ()
{
return 0;
}
void bob::Attack() // the "::" is called the scope resolution operator
{
//Define this method function
}
Arrays
Create strings of variables of the same type
int x [10] = { 1,2,3,4,5,6,7,8,9,10 }; // Creates the array and
initialize 10 variables
cout << x [0] << endl; // x [0] = 1
cout << x [1] << endl; // x [2] = 2
cout << x [3] << endl; // x [3] = 4
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"
Include all header files in your main.cpp file.
SEE NEXT PAGE FOR EXAMPLE - - - - >
This code is an example of a C++ program over multiple files
Note: #include "stdafx.h" in all source files
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;
}