Cpp Note - sonhongly/MyC- GitHub Wiki
Welcome to the MyC- wiki!
1. Constructor
When an object of the class is defined, the compiler will invoke the default constructor implicitly to construct its object of the class type. There are two types of constructors in C++: default constructor and custom constructor.
1.1 Default constructor
It is the special member function of the class with no return type and no parameters which is implemented to construct its object. Example of declaration of a default constructor:
class Cube {
public:
Cube();
private:
float length_;
}
1.2 Custom constructor
Like the default constructor, it is a special member function with no return type, but it has one or more parameters. The custom constructor is also called a parameter constructor.
class Cube {
public:
Cube();
Cube(float l): length(l) {}; // custom constructor
private:
float length_;
}
2. Copy constructor
A copy constructor is a special member function that exists to make a copy of an existing object.
2.1 Automatic copy constructor
If no custom copy constructor is provided, the compiler will provide the automatic copy constructor for the object which copies attributes from another existing object.
2.2 Custom copy constructor
A custom copy constructor is a special member function with exactly one parameter of the same type as the class. Notice that, the parameter must be a const reference.
Example of a custom copy constructor:
Cube::Cube(const Cube & obj);
2.3 Copy constructor invocation
Often, the copy constructor is invoked automatically by:
- Passing an object as the argument
- Initializing a new object
- Returning an object from a function