Getting Started
hello.cpp
Compiling and running
```shell script $ g++ hello.cpp -o hello $ ./hello Hello CheatSheets
## Variables
```cpp
int number = 5; // Integer
float f = 0.95; // Floating number
double PI = 3.14159; // Floating number
char yes = 'Y'; // Character
std::string s = "ME"; // String (text)
bool isRight = true; // Boolean
// Constants
const float RATE = 0.8;
Primitive Data Types
| Data Type | Size | Range |
|---|---|---|
int |
4 bytes | -2^31^ ^to^ 2^31^-1 |
float |
4 bytes | N/A |
double |
8 bytes | N/A |
char |
1 byte | -128 ^to^ 127 |
bool |
1 byte | true / false |
void |
N/A | N/A |
wchar_t |
2 ^or^ 4 bytes | 1 wide character |
User Input
Swap
Comments
If statement
Loops
Functions
#include <iostream>
void hello(); // Declaring
int main() { // main function
hello(); // Calling
}
void hello() { // Defining
std::cout << "Hello CheatSheets!\n";
}
References
int i = 1;
int& ri = i; // ri is a reference to i
ri = 2; // i is now changed to 2
std::cout << "i=" << i;
i = 3; // i is now changed to 3
std::cout << "ri=" << ri;
ri and i refer to the same memory location.
Namespaces
#include <iostream>
namespace ns1 {int val(){return 5;}}
using namespace ns1;
using namespace std;
int main()
{
cout << val();
}
Namespaces allow global identifiers under a name