Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ project(LearnCPP)

set(CMAKE_CXX_STANDARD 20)

add_executable(LearnCPP main.cpp
main.cpp
main.cpp
PrintStandard.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
)
File renamed without changes.
32 changes: 8 additions & 24 deletions main.cpp → c0/helloworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -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;
}
24 changes: 24 additions & 0 deletions c1/Expression.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>

// 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;
}
14 changes: 14 additions & 0 deletions c1/FirstProgram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>

// 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;
}
15 changes: 15 additions & 0 deletions c1/InitialisationType.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>

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;
}
13 changes: 13 additions & 0 deletions c1/ListInitialisation.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions c1/LiteralsVariables.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>

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;
}
30 changes: 30 additions & 0 deletions c1/MaybeUnused.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>

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;
}
15 changes: 15 additions & 0 deletions c1/Variables.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>

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;
}
16 changes: 16 additions & 0 deletions c1/buffering.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream> // 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;
}
12 changes: 12 additions & 0 deletions c1/cin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <iostream> // 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;
}
11 changes: 11 additions & 0 deletions c1/cout.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream> // 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;
}
Loading