Default Constructors and Destructors
The default constructor is a special member function which is invoked by the C++ compiler without any argument for initializing the objects of a class. In other words, a default constructor function initializes the data members with no arguments. It can be explicitly written in a program. In case, a default destructor is not defined in a program, the C++ compiler automatically generates it in a program. The purpose of the default constructor is to construct a default object of the class type.
Example 7.
A program to demonstrate the default initialization of the constructor member function of a class object of the students’ information system such as name, roll number, sex, height and weight.
// demonstration of default constructor
#include <iostream>
using namespace std;
class student {
private :
char name[20];
long int rollno;
char sex;
oat height;
oat weight;
public :
student(); // constructor
void display();
};
student :: student()
{
name[0] = ‘\0’;
rollno = 0;
sex = ‘\0’;
height = 0;
weight = 0;
}
void student :: display()
{
cout << “ name = ” << name <<endl ;
cout << “ rollno = ” << rollno <<endl;
cout << “ sex = ” << sex << endl;
cout << “ height = ” << height << endl;
cout << “ weight = ” << weight << endl;
}
int main()
{
student a;
cout << “ demonstration of default constructor \n”;
a.display();
return 0;
}
Output of the above program
demonstration of default constructor
name =
roll no = 0
sex =
height = 0
weight = 0
Example 8.
A program to demonstrate the default initialization of the constructor member function of a class object where the default constructor is defined within the scope of the class definition itself.
// demonstration of default constructor
#include <iostream>
#include <string>
using namespace std;
class student {
private :
char name[20];
long int rollno;
char sex;
oat height;
oat weight;
public :
student(char na[] =“\0”,long int rn = 0,char sx = ‘\0’,
oat ht = 0, oat wt=0);// constructor
void display();
};
student:: student(char na[],long int rn,char sx, oat ht, oat wt)
{
strcpy(name, na);
rollno = rn;
sex = sx;
height = ht;
weight = wt;
}
void student :: display()
{
cout << “ name = ” << name <<endl ;
cout << “ rollno = ” << rollno <<endl;
cout << “ sex = ” << sex << endl;
cout << “ height = ” << height << endl;
cout << “ weight = ” << weight << endl;
}
int main()
{
cout << “ demonstration of default constructor \n”;
a.display();
return 0;
}
Output of the above program
demonstration of default constructor
name =
rollno = 0
sex =
height = 0
weight = 0
Example 9.
A program to demonstrate the default initialisation of the constructor member function of a class object where the default constructor is created by the compiler automatically when the default constructor is not defi ned by the user.
#include <iostream>
using namespace std;
class student {
private :
char name[20];
long int rollno;
char sex;
oat height;
oat weight;
public :
void display();
};
void student :: display()
{
cout << “ name = ” << name <<endl ;
cout << “ rollno = ” << rollno <<endl;
cout << “ sex = ” << sex << endl;
cout << “ height = ” << height << endl;
cout << “ weight = ” << weight << endl;
}
int main()
{
student a;
a.display();
return 0;
}
Output of the above program
name =
rollno = 1108544020
sex = X
height = 3.98791e-34
weight = 36.7598
Note: Automatic variables are initialised with a garbage value if it is not initialised by the user explicitly.
Overloading Constructors
The overloading constructor is a concept in OOPs in which the same constructor name is called with different arguments. Depending upon the type of argument, the constructor will be invoked automatically by the compiler to intialise the objects.
Example 10.
A program to demonstrate how to defi ne, declare and use the overloading of constructors to initialise the objects for diff erent data types.
//overloading of constructor
#include <iostream>
using namespace std;
class abc {
public:
abc();
abc(int);
abc( oat);
abc(int, oat);
};
abc :: abc()
{
cout <<“calling default constructor \n”;
}
abc :: abc (int a)
{
cout << “\n calling constructor with int \n”;
cout << “ a = ” << a << endl;
}
abc :: abc ( oat fa)
{
cout <<“\n calling constructor with oating point number \n”;
cout <<“ fa = ” << fa << endl;
}
abc :: abc(int a, oat fa)
{
cout << “ \n calling constructor with int and oat \n”;
cout << “ a = ” << a << endl;
cout << “ fa = ” << fa << endl;
}
int main()
{
abc obj;
abc(10);
abc(1.1f);
abc(20,-2.2);
return 0;
}
Output of the above program
calling default constructor
calling constructor with int
a = 10
calling constructor with oating point number
fa = 1.1
calling constructor with int and oat
a = 20
fa = -2.2
Constructors in Nested Classes
It is well known that a constructor is a special member function which is used to initialise the class objects whenever an object is created. The constructor member function can be used to initialise the class objects of nested classes. The nested class is a technique in which a class is declared as a member of another class. In other words, a class within a class is called as nested class. The scope rules to access the nested class constructor is the same as that of the member functions of the nested classes. The scope resolution operator (::) is used to identify the outer and inner class objects and the constructor member functions.
Example 11.
A program to demonstrate how to declare, defi ne and call a constructor member function in a nested class.
#include <iostream>
using namespace std;
class abc {
public:
abc();
class x {
public:
x();
};
};
abc:: abc()
{
cout << “abc - class constructor \n”;
}
abc::x :: x()
{
cout << “x - class constructor \n”;
}
int main()
{
abc obj;
abc::x obj2;
return 0;
}
Output of the above program
abc - class constructor
x - class constructor
Example 12.
A program to demonstrate how to declare, defi ne and call a constructor member function in a nested class. The fi ring order of the nested class constructor is illustrated in the following program.
#include <iostream>
using namespace std;
class abc {
public:
abc();
class x {
public:
x();
class y {
public:
y();
class z {
public:
z();
};
};
};
};
abc:: abc()
{
cout << “abc - class constructor \n”;
}
abc::x :: x()
{
cout << “x - class constructor \n”;
}
abc::x :: y :: y()
{
cout << “y - class constructor \n”;
}
abc::x ::y :: z :: z()
{
cout << “z - class constructor \n”;
}
int main()
{
abc obj1;
abc::x obj2;
abc::x::y obj3;
abc::x::y::z obj4;
return 0;
}
Output of the above program
abc - class constructor
x - class constructor
y - class constructor
z - class constructor
DESTRUCTORS
A destructor is a function that automatically executes when an object is destroyed. A destructor function gets executed whenever an instance of the class to which it belongs goes out of existence. The primary usage of the destructor function is to release space on the heap. A destructor function may be invoked explicitly.
Syntax Rules for Writing a Destructor Function The rules for writing a destructor function are:
- A destructor function name is the same as that of the class it belongs except that the first character of the name must be a tilde (~).
- It is declared with no return types (not even void) since it cannot ever return a value.
- It cannot be declared static, const or volatile.
- It takes no arguments and therefore cannot be overloaded.
- It should have public access in the class declaration.
When the Destructor Function is Invoked Under the following circumstances, a destructor function is invoked:
- After the end of main () for all static, local to main () and global instances of class.
- At the end of each block containing the auto variable of class.
- At end of each function containing an argument of class.
- To destroy any unnamed temporaries of class after their use.
- When an instance of class allocated on the heap is destroyed via delete.
Example 13.
A program to simulate a simple banking system in which the initial balance and the rate of interest are read from the keyboard and these values are initialized using the constructor member function. The destructor member function is defined in this program to destroy the class objects created using the constructor member function. The program consists of the following methods:
- To initialize the balance and the rate of interest using the constructor member function.
- To make a deposit.
- To withdraw an amount from the balance.
- To find the compound interest based on the rate of interest.
- To know the balance amount.
- To display the menu options.
- To destroy the object of class, the destructor member function is defined.
//program for class demonstration
// constructor and destructor
#include <iostream>
using namespace std;
class account {
private :
oat balance;
oat rate;
public :
account(); // constructor
~account(); // destructor
void deposit ();
void withdraw ();
void compound();
void getbalance();
void menu();
}; //end of class de nition
account :: account() // constructor
{
cout << “ enter the initial balance \n”;
cin >> balance;
cout << “ interest rate \n”;
cin >> rate;
}
account :: ~account() // constructor
{
cout << “ data base has been deleted \n”;
}
//deposit
void account :: deposit ()
{
oat amount;
cout << “ enter the amount ”;
cin >> amount;
balance = balance+amount;
}
//attempt to withdraw
void account :: withdraw ()
{
oat amount;
cout << “ how much to withdraw ? \n”;
cin >> amount;
if (amount <= balance) {
balance = balance-amount;
cout << “ amount drawn = ” <<amount << endl;
cout << “ current balance = ” << balance << endl;
}
else
cout << 0;
}
void account :: compound ()
{
oat interest;
interest = balance*rate;
balance = balance+interest;
cout << “interest amount = ” << interest <<endl;
cout << “ toal amount = ” << balance << endl;
}
void account :: getbalance()
{
cout << “ Current balance = ” ;
cout << balance << endl;
}
void account :: menu()
{
cout << “ d -> deposit\n”;
cout << “ w -> withdraw\n”;
cout << “ c -> compound interest\n”;
cout << “ g -> get balance\n”;
cout << “ m -> menu \n”;
cout << “ q -> quit \n”;
cout << “ option, please ? \n”;
}
int main()
{
account acct;
char ch;
acct.menu();
while ( (ch = cin.get()) != ‘q’) {
switch (ch) {
case ‘d’ :
acct.deposit();
break;
case ‘w’ :
acct.withdraw();
break;
case ‘c’ :
acct.compound();
break;
case ‘g’ :
acct.getbalance();
break;
case ‘m’:
acct.menu();
break;
} // end of switch statement
}
cout << endl;
return 0;
}
Output of the above program
enter the initial balance
1000
interest rate
0.2
d -> deposit
w -> withdraw
c -> compound interest
g -> get balance
m -> menu
q -> quit
option, please?
d
enter the amount
500
g
Current balance = 1500
w
how much to withdraw?
1000
amount drawn = 1000
Current balance = 500
c
interest amount = 100
toal amount = 600
q
data base has been deleted
Destructors in Nested Classes
It is well known that a destructor is a special member function which is used to destroy the class objects automatically whenever an object is released. The destructor member function can also be used to implement the nested class objects. The fi ring order of destructor under nested class is that the innermost class object will be released fi rst and so on. The scope rules to access the nested class destructor is the same as that of the member functions of the nested classes. The scope resolution operator (::) is used to identify the outer and inner class objects and the constructor/destructor member functions.
Example 14.
A program to demonstrate how to declare, defi ne and call a destructor member function in a nested class.
#include <iostream>
using namespace std;
class abc {
public:
~abc();
class x {
public:
~x();
class y{
public:
~y();
class z {
public:
~z();
};
};
};
};
abc:: ~abc()
{
cout << “abc - class destructor \n”;
}
abc::x :: ~x()
{
cout << “x - class destructor \n”;
}
abc::x ::y :: ~y()
{
cout << “y - class destructor \n”;
}
abc::x ::y :: z :: ~z()
{
cout << “z - class destructor \n”;
}
int main()
{
abc obj1;
abc::x obj2;
abc::x::y obj3;
abc::x::y::z obj4;
return 0;
}
Output of the above program
z - class destructor
y - class destructor
x - class destructor
abc - class destructor
Example 15.
A program to demonstrate how to declare, defi ne and call a constructor and a destructor member function in a nested class. This program illustrates the fi ring order of constructor and destructor under the nested class.
#include <iostream>
using namespace std;
class abc {
public:
abc();
~abc();
class x {
public:
x();
~x();
class y{
public:
y();
~y();
class z {
public:
z();
~z();
};
};
};
};
abc:: abc()
{
cout << “abc - class constructor \n”;
}
abc:: ~abc()
{
cout << “abc - class destructor \n”;
}
abc::x :: x()
{
cout << “x - class constructor \n”;
}
abc::x :: ~x()
{
cout << “x - class destructor \n”;
}
abc::x ::y :: y()
{
cout << “y - class constructor \n”;
}
abc::x ::y :: ~y()
{
cout << “y - class destructor \n”;
}
abc::x ::y :: z :: z()
{
cout << “z - class constructor \n”;
}
abc::x ::y :: z :: ~z()
{
cout << “z - class destructor \n”;
}
int main()
{
abc obj1;
abc::x obj2;
abc::x::y obj3;
abc::x::y::z obj4;
return 0;
}
Output of the above program
abc - class constructor
x - class constructor
y - class constructor
z - class constructor
z - class destructor
y - class destructor
x - class destructor
abc - class destructor

0 comments
Post a Comment