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;
int age {25}; // Since C++11
std::cout << age; // Print 25
Primitive Data Types
Data Type
Size
Range
int
4 bytes
-231 to 231-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
int num;
std::cout << "Type a number: ";
std::cin >> num;
std::cout << "You entered " << num;
Swap
int a = 5, b = 10;
std::swap(a, b);
// Outputs: a=10, b=5
std::cout << "a=" << a << ", b=" << b;
Comments
// A single one line comment in C++
/* This is a multiple line comment
in C++ */
If statement
if (a == 10) {
// do something
}
See: Conditionals
Loops
for (int i = 0; i < 10; i++) {
std::cout << i << "\n";
}
Functions
#include <iostream>
void hello(); // Declaring
int main() { // main function
hello(); // Calling
}
void hello() { // Defining
std::cout << "Hello Vietelligent!\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;
std::array<int, 6> marks = {92, 97, 98, 99, 98, 94};
// Print first element
std::cout << marks[0];
// Change 2th element to 99
marks[1] = 99;
// Take input from the user
std::cin >> marks[2];
Displaying
char ref[5] = {'R', 'e', 'f'};
// Range based for loop
for (const int &n : ref) {
std::cout << std::string(1, n);
}
// Traditional for loop
for (int i = 0; i < sizeof(ref); ++i) {
std::cout << ref[i];
}
for (int n : {1, 2, 3, 4, 5}) {
std::cout << n << " ";
}
// Outputs: 1 2 3 4 5
std::string hello = "Vietelligent.ME";
for (char c: hello)
{
std::cout << c << " ";
}
// Outputs: V i e t e l l i g e n t . M E
Break statements
int password, times = 0;
while (password != 1234) {
if (times++ >= 3) {
std::cout << "Locked!\n";
break;
}
std::cout << "Password: ";
std::cin >> password; // input
}
Several variations
for (int i = 0, j = 2; i < 3; i++, j--){
std::cout << "i=" << i << ",";
std::cout << "j=" << j << ";";
}
// Outputs: i=0,j=2;i=1,j=1;i=2,j=0;
C++ Functions
Arguments & Returns
#include <iostream>
int add(int a, int b) {
return a + b;
}
int main() {
std::cout << add(10, 20);
}
add is a function taking 2 ints and returning int
Overloading
void fun(string a, string b) {
std::cout << a + " " + b;
}
void fun(string a) {
std::cout << a;
}
void fun(int a) {
std::cout << a;
}
Built-in Functions
#include <iostream>
#include <cmath> // import library
int main() {
// sqrt() is from cmath
std::cout << sqrt(9);
}
C++ Classes & Objects
Defining a Class
class MyClass {
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
Creating an Object
MyClass myObj; // Create an object of MyClass
myObj.myNum = 15; // Set the value of myNum to 15
myObj.myString = "Hello"; // Set the value of myString to "Hello"
cout << myObj.myNum << endl; // Output 15
cout << myObj.myString << endl; // Output "Hello"
class MyClass {
public:
int myNum;
string myString;
MyClass() { // Constructor
myNum = 0;
myString = "";
}
~MyClass() { // Destructor
cout << "Object destroyed." << endl;
}
};
MyClass myObj; // Create an object of MyClass
// Code here...
// Object is destroyed automatically when the program exits the scope
Class Methods
class MyClass {
public:
int myNum;
string myString;
void myMethod() { // Method/function defined inside the class
cout << "Hello World!" << endl;
}
};
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
Access Modifiers
class MyClass {
public: // Public access specifier
int x; // Public attribute
private: // Private access specifier
int y; // Private attribute
protected: // Protected access specifier
int z; // Protected attribute
};
MyClass myObj;
myObj.x = 25; // Allowed (public)
myObj.y = 50; // Not allowed (private)
myObj.z = 75; // Not allowed (protected)
Getters and Setters
class MyClass {
private:
int myNum;
public:
void setMyNum(int num) { // Setter
myNum = num;
}
int getMyNum() { // Getter
return myNum;
}
};
MyClass myObj;
myObj.setMyNum(15); // Set the value of myNum to 15
cout << myObj.getMyNum() << endl; // Output 15
Inheritance
class Vehicle {
public:
string brand = "Ford";
void honk() {
cout << "Tuut, tuut!" << endl;
}
};
class Car : public Vehicle {
public:
string model = "Mustang";
};
Car myCar;
myCar.honk(); // Output "Tuut, tuut!"
cout << myCar.brand + " " + myCar.model << endl; // Output "Ford Mustang"
C++ Preprocessor
Preprocessor
Includes
#include "iostream"
#include <iostream>
Defines
#define FOO
#define FOO "hello"
#undef FOO
If
#ifdef DEBUG
console.log('hi');
#elif defined VERBOSE
...
#else
...
#endif
Error
#if VERSION == 2.0
#error Unsupported
#warning Not really supported
#endif