From da281106bef0fd9199344d6dc21b7b1382f4ec1f Mon Sep 17 00:00:00 2001 From: Ttiki Date: Mon, 21 Sep 2026 10:25:15 +0200 Subject: [PATCH 1/2] 1.0 to 1.4 --- .idea/vcs.xml | 2 +- CMakeLists.txt | 13 ++++++--- InitialisationType.cpp | 15 +++++++++++ ListInitialisation.cpp | 13 +++++++++ MaybeUnused.cpp | 30 +++++++++++++++++++++ Variables.cpp | 15 +++++++++++ PrintStandard.cpp => v0/PrintStandard.cpp | 0 main.cpp => v0/helloworld.cpp | 32 ++++++----------------- 8 files changed, 91 insertions(+), 29 deletions(-) create mode 100644 InitialisationType.cpp create mode 100644 ListInitialisation.cpp create mode 100644 MaybeUnused.cpp create mode 100644 Variables.cpp rename PrintStandard.cpp => v0/PrintStandard.cpp (100%) rename main.cpp => v0/helloworld.cpp (61%) diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 91acb14..e21fd8e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,12 @@ project(LearnCPP) set(CMAKE_CXX_STANDARD 20) -add_executable(LearnCPP main.cpp - main.cpp - main.cpp - PrintStandard.cpp) +add_executable(LearnCPP v0/helloworld.cpp + v0/helloworld.cpp + v0/helloworld.cpp + v0/PrintStandard.cpp + Variables.cpp + Variables.cpp + InitialisationType.cpp + ListInitialisation.cpp + MaybeUnused.cpp) diff --git a/InitialisationType.cpp b/InitialisationType.cpp new file mode 100644 index 0000000..4ae6ad6 --- /dev/null +++ b/InitialisationType.cpp @@ -0,0 +1,15 @@ +#include + +int main() { + int a; // default-initialization (no initializer) + + // Traditional initialization forms: + int b = 5; // copy-initialization (initial value after equals sign) + int c ( 6 ); // direct-initialization (initial value in parenthesis) + + // Modern initialization forms (preferred): + int d { 7 }; // direct-list-initialization (initial value in braces) + int e {}; // value-initialization (empty braces) + + return 0; +} \ No newline at end of file diff --git a/ListInitialisation.cpp b/ListInitialisation.cpp new file mode 100644 index 0000000..f3ebf96 --- /dev/null +++ b/ListInitialisation.cpp @@ -0,0 +1,13 @@ +int main() +{ + // An integer can only hold non-fractional values. + // Initializing an int with fractional value 4.5 requires the compiler to convert 4.5 to a value an int can hold. + // Such a conversion is a narrowing conversion, since the fractional part of the value will be lost. + + int w1 { 4.5 }; // compile error: list-init does not allow narrowing conversion + + int w2 = 4.5; // compiles: w2 copy-initialized to value 4 + int w3 (4.5); // compiles: w3 direct-initialized to value 4 + + return 0; +} \ No newline at end of file diff --git a/MaybeUnused.cpp b/MaybeUnused.cpp new file mode 100644 index 0000000..5a1c841 --- /dev/null +++ b/MaybeUnused.cpp @@ -0,0 +1,30 @@ +#include + +int bad() +{ + // Here's some math/physics values that we copy-pasted from elsewhere + double pi { 3.14159 }; + double gravity { 9.8 }; + double phi { 1.61803 }; + + std::cout << pi << '\n'; // pi is used + std::cout << phi << '\n'; // phi is used + + // The compiler will likely complain about gravity being defined but unused + + return 0; +} + +int main() +{ + [[maybe_unused]] double pi { 3.14159 }; // Don't complain if pi is unused + [[maybe_unused]] double gravity { 9.8 }; // Don't complain if gravity is unused + [[maybe_unused]] double phi { 1.61803 }; // Don't complain if phi is unused + + std::cout << pi << '\n'; + std::cout << phi << '\n'; + + // The compiler will no longer warn about gravity not being used + + return 0; +} \ No newline at end of file diff --git a/Variables.cpp b/Variables.cpp new file mode 100644 index 0000000..0234683 --- /dev/null +++ b/Variables.cpp @@ -0,0 +1,15 @@ +#include + +int main() +{ + int width; // define a variable named width + width = 5; // copy assignment of value 5 into variable width + + std::cout << width; // prints 5 + + width = 7; // change value stored in variable width to 7 + + std::cout << width; // prints 7 + + return 0; +} \ No newline at end of file diff --git a/PrintStandard.cpp b/v0/PrintStandard.cpp similarity index 100% rename from PrintStandard.cpp rename to v0/PrintStandard.cpp diff --git a/main.cpp b/v0/helloworld.cpp similarity index 61% rename from main.cpp rename to v0/helloworld.cpp index a3b6aff..9cb7991 100644 --- a/main.cpp +++ b/v0/helloworld.cpp @@ -5,11 +5,10 @@ const int numStandards = 7; // The C++26 stdCode is a placeholder since the exact code won't be determined until the standard is finalized -const long stdCode[numStandards] = { 199711L, 201103L, 201402L, 201703L, 202002L, 202302L, 202612L}; -const char* stdName[numStandards] = { "Pre-C++11", "C++11", "C++14", "C++17", "C++20", "C++23", "C++26" }; +const long stdCode[numStandards] = {199711L, 201103L, 201402L, 201703L, 202002L, 202302L, 202612L}; +const char *stdName[numStandards] = {"Pre-C++11", "C++11", "C++14", "C++17", "C++20", "C++23", "C++26"}; -long getCPPStandard() -{ +long getCPPStandard() { // Visual Studio is non-conforming in support for __cplusplus (unless you set a specific compiler flag, which you probably haven't) // In Visual Studio 2015 or newer we can use _MSVC_LANG instead // See https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/ @@ -24,36 +23,21 @@ long getCPPStandard() #endif } -int main() -{ +int main() { long standard = getCPPStandard(); - if (standard == -1) - { + if (standard == -1) { std::cout << "Error: Unable to determine your language standard. Sorry.\n"; return 0; } - for (int i = 0; i < numStandards; ++i) - { + for (int i = 0; i < numStandards; ++i) { // If the reported version is one of the finalized standard codes // then we know exactly what version the compiler is running - if (standard == stdCode[i]) - { + if (standard == stdCode[i]) { std::cout << "Your compiler is using " << stdName[i] - << " (language standard code " << standard << "L)\n"; - break; - } - - // If the reported version is between two finalized standard codes, - // this must be a preview / experimental support for the next upcoming version. - if (standard < stdCode[i]) - { - std::cout << "Your compiler is using a preview/pre-release of " << stdName[i] - << " (language standard code " << standard << "L)\n"; + << " (language standard code " << standard << "L)\n"; break; } } - - return 0; } \ No newline at end of file From b1a7d123a55364b004b82f3ef6603430d731603e Mon Sep 17 00:00:00 2001 From: Ttiki Date: Tue, 22 Sep 2026 08:49:04 +0200 Subject: [PATCH 2/2] c1 completed --- CMakeLists.txt | 25 ++++++++++++------- {v0 => c0}/PrintStandard.cpp | 0 {v0 => c0}/helloworld.cpp | 0 c1/Expression.cpp | 24 ++++++++++++++++++ c1/FirstProgram.cpp | 14 +++++++++++ .../InitialisationType.cpp | 0 .../ListInitialisation.cpp | 0 c1/LiteralsVariables.cpp | 10 ++++++++ MaybeUnused.cpp => c1/MaybeUnused.cpp | 0 Variables.cpp => c1/Variables.cpp | 0 c1/buffering.cpp | 16 ++++++++++++ c1/cin.cpp | 12 +++++++++ c1/cout.cpp | 11 ++++++++ 13 files changed, 103 insertions(+), 9 deletions(-) rename {v0 => c0}/PrintStandard.cpp (100%) rename {v0 => c0}/helloworld.cpp (100%) create mode 100644 c1/Expression.cpp create mode 100644 c1/FirstProgram.cpp rename InitialisationType.cpp => c1/InitialisationType.cpp (100%) rename ListInitialisation.cpp => c1/ListInitialisation.cpp (100%) create mode 100644 c1/LiteralsVariables.cpp rename MaybeUnused.cpp => c1/MaybeUnused.cpp (100%) rename Variables.cpp => c1/Variables.cpp (100%) create mode 100644 c1/buffering.cpp create mode 100644 c1/cin.cpp create mode 100644 c1/cout.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e21fd8e..4e665a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,12 +4,19 @@ project(LearnCPP) set(CMAKE_CXX_STANDARD 20) -add_executable(LearnCPP v0/helloworld.cpp - v0/helloworld.cpp - v0/helloworld.cpp - v0/PrintStandard.cpp - Variables.cpp - Variables.cpp - InitialisationType.cpp - ListInitialisation.cpp - MaybeUnused.cpp) +add_executable(LearnCPP c0/helloworld.cpp + c0/helloworld.cpp + c0/helloworld.cpp + c0/PrintStandard.cpp + c1/Variables.cpp + c1/Variables.cpp + c1/InitialisationType.cpp + c1/ListInitialisation.cpp + c1/MaybeUnused.cpp + c1/cout.cpp + c1/cin.cpp + c1/buffering.cpp + c1/LiteralsVariables.cpp + c1/Expression.cpp + c1/FirstProgram.cpp +) diff --git a/v0/PrintStandard.cpp b/c0/PrintStandard.cpp similarity index 100% rename from v0/PrintStandard.cpp rename to c0/PrintStandard.cpp diff --git a/v0/helloworld.cpp b/c0/helloworld.cpp similarity index 100% rename from v0/helloworld.cpp rename to c0/helloworld.cpp diff --git a/c1/Expression.cpp b/c1/Expression.cpp new file mode 100644 index 0000000..299554d --- /dev/null +++ b/c1/Expression.cpp @@ -0,0 +1,24 @@ +#include + +// five() is a function that returns the value 5 +int five() +{ + return 5; +} + +int main() +{ + int a{ 2 }; // initialize variable a with literal value 2 + int b{ 2 + 3 }; // initialize variable b with computed value 5 + int c{ (2 * 3) + 4 }; // initialize variable c with computed value 10 + int d{ b }; // initialize variable d with variable value 5 + int e{ five() }; // initialize variable e with function return value 5 + + int x {}; + x = 5; // x = 5 has side effect of assigning 5 to x, evaluates to x + x = 2 + 3; // has side effect of assigning 5 to x, evaluates to x + std::cout << x; // has side effect of printing value of x to console, evaluates to std::cout + + + return 0; +} \ No newline at end of file diff --git a/c1/FirstProgram.cpp b/c1/FirstProgram.cpp new file mode 100644 index 0000000..4eea70a --- /dev/null +++ b/c1/FirstProgram.cpp @@ -0,0 +1,14 @@ +#include + +// preferred version +int main() +{ + std::cout << "Enter an integer: "; + + int num{ }; + std::cin >> num; + + std::cout << "Double that number is: " << num * 2 << '\n'; // use an expression to multiply num * 2 at the point where we are going to print it + + return 0; +} \ No newline at end of file diff --git a/InitialisationType.cpp b/c1/InitialisationType.cpp similarity index 100% rename from InitialisationType.cpp rename to c1/InitialisationType.cpp diff --git a/ListInitialisation.cpp b/c1/ListInitialisation.cpp similarity index 100% rename from ListInitialisation.cpp rename to c1/ListInitialisation.cpp diff --git a/c1/LiteralsVariables.cpp b/c1/LiteralsVariables.cpp new file mode 100644 index 0000000..0218ca3 --- /dev/null +++ b/c1/LiteralsVariables.cpp @@ -0,0 +1,10 @@ +#include + +int main() +{ + std::cout << 5 << '\n'; // print the value of a literal + + int x { 5 }; + std::cout << x << '\n'; // print the value of a variable + return 0; +} \ No newline at end of file diff --git a/MaybeUnused.cpp b/c1/MaybeUnused.cpp similarity index 100% rename from MaybeUnused.cpp rename to c1/MaybeUnused.cpp diff --git a/Variables.cpp b/c1/Variables.cpp similarity index 100% rename from Variables.cpp rename to c1/Variables.cpp diff --git a/c1/buffering.cpp b/c1/buffering.cpp new file mode 100644 index 0000000..fec4017 --- /dev/null +++ b/c1/buffering.cpp @@ -0,0 +1,16 @@ +#include // for std::cout and std::cin + +int main() +{ + std::cout << "Enter two numbers: "; + + int x{}; + std::cin >> x; // If you enter 4 5 here, the buffer will be {4, 5} + + int y{}; + std::cin >> y; //Thus the prompt will not appear as we have an item in the buffer {5} + + std::cout << "You entered " << x << " and " << y << '\n'; // The program will go directly here and print "You entered 4 and 5" + + return 0; +} \ No newline at end of file diff --git a/c1/cin.cpp b/c1/cin.cpp new file mode 100644 index 0000000..a052f7b --- /dev/null +++ b/c1/cin.cpp @@ -0,0 +1,12 @@ +#include // for std::cout and std::cin + +int main() +{ + std::cout << "Enter a number: "; // ask user for a number + + int x{}; // define variable x to hold user input (and value-initialize it) + std::cin >> x; // get number from keyboard and store it in variable x + + std::cout << "You entered " << x << '\n'; + return 0; +} \ No newline at end of file diff --git a/c1/cout.cpp b/c1/cout.cpp new file mode 100644 index 0000000..1f10d73 --- /dev/null +++ b/c1/cout.cpp @@ -0,0 +1,11 @@ +#include // for std::cout + +int main() +{ + int x{ 5 }; + std::cout << "x is equal to: " << x << '\n'; // single quoted (by itself) (conventional) + std::cout << "Yep." << "\n"; // double quoted (by itself) (unconventional but okay) + std::cout << "And that's all, folks!\n"; // between double quotes in existing text (conventional) + std::cout << std::endl; // Move to a new line, flush buffer (slow, prefer using \n) + return 0; +} \ No newline at end of file