16.7 Primary and Secondary Constructors
A class will often need to perform some initialization tasks at the point of creation. These tasks can be implemented using constructors within the class. In the case of the BankAccount class, it would be useful to be able to initialize the account number and balance properties with values when a new class instance is created. To achieve this, a secondary constructor can be declared within the class header as follows:
class BankAccount {
var accountBalance: Double = 0.0
var accountNumber: Int = 0
constructor(number: Int, balance: Double) {
accountNumber = number
accountBalance = balance
}
.
.
}
When creating an instance of the class, it will now be necessary to provide...