Diamond Inheritance

virtual keyword on multiple base class creates one and only copy

All Virtual Methods Are Abstract in CPP

Virtual is a keyword to declare that the method must be override

If all method is virtual and equals 0, the class runs as an interface.

Override keywords shows that the virtual method has been overridden.

The shared pointer is the smart pointer way to declare a variable as a pointer. You also use a unique pointer too.

The base classes deconstruction method should be virtual every time. Otherwise, its deconstruction doesn’t work in polimofic cases.

You can not override the inherited method if it is not mentioned by virtual keyword. If a class not suitable for inheriting we should mark it with final keyword.

Implementing Constructors on Inheritance with using Keyword on C++

using keyword is the new way to implement a constructor to the child class.

initializer_list c++

If you set a list to an abject (or on auto keyword). CPP compiler assumes that the type of variable is initializer_list

Constexpr in C++

Constexpr is a new keyword for a C++ developer. Normally Const returns constant but in runtime. Constexpr generally is used in front of a function. The function should be able to calculate in compile time. These functions must return void or a primitive or primitive reference.

Enums in C++

Here is the short note

Enum class a new type, It is, of course, an enum bat using namespace. Type is the variable is char in here and the value of a type declared as “a” in here.

Smart pointers

There are two types of smart pointers. The unique pointer is a standard method which came from C++11 implementation that returns a pointer. A unique pointer does not to be shared by other parts of the code. To use any of smart pointer you have to include the memory header file into your program.

So you may mention that t is not necessary to work about delete operation. That’s why the unique pointer overcomes our problem about memory management.

But if you copy of a unique pointer, for instance, passing to another method as a parameter, causes a problem that it deletes actual pointer. So you can’t pass a unique pointer to another method directly. Of course std::move function which calls move constructor to help us call a function. But keep it in mind, if you call a move method you can’t reach the old value of actual pointer after return the function.

But there is a solution to it. Passing reference of pointer instates of itself.

On the other hand, the shared pointer is fine in many cases. Shared pointers hold an indicator counter and it increases it in every case of constructors and decreases it in each deconstruction calls. Shared pointer gives memory to the system back when there is no other copy in a complex operation.

So you don’t have to pass a pointer of the unique pointer to a method. At the same time, there is only one way, shared pointer, in the case of “has a” relation of two objects.

Weak Pointer

Shared pointer has two value, pointer and counter. The weak pointer indicates a pointer for the counter. And the weak pointer has two methods.

Expired is the method which returns a boolean value. If the shared pointers counter is 0 it’s expired.

The lock is the other method which returns a shared pointer of the pointer and increases the counter of it.

In circular reference, shared pointer causes a memory leak. We also use the weak pointer to avoid that.

Cast in C++

My short notes for casting in C++

Static Cast

Checks in compile time that the types of the casing is possible and correct

Reinterpret Cast

Allows casting in different types. Even if the types are not related. But it does not cause disqualifiers

Const Cast

Constantly convert two types of data

Dynamic Cast

Casting on running time.

Copy and Move Constructors

In the life of the C++ programmer, there are 2 major interesting constructor strategies. One of them is for copping an object and the other for moving data.

The copy constructor is basic structure which receives a reference of an object. The parameter object must be constant because in the copping operation we can’t modify the values of the object. The structure of the method is:

The other method is the move constructor that receives reference of the reference object. The structure is;

this method runs when a method needs to create a copy of data of existing class

In flowing code, you may find an example code

The move constructor has to set pointers to nullptr because if old object destroys its destroy method may delete existing pointer and we can not reach the values on newly created or moved one.

To force a method to run this move controller: you have to include <memory> header file and std::move is the method to call this constructor.

Delete, Default and Delegating Constructors

If you using modern C++ in your programing life there are three important concepts in your life to manage methods.

If you compose a class without writing your own constructor (or copy constructor), The compiler creates a method for you to overcome basic structures. Of course, it does nothing but it is really necessary.

The thing about a scenario that you overwrite constructor method but this method takes a parameter, What happens if you did not present a constructor without parameter? Of course, the answer is the cosplayer don’t produce any constructor for you. So the default parameter cames here to help to say the compiler to generate a method for you. On the other hand, the delete keywords tells the compiler not to provide any peace of code for us for the method. Let see it in bellow.

In some cases, you do not prefer to implement all of the version of constructors because another version may overcome that problem. So the way is delegating the method to another.

Keep in mind, delegation works before your codes in the method.