Constructors
- Class objects, like structures, must be initialized before they can be
used.
This may be accomplished by defining a member function that performs the
initialization, such as the init() member function in the
previous
example. Alternatively, a constructor may be used
- Constructors are special functions that are used to perform
initializations.
They are invoked as soon as an object is defined. They are distinct from
the
other member functions because they have the same name as the class name,
and
they do not have a return data type. Example...
- In C++, one can define multiple functions that have the same function
name. However, the functions must have different argument lists (also
known as the
function's signature. This is called function overloading
- Functions calls to overloaded functions are resolved by the compiler
based
on the function's signature
- Constructors, similar to functions, may also be overloaded.
Example...
- Constructors, similar to functions, may also be provided with default
arguments.
Example...
- Similar to built-in data types, arrays of objects can be defined.
This,however, results in the calls to the constructor for each object in
the
array. Example...
Back to Previous Page
Document:
Local Date:
Last Modified On: