Skip to content

neogeek/learning-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

13 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Learning C++

TOC

Variables

Initialization

int i = 1; // copy initialization,
int j(2); // direct initialization, improved performance
int k{3}; // uniform initialization, best performance (C++ 11 and up)
int i = 1, j = 2, k = 3;
int i(1), j(2), k(3);

C++ 11

int i{1}, j{2}, k{3};

Integers

int i = 1; // 16 bit
long j = 2; // 32 bit

Floats

Make sure that float values end in f to prevent loss of precision as it's converting a double to a float.

float k = 3.14f; // 32 bit
double l = 3.14f; // 64 bit

Strings

#include <iostream>

std::string message = "Hello, world";

Booleans

bool enabled = false;

Dynamic Types

decltype requires C++ 11

float k = 3.14f;
decltype(k) a{k};

Auto Types

auto i = 1;
auto j = 3.14f;
auto message = "Hello, world";

Typedefs

#include <iostream>

typedef float distance;

int main() {
    distance metersFromTarget = 25;
    std::cout << typeid(metersFromTarget).name() << std::endl; // f
}

Type Casting

int i = (int)42;
int j = static_cast<int>(42);
float i = (float)3.14;
float j = static_cast<float>(3.14);
#include <iostream>

int main() {
    std::cout << "Pie is " + std::to_string(3.14) << std::endl;
}

Constants

const float p = 3.14f;

Compile Time Constant Expressions

The value of p2 is calculated at compile time and then stored in p2 as a constant.

constexpr float p2 = 3.14f * 2;

Symbolic Constants

#define MAX_NUM_HTTP_RETRIES 5

Enums

Basic enum values are accessible without prefix.

#include <iostream>

enum COLOR {
    RED,
    GREEN,
    BLUE
};

int main() {
    std::cout << GREEN << std::endl;
}

Class-based enum values require the prefix. These are preferred.

#include <iostream>

enum class COLOR {
    RED,
    GREEN,
    BLUE
};

int main() {
    std::cout << (int)COLOR::GREEN << std::endl;
}

Class-based enum class with custom int values.

enum class EXIT_CODES {
    SUCCESS = 0,
    GENERAL_ERROR = 1,
    MISSUSE_OF_SHELL_BUILTIN = 2,
    COMMAND_CANNOT_BE_EXECUTUED = 126,
    COMMAND_NOT_FOUND = 127,
    INVALID_ARGUMENT = 128,
    SCRIPT_TERMINATED = 130,
    EXIT_STATUS_OUT_OF_RANGE = 255
};

int main() {
    return (int)EXIT_CODES::SCRIPT_TERMINATED;
}

Conditional Statements

If

#include <iostream>

int main() {

    bool activeStatus = false;

    if (activeStatus == true) {

        std::cout << "The device is active." << std::endl;

    } else {

        std::cout << "The device is inactive." << std::endl;

    }

}

For

#include <iostream>

int main() {

    for (int i = 0; i < 10; i += 1) {

        std::cout << std::to_string(i) << " Hello, world." << std::endl;

    }

}

While

#include <iostream>

int main() {

    int i = 0;

    while (i < 10) {

        std::cout << std::to_string(i) << " Hello, world" << std::endl;

        i++;

    }

}

Switch

#include <iostream>

int main() {

    int status = 0;

    switch (status) {
        case 0:
            std::cout << "Success." << std::endl;
            break;

        case 1:
            std::cout << "Unknown error." << std::endl;
            break;

        case 130:
            std::cout << "Script terminated." << std::endl;
            break;

        default:
            break;
    }

}

Functions

#include <iostream>

void displayMessage(std::string message) {
    std::cout << message << std::endl;
};

int main() {
    displayMessage("Hello, world!");
}
#include <iostream>

int addNumbers(int a, int b) {
    return a + b;
}

int main() {
    std::cout << addNumbers(1, 2) << std::endl;
}

Templates

#include <iostream>

template<typename T, size_t N>
void printArray(T (&array)[N]) {

    int length = std::size(array);

    std::string output = "";

    for (int i = 0; i < length; i += 1) {

        output += std::to_string(array[i]) + " ";

    }

    std::cout << output << std::endl;

}
#include <iostream>

template<typename T, size_t N>
void sortArray(T (&array)[N]) {

    int length = std::size(array);

    for (int i = 0; i < length; i += 1) {

        int lowestValueIndex = i;

        for (int j = i; j < length; j += 1) {

            if (array[j] < array[lowestValueIndex]) {

                lowestValueIndex = j;

            }

        }

        std::swap(array[i], array[lowestValueIndex]);

    }

}

Structs

struct Vector2 {
    float x;
    float y;
};

int main() {
    Vector2 position{1,2};
}

Methods

struct Vector2 {
    float x;
    float y;
    Vector2 add(Vector2 other) {
        return {
            this->x + other.x,
            this->y + other.y
        };
    };
};

int main() {
    Vector2 position{1,2};
    Vector2 newPosition = position.add(Vector2{1,1});
}

Arithmetic Operators

struct Vector2 {
    float x;
    float y;
    friend Vector2 operator+(Vector2 a, Vector2 b);
};

Vector2 operator+(Vector2 a, Vector2 b) {
    return {
        a.x + b.x,
        a.y + b.y
    };
}

int main() {
    Vector2 position{1,2};
    Vector2 newPosition = position + Vector2{1,1};
}

Arrays

std::size requires C++ 17

int main()
{
    int array[5];
    array[0] = 1;
    array[1] = 2;
    array[2] = 3;
    array[3] = 4;
    array[4] = 5;
}
#include <iostream>

int main()
{
    int array[5]{1, 2, 3, 4, 5};
    std::cout << std::size(array) << std::endl;
}

IO

Output

#include <iostream>

int main()
{
    std::cout << "Hello, world!\n";
}

Input

#include <iostream>

int main()
{
    int test;
    std::cout << "Enter integer: ";
    std::cin >> test;
    std::cout << "You entered " << test << "\n";
}

About

๐Ÿ’พ main.cpp

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published