Constructor Overloading

Like an ordinary method, a construction can be overloaded. This means that you can have different constructors following the rules of overloading a function. Since we saw that a constructor can be used to initialize the member variables of its object, you can use multiple constructors to apply different initializations.

If you declare a constructor as

TBrick Foundation;

you can use it to call other method members of the same object. The problem is that if you just try to call a method that displays the values of the member variables, you will get bizarre and unpredictable results. Consider the TBrick object created as

And implemented as

If you declare the TBrick object using the default constructor, and decide to call a method that displays the variables values, you could write it like this:

This would produce the following result:

As you can see, these values do not make sense to us.

To make sure that a calling function does not have to supply values for the member variables, you can also use the empty constructor to supply default values to these variables. If you simply use the default constructor to get the values of the member variables, the object would use the values given in the empty constructor and perform all necessary operations:

This time, the same program would produce a sound result:

This technique of using the default constructor allows you to conveniently supply default values for the member variables. As flexible as this is, you can use a certain constructor to initialize just one of the member variables and supply default values for the others. When constructing a brick, one of the dimensions would be of primary importance because it influences what the brick is used for. On this exercise, let’s allow a calling function to supply the length of the brick while we control the other two dimensions. We can declare more than one constructor in the header file:

Brick Unit – Header File: Brick.h

Brick Unit – Source File: Brick.cpp

Since this constructor takes one argument, when declaring an object that would use this constructor, assign only one value to the argument variable. Such a value is provided in the parentheses allocated to the instance of the object. Here is an example:

Main File: Main.cpp

This would produce the following result:

If you declare different constructors with different arguments to initialize (remember the rules of function overloading), when declaring these objects, make sure you initialize each instance with the right number of arguments; otherwise, the compiler would complain.

Here is our program that makes use of three constructors, each with different arguments:

Brick Unit – Header File: Brick.h

The new constructor can be implemented as follows:

Main File: Main.cpp

This would produce:

If you create an object and create it with only one constructor, if you create this constructor with at least one argument, the default constructor would not be available anymore. For example if you create a TBrick object as follows:

and implement the TBrick(double L, double h, double t) constructor as we saw above, the following main() function would halt the program:

Therefore, if you want to access a default constructor of an object, you have two alternatives:

  • If you don’t create any constructor at all on an object, the default constructor would always be available whenever you invoke that object.
  • If you create at least one constructor on an object and supply at least one argument to that constructor, you must explicitly create a default constructor for your object.

Based on what we have learned already, here is our complete program with set and get methods.

Brick Unit – Header File: Brick.h

Brick Unit – Source File: Brick.cpp

Main File: Main.cpp

Here is an example of running the program:

Share
Tweet
Share
Pin