Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
hoklavat committed Jan 7, 2021
1 parent 8e804a9 commit 80eb0e2
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 3 deletions.
File renamed without changes.
File renamed without changes.
45 changes: 45 additions & 0 deletions 09-Abstraction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//09-Abstraction

#include <iostream>

//Interface/Abstract Class/Super Class/Base Class.
class A{
public:
//Pure Virtual Function.
virtual void f() = 0;

//Virtual Destructor.
virtual ~A(){
std::cout << "A - destructor." << std::endl;
}
};

//Concrete Class/Sub Class/Derived Class.
class B: public A{
public:
B(){
std::cout << "B - constructor." << std::endl;
}

~B(){
std::cout << "B - destructor." << std::endl;
}

//Override.
void f(){
std::cout << "Hello World!" << std::endl;
}
};

int main(){
B b;
b.f();

std::cout << std::endl;

A *a = new B();
a->f();
delete a;

std::cout << std::endl;
}
57 changes: 57 additions & 0 deletions 10-Copy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//10-Copy

#include <iostream>

class A{
private:
int *data;
int size;

public:
A(int s): size{s}{
data = new int[s];
}

//Copy Constructor.
A(const A& a): data{new int[a.size]}, size{a.size}{
for(int i = 0; i < a.size; i++)
data[i] = a.data[i];
}

//Copy Assignment.
A& operator=(const A& a){
int *p = new int[a.size];
for(int i = 0; i < a.size; i++)
p[i] = a.data[i];
delete[] data;
data = p;
size = a.size;
return *this;
}

void fill(int n){
for(int i = 0; i < size; ++i){
data[i] = n;
}
}

void print(){
for(int i = 0; i < size; ++i){
std::cout << data[i] << " ";
}
}
};

int main(){
A a(10);
a.fill(10);
a.print();

std::cout << std::endl;

A b(a);
b.print();

std::cout << std::endl;

}
54 changes: 54 additions & 0 deletions 11-Move.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//11-Move

#include <iostream>

class A{
private:
int *data;
int size;

public:
A(int s): size{s}{
data = new int[s];
}

~A(){};

//Move Constructor.
A(A&& a): data{a.data}, size{a.size}{
std::cout << "move constructor." << std::endl;
a.data = nullptr;
a.size = 0;
}

//Move Assignment.
A& operator=(A&& a){
std::cout << "move assignment." << std::endl;
data = a.data;
size = a.size;
a.data = nullptr;
a.size = 0;
return *this;
}

void fill(int n){
for(int i = 0; i < size; ++i){
data[i] = n;
}
}

void print(){
for(int i = 0; i < size; ++i){
std::cout << data[i] << " ";
}
}
};

int main(){
A a(10);
a.fill(10);

A b(10);
b = std::move(a);
b.print();
}
9 changes: 6 additions & 3 deletions MODERNCPP_SOURCE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
* Compiler: MinGW GCC, MSVC
* IDE: CodeLite, Visual Studio 2019

01-Empty
01-Minimal
02-Constant
03-Enum
04-Seperation
04-Modularity
05-Namespace
06-Exception
07-StaticAssertion
08-InitializerList
08-InitializerList
09-Abstraction
10-Copy
11-Move

0 comments on commit 80eb0e2

Please sign in to comment.