3.11 Multiple Constructors


A constructor can take parameters to initialise the state. However, it is possible for the initial state to be determined by different sets of parameters.

For example: you might wish to set up an account, by specifying:

  • The account number, name, balance.

  • The account number, name, (no balance, defaults to 0.0).

  • The account number, name, initial balance, and a referee.

  • etc.

So it is useful that this constructor can be overloaded. So for the Account class:

 1 
 2  // Basic Bank Account Example demonstrating multiple constructors
 3  
 4  #include<iostream>
 5  #include<string>
 6  
 7  using namespace std;
 8  
 9  class Account{
10 
11   protected:
12 
13     int accountNumber;
14     float balance;
15     string owner;
16 
17   public:
18 
19     Account(string owner, float aBalance, int anAccountNumber);
20     Account(float aBalance, int anAccountNumber);
21     Account(int anAccountNumber);
22 
23     virtual void display();
24     virtual void makeLodgement(float);
25     virtual void makeWithdrawal(float);
26  };
27 
28  Account::Account(string anOwner, float aBalance, int anAccNumber):
29 	accountNumber(anAccNumber), balance(aBalance),
30 	owner (anOwner) {}
31 
32  Account::Account(float aBalance, int anAccNumber) :
33 	accountNumber(anAccNumber), balance(aBalance),
34 	owner ("Not Defined") {}
35 
36  Account::Account(int anAccNumber):
37 	accountNumber(anAccNumber), balance(0.0f),
38 	owner ("Not Defined") {}
39 
40 
41  void Account::display(){;
42     cout << "account number: " <<accountNumber
43          << " has balance: " << balance << " Euro" << endl;
44     cout << "This account is owned by: " << owner << endl;
45  }
46 
47  void Account::makeLodgement(float amount){
48     balance = balance + amount;
49  }
50 
51  void Account::makeWithdrawal(float amount){
52     balance = balance - amount;
53  }
54  
55  int main()
56  {
57   Account a = Account(10.50, 123456);  
58   a.display();
59 
60   Account b = Account("Derek Molloy", 35.50, 123457);  
61   b.display();
62  }
63 

The full source code for this example is in AccountMultipleConstructor.cpp

Figure 3.9. The Account class with Multiple Constructors example output.

The Account class with Multiple Constructors example output.

You can see from Figure 3.9, “The Account class with Multiple Constructors example output.” that the output for the a state when the display() method is called, displays the string "Not Defined", as assigned in the second constructor.

A destructor cannot be over-loaded. There can only be one destructor, as destructors do not take any parameters.

In C++, neither constructors or destructors are inherited by derived classes. i.e. if you define a particular constructor in the base class that takes 3 parameters, then the derived class does not have this constructor unless it explicitly defines it.




These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way.
ċ
AccountMultipleConstructor.cpp
(1k)
Derek Molloy,
19 Oct 2013, 05:10
Comments