Constructors with parameters

It is possible to have as many constructors as you like as long as their signatures are different, and are therefore being overloaded.

 

This allows your parameters to be set at instantiation, rather than a default set.

 

Compile & Run:

myTriangle width was set at 22.22
myTriangle height was set at 31.13
myTriangle area: 345.854

 

 

The default constructor could now be placed within the parametrised constructor to simplify:

Compile & Run:

myTriangle width was set at 22.22
myTriangle height was set at 31.13
myTriangle area: 345.854

Leave a Reply