From 8e804a9e5cbe9eff86b292d5a2a6be82e5bf8791 Mon Sep 17 00:00:00 2001 From: hoklavat Date: Tue, 5 Jan 2021 22:09:21 +0300 Subject: [PATCH] . --- 01-Empty.cpp | 3 +++ 02-Constant.cpp | 16 ++++++++++++++++ 03-Enum.cpp | 13 +++++++++++++ 04-Seperation.cpp | 18 ++++++++++++++++++ 05-Namespace.cpp | 14 ++++++++++++++ 06-Exception.cpp | 20 ++++++++++++++++++++ 07-StaticAssertion.cpp | 9 +++++++++ 08-InitializerList.cpp | 18 ++++++++++++++++++ MODERNCPP_REMINDER.txt | 1 + MODERNCPP_SOURCE.txt | 14 ++++++++++++++ 10 files changed, 126 insertions(+) create mode 100644 01-Empty.cpp create mode 100644 02-Constant.cpp create mode 100644 03-Enum.cpp create mode 100644 04-Seperation.cpp create mode 100644 05-Namespace.cpp create mode 100644 06-Exception.cpp create mode 100644 07-StaticAssertion.cpp create mode 100644 08-InitializerList.cpp create mode 100644 MODERNCPP_REMINDER.txt create mode 100644 MODERNCPP_SOURCE.txt diff --git a/01-Empty.cpp b/01-Empty.cpp new file mode 100644 index 0000000..710c217 --- /dev/null +++ b/01-Empty.cpp @@ -0,0 +1,3 @@ +//01-Minimal + +int main(){} \ No newline at end of file diff --git a/02-Constant.cpp b/02-Constant.cpp new file mode 100644 index 0000000..0cd8e1d --- /dev/null +++ b/02-Constant.cpp @@ -0,0 +1,16 @@ +//02-Constant +//immutability. +//const: run time constant. +//constexpr: compile time constant. + +int x = 1; +const int y = x; +//constexpr int z = x; //error. + + +//double f(double x){return x*x;} +constexpr double f(double x){return x*x;} +constexpr int z = f(10); + + +int main(){} \ No newline at end of file diff --git a/03-Enum.cpp b/03-Enum.cpp new file mode 100644 index 0000000..7b189b7 --- /dev/null +++ b/03-Enum.cpp @@ -0,0 +1,13 @@ +//03-Enum + +//plain enum. +enum COLOR{Red, Green, Blue}; +int C = COLOR::Red; //Red. + +//strongly typed enum. +enum class color{Red, Green, Blue}; +//int c = color::Red; //error. +//color c = 1; //error. +color c = color::Red; + +int main(){} \ No newline at end of file diff --git a/04-Seperation.cpp b/04-Seperation.cpp new file mode 100644 index 0000000..c129923 --- /dev/null +++ b/04-Seperation.cpp @@ -0,0 +1,18 @@ +//04-Modularity +//declaration and implementation are seperated into different files. + +//class.h +class C{ + int x; + void f(int); +}; + +//class.cpp +//#include "class.h" +void C::f(int y){ + x = y; +} + +//main.cpp +//#include "class.h" +int main(){} \ No newline at end of file diff --git a/05-Namespace.cpp b/05-Namespace.cpp new file mode 100644 index 0000000..d0def7d --- /dev/null +++ b/05-Namespace.cpp @@ -0,0 +1,14 @@ +//05-Namespace +//used to organize components. + +namespace N{ + void f(){} //N namespace. +}; + +void f(){} //global scope. + +int main(){ + //using namespace N; //error. conflict. + N::f(); + f(); +} \ No newline at end of file diff --git a/06-Exception.cpp b/06-Exception.cpp new file mode 100644 index 0000000..bd020dc --- /dev/null +++ b/06-Exception.cpp @@ -0,0 +1,20 @@ +//06-Exception +//report errors found at run time. + +#include + +class C{ +public: + C(int x){ + if(x > 10) throw std::out_of_range{"Exception: Large Number."}; + } +}; + +int main(){ + try{ + C c{15}; + } + catch(std::out_of_range){ + std::cout << "Exception occured.\n"; + } +} \ No newline at end of file diff --git a/07-StaticAssertion.cpp b/07-StaticAssertion.cpp new file mode 100644 index 0000000..e9184c8 --- /dev/null +++ b/07-StaticAssertion.cpp @@ -0,0 +1,9 @@ +//07-StaticAssertion +//compile time error checking. + +#include + +int main(){ + const int x = 15; + static_assert(x<10, "integer is too large."); //error at compile time. +} \ No newline at end of file diff --git a/08-InitializerList.cpp b/08-InitializerList.cpp new file mode 100644 index 0000000..0498513 --- /dev/null +++ b/08-InitializerList.cpp @@ -0,0 +1,18 @@ +//08-InitializerList + +#include + +class C{ +public: + C(std::initializer_list l){ + data = new int[l.size()]; + std::copy(l.begin(), l.end(), data); + } + + int *data; +}; + +int main(){ + C c{1, 2, 3}; + std::cout << c.data[0] << std::endl; +} \ No newline at end of file diff --git a/MODERNCPP_REMINDER.txt b/MODERNCPP_REMINDER.txt new file mode 100644 index 0000000..9e09459 --- /dev/null +++ b/MODERNCPP_REMINDER.txt @@ -0,0 +1 @@ +*** MODERN CPP *** diff --git a/MODERNCPP_SOURCE.txt b/MODERNCPP_SOURCE.txt new file mode 100644 index 0000000..6d5ef86 --- /dev/null +++ b/MODERNCPP_SOURCE.txt @@ -0,0 +1,14 @@ +*** MODERN CPP *** + +[SOFTWARE] +* Compiler: MinGW GCC, MSVC +* IDE: CodeLite, Visual Studio 2019 + +01-Empty +02-Constant +03-Enum +04-Seperation +05-Namespace +06-Exception +07-StaticAssertion +08-InitializerList \ No newline at end of file