Documentation

C++

This concise and practical C++ programming cheat sheet provides an essential reference for both beginners and experienced programmers. It covers fundamental concepts, syntax, and code snippets, allowing you to quickly grasp and apply key concepts in C++.

C++

Standard Libraries

  • Essential for using various functionalities in C++
#include <iostream> // For Input/Output
#include <vector> // For Dynamic Arrays
#include <string> // For String Operations
#include <map> // For Map Collections
#include <cmath> // For Mathematical Functions
#include <fstream> // For File I/O
#include <cstdlib> // For General Purpose Functions, including rand() and srand()
#include <ctime> // For Time-related Functions

Standard Input/Output

  • Input from the user
cin >> variable; // Input
  • Output to the console
cout << "Hello, World!"; // Output

Basic Data Types

  • Fundamental types for storing data
Data TypeDescriptionExample
intIntegerint age = 25;
floatSingle-precision floating-pointfloat pi = 3.14;
doubleDouble-precision floating-pointdouble price = 99.99;
charCharacterchar grade = 'A';
boolBooleanbool isTrue = true;

Control Flow

  • if statement
if (condition) { /* true block */ }

  • else if and else clauses
if (condition1) {
    // Code block 1
} else if (condition2) {
    // Code block 2
} else {
    // Code block 3
}

  • for loop
for (int i = 0; i < 10; ++i) {
    // Code to execute
}

  • while loop
while (condition) {
    // Code to execute
}
  • do-while loop
do {
    // Code to execute
} while (condition);
  • Range-Based For Loop
for (const auto& item : container) {
    // Use item
}

Functions

  • Creating reusable blocks of code.
// Declaration
return_type function_name(parameter_list);

// Definition
return_type function_name(parameter_list) {
    // Function body
    return value;
}

Function Overloading

  • Functions can have the same name but different parameters
int multiply(int a, int b);
double multiply(double a, double b);


Arrays

  • Declaring and initializing an array
int numbers[5] = {1, 2, 3, 4, 5};

  • Accessing array elements
int element = numbers[index];

Pointers

  • Declaring a Pointer and Assigning It to a Variable’s Address
int number = 42;
int* ptr = &number;

  • Dereferencing a Pointer to Access the Value It Points To
int value = *ptr;


Classes and Objects

  • Class is a blueprint for creating objects
class ClassName {
public:
    // Constructors
    ClassName();

    // Member functions
    void method();

private:
    // Member variables
    int attribute;
};

Inheritance

  • Inheritance allows a class to inherit attributes and methods from another class
class Base {
public:
    void display();
};

class Derived : public Base {
    // Inherits display()
};

Exception Handling

  • Exceptions handle runtime errors gracefully
try {
    // Code that may throw an exception
} catch (exception_type& e) {
    // Exception handling code
}

C-style Strings and Functions

  • Handling C-style strings
#include <cstring>
#include <iostream>
using namespace std;

// Length of a CString
int lengthOfCString(const char* str) {
    return strlen(str);
}

// Copy CString
void copyCString(char* destination, const char* source) {
    strcpy(destination, source);
}

// Concatenate CStrings
void concatenateCStrings(char* destination, const char* source) {
    strcat(destination, source);
}

// Compare CStrings
int compareCStrings(const char* str1, const char* str2) {
    return strcmp(str1, str2); // Returns 0 if equal
}

// Find Substring in CString
const char* findSubstring(const char* str, const char* substr) {
    return strstr(str, substr);
}

// Convert CString to Integer
int cstringToInt(const char* str) {
    return atoi(str);
}

// Convert CString to Double
double cstringToDouble(const char* str) {
    return atof(str);
}


Random Number Generation

  • Utilize rand() and srand() for generating random numbers
#include <cstdlib> // Include for rand() and srand()
#include <ctime> // Include for time()

void initializeRandomSeed() {
  srand(static_cast < unsigned int > (time(0))); // Initialize random seed
}

int getRandomNumber(int min, int max) {
  // Returns a random number between min and max (inclusive)
  return rand() % (max - min + 1) + min;
}

// Example Usage
initializeRandomSeed();
int randomNum = getRandomNumber(1, 100); // Random number between 1 and 100


Sorting Methods

  • Quick and easy sorting with C++ Standard Library.
#include <algorithm>
#include <vector>
using namespace std;

void standardSort(vector < int > & vec) {
  sort(vec.begin(), vec.end());
}


  • Bubble Sort
void bubbleSort(vector < int > & vec) {
  for (size_t i = 0; i < vec.size() - 1; ++i) {
    for (size_t j = 0; j < vec.size() - i - 1; ++j) {
      if (vec[j] > vec[j + 1]) {
        swap(vec[j], vec[j + 1]);
      }
    }
  }
}