program generated constructor cpp code example

Example 1: how to initialize the object in constructor in c++

BigMommaClass {
    BigMommaClass(int, int);

private:
    ThingOne thingOne;
    ThingTwo thingTwo;
};

BigMommaClass::BigMommaClass(int numba1, int numba2): thingOne(numba1 + numba2), thingTwo(numba1, numba2) {
// Code here
}

Example 2: what is constractor

Constructors are the special methods without any return type. 
Constructors have the same name with the className
Constructors are special methods, which are called 
whenever the new keyword is used to create an object of a class
By default every class always has a default constructor 
with no parameters. This default constructor is no longer there if a 
constructor is created manually
Constructors usually act to initialize instance variables or perform 
actions that need to be taken whenever an object of a class is created.
Like methods, constructors can also be overloaded. 
The same rules apply. This is done by providing a constructor with 
with a different parameters
this() can be used to call the overloaded
constructors with other parameters

Tags:

Cpp Example