constructor
A constructor is a special function having the "same name" as the classname, with no return-type. As the name implied, constructor is called each time when an instance is declared (or constructed). Constructor is used for initializing the data members of the instances created. The syntax is:
// Declare constructor inside the class declaration
class ClassName {
ClassName(parameter-list); // prototype only
}
// Constructor implementation - identified via scope resolution operator
ClassName::ClassName(parameter-list) {
function-body;
}
Member Initializer List: used to initialize data members in the constructor. For example,
Point::Point(int x, int y) : x(x), y(y) {
// The body runs after the member initializer list
}
The member initialiser list is placed after the function parameter list, separated by a colon. For fundamental-type data members (e.g., int, double), x(x) is the same as this->x = x. For object data members, the copy constructor will be invoked for each of the object. The function body will be executed after the member initializer list, which is empty in this case.
Alternatively, you could initialize the data members inside the constructor's body:
Point::Point(int x, int y) {
this->x = x;
this->y = y;
}
where this ->x refers to the data member x; and x refer to the function parameter x.
Another alternative that avoids naming conflict, but is hard to read:
Point::Point(int x_, int y_) {
x = x_;
y = y_;
}
Default Constructor: The default constructor refers to the constructor that takes no parameter - either it has no parameter or all parameters have their default value (e.g., the above Point's constructor). If no constructor is defined in the class, the compiler inserts a default constructor that takes no argument and does nothing (i.e., ClassName::ClassName() {}). However, if you define one (or more) constructors, compiler will not insert the default constructor. You need to define your default constructor, if desired.
// Declare constructor inside the class declaration
class ClassName {
ClassName(parameter-list); // prototype only
}
// Constructor implementation - identified via scope resolution operator
ClassName::ClassName(parameter-list) {
function-body;
}
Member Initializer List: used to initialize data members in the constructor. For example,
Point::Point(int x, int y) : x(x), y(y) {
// The body runs after the member initializer list
}
The member initialiser list is placed after the function parameter list, separated by a colon. For fundamental-type data members (e.g., int, double), x(x) is the same as this->x = x. For object data members, the copy constructor will be invoked for each of the object. The function body will be executed after the member initializer list, which is empty in this case.
Alternatively, you could initialize the data members inside the constructor's body:
Point::Point(int x, int y) {
this->x = x;
this->y = y;
}
where this ->x refers to the data member x; and x refer to the function parameter x.
Another alternative that avoids naming conflict, but is hard to read:
Point::Point(int x_, int y_) {
x = x_;
y = y_;
}
Default Constructor: The default constructor refers to the constructor that takes no parameter - either it has no parameter or all parameters have their default value (e.g., the above Point's constructor). If no constructor is defined in the class, the compiler inserts a default constructor that takes no argument and does nothing (i.e., ClassName::ClassName() {}). However, if you define one (or more) constructors, compiler will not insert the default constructor. You need to define your default constructor, if desired.
Comments
Post a Comment