Function Overloading
Overloading constructor functions
• There are three main reasons why we will want to
overload a constructor function:
-- to gain flexibility
-- to support array
-- to create copy constructor
• If a program attempts to create an object for which
no matching constructor is found, a compile-time
error occurs.
Overloading Constructor Functions
Example:
Overloading Constructor Functions
Overloading Constructor Functions
Another common reason constructor functions are
overloaded to allow both individual objects and array
of objects to occur within a program.
Overloading Constructor Functions
Creating and Using a Copy Constructor
Preceding chapter shown, problems can occur when
an object is passed to or returned from a function.
• One way to avoid these problems to define a copy
constructor.
Creating and Using a Copy Constructor
• It is important to understand that C++ defines two
distinct types of situations in which the value of one
object is given to another.
• The first situation is assignment.
• The second situation is initialization which can
occur three ways:
1. when an object is used to initialize another in a
declaration statement.
2. when an object is passed as a parameter to a
function, and
3. a temporary object is created for use as a return
value of a function.
Creating and Using a Copy Constructor
• The copy constructor only applies to initializations.
• It does not apply to assignment.
• By default, when an initialization occurs, the
compiler will automatically provide a bitwise copy.
• However, it is possible to specify precisely how one
object will initialize another by defining a copy
constructor.
• Once defined, the copy constructor is called
whenever an object is used to initialize another.
Creating and Using a Copy Constructor
• The most common form of copy constructor is
shown here:
Creating and Using a Copy Constructor
• For example, assuming a class called myclass, and
y is an object of type myclass, the following
statements would invoke the myclass copy
constructor.
• In the first two cases, a reference to y would be
passed to a copy constructor.
• In the third, a reference to the object returned by
func( ) is passed to the copy constructor.
Creating and Using a Copy Constructor
• Here is an example that illustrates why an explicit
copy constructor function is needed.
Creating and Using a Copy Constructor
• The copy constructor is called only for
initialization.
• For example, the following sequence does not call
the copy constructor defined in the preceding
program:
Creating and Using a Copy Constructor
• To see how the copy constructor helps prevent
some of the problems associated with passing
certain types of objects to functions, consider the
following (incorrect ) program.
Creating and Using a Copy Constructor
Creating and Using a Copy Constructor
Creating and Using a Copy Constructor
• The solutions to the preceding problem is to define
a copy constructor for the strtype class that allocate
memory for the copy when the copy is created.
Creating and Using a Copy Constructor
Creating and Using a Copy Constructor
Creating and Using a Copy Constructor
Using Default Arguments
• It allows you to give a parameter a default value
when on corresponding argument is specified when
the function is called.
• For example, this function gives its two parameters
default values of 0:
• This function can be called three different ways:
Using Default Arguments
• Example
Using Default Arguments
• To understand how default arguments are related to
function overloading
Using Default Arguments
Using Default Arguments
• It is not only legal to give constructor functions
default arguments, it is also common.
Using Default Arguments