C Plus Plus Cheat Sheet
Published: 2025-03-05
Intro
This post is more of a reference for myself of the C++ programming language. It may or may not be usefull to anyone else, make of it what you will.
Comment
// Single Line Comment.
/*
Multi-Line
Comment
*/
Header File
Header files are used to share code and across multiple source files.
Using Header Files
// Declarations in <> brackets identify pre-existing header file such as those in the standard library.
#include <iostream>
// Declarations in double quotes ("") identify programmer defined header files.
#include "stuff.h"
Considerations
- The #include keyword is used to import header files.
- By convention, header files have a .h suffix.
Using Identifiers
The using keyword allows you bring identifiers into the current scope.
#include <iostream>
// Brings all identifiers from the `std` namespace into scope.
using namespace std;
// Bring individual identifiers into scope.
using std::cout;
using std::endl;
// Using identifiers now don't need to be prefixed with `std`.
int main() {
string blah;
cout << "hello" << endl;
cin >> blah;
return 0;
}
Types
Variables
Uniform Initialization
Uniform initialization (also known as Brace initialization has been available since C++11 and is the preferred variable initialization method.
int a{}, // Empty initiallized int
int b{5} // Initialized with 5
Function
Functions encapsulate a set of instructions.
// Function Syntax
<return_type> <function_name>(<argument_type>: <argument_name>) { // function signature
// function body
}
// Example
int stuff(x: int) {
return 0;
}
Function Overloading
Function overloading allows you to use the same name for a fuction that accpets different types. The functions could have different implementations depending on the function signature.
#include <iostream>
// Function Prototypes, define the function signature.
int stuff(int a, int b=1);
std::string stuff(std::string a, std::string b="World");
// Function Implementation, defines the functionality for the function signature.
// Default arguments from the function prototype, are not defined in the implementation.
int stuff(int a)
{
return a * b;
}
std::string stuff(std::string a)
{
return a + b;
}
int main()
{
int result1 = stuff(5, 10);
std::string result2 = stuff("Hello ");
std::cout << result1 << std::endl; // 50
std::cout << result2 << std::endl; // Hello World
return 0;
}
Template Function
Template Functions define a blueprint for creating functions that operate on different data type. They use Generic type markers to allow for flexible argument and return types.
#include <iostream>
// `T` is a generic type, allowing `stuff` to accept any type `T`.
// The return type will also be of the same type `T`.
template <typename T> // Prototype
T stuff(T a, T b);
int main()
{
// The stuff function is overloaded to accept both `int` and `std::string` argument types.
int result1 = stuff<int>(5, 10);
std::string result2 = stuff<std::string>("Hello ", "World");
std::cout << result1 << std::endl; // 15
std::cout << result2 << std::endl; // Hello World
return 0;
}
// Implementation
template <typename T>
T stuff(T a, T b)
{
return a + b;
}
Considerations
- Template functions can be overloaded in a similar way to normal function overloading.
- Implementations are defined after the main() function.
Outro
Stay weird nerds ✌️
Links
https://www.learncpp.com/cpp-tutorial/header-files/