Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
hoklavat committed Jan 5, 2021
1 parent 805e7fb commit 8e804a9
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 01-Empty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//01-Minimal

int main(){}
16 changes: 16 additions & 0 deletions 02-Constant.cpp
Original file line number Diff line number Diff line change
@@ -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(){}
13 changes: 13 additions & 0 deletions 03-Enum.cpp
Original file line number Diff line number Diff line change
@@ -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(){}
18 changes: 18 additions & 0 deletions 04-Seperation.cpp
Original file line number Diff line number Diff line change
@@ -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(){}
14 changes: 14 additions & 0 deletions 05-Namespace.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
20 changes: 20 additions & 0 deletions 06-Exception.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//06-Exception
//report errors found at run time.

#include <iostream>

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";
}
}
9 changes: 9 additions & 0 deletions 07-StaticAssertion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//07-StaticAssertion
//compile time error checking.

#include <iostream>

int main(){
const int x = 15;
static_assert(x<10, "integer is too large."); //error at compile time.
}
18 changes: 18 additions & 0 deletions 08-InitializerList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//08-InitializerList

#include <iostream>

class C{
public:
C(std::initializer_list<int> 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;
}
1 change: 1 addition & 0 deletions MODERNCPP_REMINDER.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*** MODERN CPP ***
14 changes: 14 additions & 0 deletions MODERNCPP_SOURCE.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8e804a9

Please sign in to comment.