constructor in C++

* Unlike new, malloc() doesn’t call constructor.
* All members are initialised before the body of the constructor.
* There is no such a thing as virtual constructor. The constructor can’t be virtual as the constructor is responsible for creating an instance of a class and it can’t be delegated to any other object by virtual keyword.
* Default constructor is the constructor without parameters. If we write any constructor, then compiler doesn’t create the default constructor.
* A copy constructor initialises an object using another object of the same class. 
    * Compiler creates a copy constructor if we don’t write our own. 
    * Objects must be passed by reference in copy constructors. Compiler checks for this and produces compiler error if not passed by reference.
    * There must be a user defined copy constructor in classes with pointers or dynamic memory allocation to avoid shallow copy.
    * In C++, a copy constructor may be called for the following cases. It is, however, not guaranteed that a copy constructor will be called in all these cases, because C++ allows the compiler to optimise the copy away in certain cases, one example being the return value optimisation.
        * When an object of the class is returned by value; usually optimised away. This is called copy elision.
        * When an object of the class is passed to a function by value as an argument. 
        *  When an object is constructed based on another object of the same class. 
        * When the compiler generates a temporary object. 
* Move constructor
    * If the definition of a class does not explicitly declare a move constructor, a non-explicit one will be implicitly declared as defaulted if and only if
the class does not have a user-declared copy constructor.

```
#include <iostream>
using namespace std;

class A {
private:
    int val;
public:
    A(int v) { val = v; }
    A() { val = 0; }
    void say() {
        cout << val << endl;
    }
};

int main() {
    A a1, a2(3);
    a1.say(); //0
    a2.say(); //3
    return 0;
}
```