Friday, 20 March 2026

Special Member Functions in C++

 
Special Member Functions in C++
The special member functions are a set of functions that can be declared only as class members and invoked automatically by the C++ compiler. These functions affect the way objects of a given class are created, destroyed, copied, and converted into objects of other types. Another important property of many of these functions is that they can be called implicitly (by the compiler). The following is a list of the special member functions that are defi ned, declared and used in C++:

(1) Constructors
(2) Destructors
(3) Conversion functions
(4) Operator new function
(5) Operator delete function
(6) Assignment operator (operator=) function

(1) Constructors: The constructors are the special member functions that are used in class objects to enable automatic initialization of objects.
(2) Destructors: The destructors are used to perform clean up after objects are explicitly or implicitly destroyed.
(3) Conversion Functions: Objects of a given class type can be converted to objects of another type. This is done by constructing an object of the target class type from the source class type and copying the result to the target object. This process is called conversion by constructor. Objects can also be converted by user supplied conversion functions.
(4) Operator New Function: C++ supports dynamic allocation and deallocation of objects using the new and delete operators. These operators allocate memory for objects from a pool called the free store. The new operator is the special member function which is used for dynamically allocates storage for class objects.
(5) Operator Delete Function: The operator delete function is used to release storage allocated using the new operator. The delete operator calls the operator delete function, which frees memory back to the available pool.
(6) Assignment Operator (Operator=) Function: The assignment operator (operator=) function is used when an assignment takes place between class objects.
Constructors
A constructor is a special member function for automatic initialisation of an object. Whenever an object is created, the special member function, i.e., is the constructor will be executed automatically. A constructor function is different from all other nonstatic member functions in a class because it is used to initialise the variables of whatever instance being created. Note that a constructor function can be overloaded to accommodate many different forms of initialisation.
Syntax rules for writing constructor functions The following rules are used for writing a constructor function:
  • A constructor name must be the same as that of its class name.
  • It is declared with no return type (not even void).
  • It cannot be declared const or volatile but a constructor can be invoked for a const and volatile objects.
  • It may not be static.
  • It may not be virtual.
  • It should have public or protected access within the class and only in rare circumstances it should be declared private.
The general syntax of the constructor function in C++ is,
class class_name {
private :
-------
-------
protected :
-------
-------
public :
class_name(); // constructor
-------
-------
};
class_name :: class_name()
{
-------
-------
}
The following examples illustrate the constructor declaration in a class defi nition.
(1)
class employee {
private :
char name[20];
int ecode;
char address[20];
public :
employee(); // constructor
void getdata();
void display ();
};
employee() :: employee(); // constructor
{
-------
-------
}
(2)
class account {
private :
oat balance;
oat rate;
public :
account() //constructor
{
-------
-------
}
void create_acct();
};

Example 1.
A program to demonstrate how to use a special member function, namely, constructor in C++.
#include <iostream>
using namespace std;
class abc
{
public:
abc() {
cout << “for class constructor\n”;
}
};
int main()
{
abc obj;
return 0;
}
Output of the above program
for class constructor

Example 2.
A program to generate a series of Fibonacci numbers using the constructor where the constructor member function has been defi ned in the scope of class defi nition itself.
//generation of the bonacci series using
// constructor
#include <iostream>
using namespace std;
class bonacci {
private :
unsigned long int f0,f1, b;
public :
bonacci () // constructor
{
f0 = 0;
f1 = 1;
cout << “Fibonacci series of rst 10 numbers\n”;
cout << f0 << ‘\t’ << f1 <<‘\t’;
b = f0+f1;
}
void increment ()
{
f0 = f1;
f1 = b;
b = f0+f1;
}
void display()
{
cout << b << ‘\t’;
}
}; // end of class construction
int main()
{
bonacci number;
for (int i = 3; i <= 10; ++i) {
number.display();
number.increment();
}
return 0;
}
Output of the above program
Fibonacci series of rst 10 numbers
0 1 1 2 3 5 8 13 21 34

Example 3.
A program to display a series of Fibonacci numbers using the constructor where the constructor member function has been defi ned out of the class defi nition using the scope resolution operator.
//generation of the bonacci series using
// constructor using scope resolution operator
#include <iostream>
using namespace std;
class bonacci {
private :
unsigned long int f0,f1, b;
public :
bonacci (); // constructor
void increment ();
void display() ;
}; // end of class construction
bonacci :: bonacci() // constructor
{
cout << “Fibonacci series of rst 10 terms\n”;
f0 = 0;
f1 = 1;
b = f0+f1;
cout << f0 << ‘\t’ << f1 << ‘\t’;
}
void bonacci :: increment ()
{
f0 = f1;
f1 = b;
b = f0+f1;
}
void bonacci ::display()
{
cout << b << ‘\t’;
}
int main()
{
bonacci number;
for (int i = 3; i <= 10; ++i) {
number.display();
number.increment();
}
return 0;
}
Output of the above program
Fibonacci series of rst 10 terms
0 1 1 2 3 5 8 13 21 34

Example 4.
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 initialised using the constructor member function. The program consists of the following methods:
  • To initialise the balance amount and the rate of interest using the constructor member function
  • To make a deposit
  • To withdraw an amount from the balance
  • To fi nd the compound interest based on the rate of interest
  • To know the balance amount
  • To display the menu options
//demonstration of constructor
//simulation of simple banking system
#include <iostream>
using namespace std;
class account {
private :
oat balance ;
oat rate;
public :
account(); // constructor
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 in decimal\n”;
cin >> rate;
}
//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 << “ q -> quit\n”;
cout << “ m -> menu\n”;
cout << “ option, please ? \n”;
}
int main()
{
class 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
}
return 0;
}
Output of the above program
enter the initial balance
1000
interest rate in decimal
0.2
d -> deposit
w -> withdraw
c -> compound interest
g -> get balance
q -> quit
m -> menu
option, please?
g
Current balance = 1000
d
enter the amount : 1000
g
Current balance = 2000
w
how much to withdraw?
500
amount drawn = 500
current balance = 1500
c
interest amount = 300
toal amount = 1800
g
Current balance = 1800
q

Copy Constructors
Copy constructors are always used when the compiler has to create a temporary object of a class object.
The copy constructors are used in the following situations:
  • The initialization of an object by another object of the same class.
  • Return of objects as a function value.
  • Stating the object as by value parameters of a function.
The copy constructor can accept a single argument of reference to same class type. The purpose of the copy constructor is copy objects of the class type. The general format of the copy constructor is, 
class_name :: class_name (class_name &ptr)

Normally, the copy constructors take an object of their own class as arguments and produce such an object. The copy constructor usually do not return a function value as constructors cannot return any function values. The following program segment demonstrates how to define a copy constructor for fi nding a series of Fibonacci numbers.
bonacci :: bonacci() // constructor
{
f0 = 0;
f1 = 1;
b = f0+f1;
}
bonacci :: bonacci( bonacci &ptr) //copy constructor
{
f0 = ptr.f0;
f1 = ptr.f1;
b = ptr. b;
}

Example 5.
A program to generate a series of Fibonacci numbers using a copy constructor where the copy constructor is defi ned within the class declaration itself.
//generation of the bonacci series using
// copy constructor
#include <iostream>
#include <iomanip>
using namespace std;
class bonacci {
public :
unsigned long int f0,f1, b;
bonacci ()
{
f0 = 0;
f1 = 1;
b = f0+f1;
}
bonacci ( bonacci &ptr) {
f0 = ptr.f0;
f1 = ptr.f1;
b = ptr. b;
}
void increment ()
{
f0 = f1;
f1 = b;
b = f0+f1;
}
void display()
{
cout << setw(4) << b;
}
}; // end of class construction
int main()
{
int n;
bonacci obj;
cout << “ How many numbers are to be displayed \n”;
cin >> n;
cout << obj.f0 << setw(4) << obj.f1;
for (int i = 2; i <= n-1; ++i) {
obj.display();
obj.increment();
}
cout << endl;
return 0;
}
How many numbers are to be displayed
8
0 1 1 2 3 5 8 13

Example 6.
A program to generate a series of Fibonacci numbers using a copy constructor where the copy constructor is defi ned out of the class declaration using scope resolution operator.
//generation of the bonacci series using
// copy constructor using scope resolution operator
#include <iostream>
#include <iomanip>
using namespace std;
struct bonacci {
public :
unsigned long int f0,f1, b;
bonacci (); // constructor
bonacci( bonacci &ptr);// copy constructor
void increment ();
void display() ;
}; // end of class construction
bonacci :: bonacci() // constructor
{
f0 = 0;
f1 = 1;
b = f0+f1;
}
bonacci :: bonacci( bonacci &ptr) //copy constructor
{
f0 = ptr.f0;
f1 = ptr.f1;
b = ptr. b;
}
void bonacci :: increment ()
{
f0 = f1;
f1 = b;
b = f0+f1;
}
void bonacci :: display()
{
cout << setw(4) << b;
}
int main()
{
int n;
bonacci obj;
cout << “How many numbers are to be displayed ?\n”;
cin >> n;
cout << obj.f0 << setw(4) << obj.f1;
for (int i = 2; i <= n-1; ++i) {
obj.display();
obj.increment();
}
cout << endl;
return 0;
}
Output of the above program
How many numbers are to be displayed?
6
0 1 1 2 3 5

0 comments

Post a Comment