| Constructor
and Destructor A constructor
is a member function that has the same name as its class, and does not
have return type. Multiple constructors can be defined for each
class. If no constructor is defined for a class, the compiler will
generate a default constructor, which is really an empty constructor and
consequently does nothing. Whenever an object of a class is
created, the compiler will implicitly call a constructor for that class.
To illustrate with the previous Account
class example:
//
declaration of class Account
class
Account
{
private:
char* name; // name of the acct
holder
char* acctNum; // account number
double bal; // account balance
public:
// constructor
Account(char* name, char* acctNum, double bal);
void setName(char*); // set the value of
name
void setAcctNum(char*); // set the value of acctNum
void setBalance(double); // set the value of bal
void deposit(double);
bool withdraw(double);
// other member functions …
};
//
implementation of Account constructor
Account::Account(char*
aName, char* aAcctNum, double aBalance)
{
setName(aName);
setAcctNum(aAcctNum);
setBalance(aBalance);
}
In the above example, Account constructor
takes three arguments to initialize the three data members - name, acctName
and bal.
Now, one could create and utilize an instance
of Account as follows:
/*
create an Account object, and initialize it with the three passed ** parameters
*/
Account
acct(“John”, “012345”, 500.86);
acct.withdraw(200);
// withdraw money from the account
acct.deposit(300);
// deposit money into the account
Here one does not have to call the three
set member functions to initialize the data members, the compiler automatically
executes the constructor upon the instantiation of the object acct, and
in this particular implementation, the constructor in turn calls the three
set functions to do the initialization.
A constructor is called each time an object
is created, and C++ also supports a mechanism complementary to constructors
for the automatic cleanup of objects – a special member function called
destructor is invoked automatically by the compiler whenever an object
is destroyed (e.g., by going out of scope or by using the “delete” operator).
A destructor has the same name as its class
but is preceded by a “~” (tilde). It takes no argument and has no
return type. In addition, there can be only one destructor for each
class. The purpose of a destructor is to do cleanup (e.g., release
memory).
To continue with the Account class example,
one might define the Account class as follows:
//
declaration of class Account
class
Account
{
private:
char* name; // name of the acct
holder
char* acctNum; // account number
double bal; // account balance
public:
// constructor
Account(char* name, char* acctNum, double bal);
// destructor
~Account();
void setName(char*); // set the value of
name
void setAcctNum(char*); // set the value of acctNum
void setBalance(double); // set the value of bal
void deposit(double);
bool withdraw(double);
// other member functions …
};
//
implementation of Account destructor
Account::~Account()
{
delete[] name; // release memory allocated
for name
delete[] acctNum; // release memory allocated for acctNum
}
The “delete” in the above destructor is
a key word in C++, and its semantics is to de-allocate memory previous
allocated with a “new” statement. We shall take more about the “new”
and “delete” in the next lecture.
|