Constructors and Destructors
7.1
Need:
When an object is created all the members of the object are allocated memory spaces.Data members are not initialized automatically. If left uninitialized these members contain garbage values. Therefore it is important that the data members are initialized to meaningful values at the time of object creation.
When a C++ program runs it invariably creates certain objects in the memory and when the program exits the objects must be destroyed so that the memory could be reclaimed for further use.
C++ provides mechanisms to cater to the above necessary activities through constructors and destructors methods.
Q. What is a constructor?
A constructor is a member function o a class that initializes objects of a class. In C++, the constructor is automatically called when the object is created. It is a special member function of the class.
A constructor is different from normal member function:
01. A constructor is a member function of a class.
02. The name of the constructor is the same as the name of the class.
03. It has no return type, so it can't use the return keyword.
04. It must be an instance member function, that is, it can never be static. (in a class two type of member function i.e. static and instance, for static we write a keyword 'static' in front of it. instance member function run for an object only and also call by objects).
05. If we do not specify a constructor, the c++ compiler generates a default constructor for us. (Except no parameter and has an empty body)
06. They should be declared in the public section
Example of constructor:
_____________________________
The use of constructor is to initialize the values at the starting whenever constructor build
_______________________________________
______
7.1 Need for Constructor and Destructor
7.1.1 Constructor
7.1.2 Destructor
7.2 Copy Constructor
7.3 Dynamic Constructors
7.4 Parameterized Constructors
7.5 Constructors with Default Arguments
7.1
Need:
When an object is created all the members of the object are allocated memory spaces.Data members are not initialized automatically. If left uninitialized these members contain garbage values. Therefore it is important that the data members are initialized to meaningful values at the time of object creation.
When a C++ program runs it invariably creates certain objects in the memory and when the program exits the objects must be destroyed so that the memory could be reclaimed for further use.
C++ provides mechanisms to cater to the above necessary activities through constructors and destructors methods.
Q. What is a constructor?
A constructor is a member function o a class that initializes objects of a class. In C++, the constructor is automatically called when the object is created. It is a special member function of the class.
A constructor is different from normal member function:
01. A constructor is a member function of a class.
02. The name of the constructor is the same as the name of the class.
03. It has no return type, so it can't use the return keyword.
04. It must be an instance member function, that is, it can never be static. (in a class two type of member function i.e. static and instance, for static we write a keyword 'static' in front of it. instance member function run for an object only and also call by objects).
05. If we do not specify a constructor, the c++ compiler generates a default constructor for us. (Except no parameter and has an empty body)
06. They should be declared in the public section
Example of constructor:
#include <iostream>
using namespace std;
class box
{
public:
int length;
int breath;
int area;
box() //constructor
{
cout << "constructor executed" << endl;
area = length * breath;
}
};
int main()
{
box b1;
b1.breath = 10;
b1.length = 10;
cout << b1.area;
}
_____________________________
The use of constructor is to initialize the values at the starting whenever constructor build
_______________________________________
#include <iostream>
using namespace std;
class student
{
public:
int std;
int age;
int rollno;
student() //constructor
{
std=5;
}
};
int main()
{
student prince;
prince.age=20;
prince.rollno=23;
cout<<"class= "<<prince.std<<endl;
student raj;
raj.rollno=23;
raj.age=15;
cout<<"class= "<<raj.std<<endl;
}
______
What is Destructor?
Destructor is a member function that destructs or deletes an object.C++ destructor is a special member function that is executed automatically when an object is destroyed that has been created by the constructor. C++ destructors are used to de-allocate the memory that has been allocated for the object by the constructor.
When is the destructor called?
The function ends.
The program ends.
A block containing local variables ends.
A delete operator is called.
How destructors are different from a normal member function?
Destructors have the same name as the class preceded by a tilde (~).
Destructors don’t take any argument and don’t return anything(not even void).
Can there be more than one destructor in a class?
No, there can only one destructor in a class with class name preceded by ~, no parameters, and no return type.
When do we need to write a user-defined destructor?
If we do not write our own destructor in class, the compiler creates a default destructor for us. The default destructor works fine unless we have dynamically allocated memory or pointer in class. When a class contains a pointer to memory allocated in class, we should write a destructor to release memory before the class instance is destroyed. This must be done to avoid a memory leak.
In the life of an object, it is the last place. It must be instance member function not static. Destructor call from last to first if multiple class or object.
Example 01
#include <iostream>
using namespace std;
class complex
{
public:
int a, b;
complex() //constructor
{
cout << "I am Constructor" << endl;
}
~complex() //constructor
{
cout << "Bye Bye I am Destructor" << endl;
}
};
int main()
{
complex c1;
}
I am Constructor
Bye Bye I am Destructor
Example 02:
#include <iostream>
using namespace std;
class complex
{
public:
int a, b;
complex() //constructor
{
cout << "I am Constructor" << endl;
}
~complex() //constructor
{
cout << "Bye Bye I am Destructor" << endl;
}
void data(int x, int y)
{
a = x;
b = y;
}
void display()
{
cout << "Value of a:" << a << endl
<< "value of b: " << b << endl;
}
};
int main()
{
complex c1;
c1.data(5, 6);
c1.display();
}
I am Constructor
Value of a:5
value of b: 6
Bye Bye I am Destructor
Example 03:
#include <iostream>
using namespace std;
class complex
{
public:
int a, b;
complex() //constructor
{
cout << "I am Constructor" << endl;
}
~complex() //constructor
{
cout << "Bye Bye I am Destructor" << endl;
}
void data(int x, int y)
{
a = x;
b = y;
}
void display()
{
cout << "Value of a:" << a << endl
<< "value of b: " << b << endl;
}
};
int main()
{
complex c1;
c1.data(5, 6);
c1.display();
complex c2;
}
I am Constructor
Value of a:5
value of b: 6
I am Constructor
Bye Bye I am Destructor
Bye Bye I am Destructor
Example 04:
#include <iostream>
using namespace std;
class complex
{
public:
int a, b;
complex() //constructor
{
cout << "I am Constructor" << endl;
}
~complex() //constructor
{
cout << "Bye Bye I am Destructor" << endl;
}
void data(int x, int y)
{
a = x;
b = y;
}
void display()
{
cout << "Value of a:" << a << endl
<< "value of b: " << b << endl;
}
};
class box
{
public:
int a;
~box()
{
cout << "Bye I am Box" << endl;
}
};
int main()
{
complex c1;
c1.data(5, 6);
c1.display();
complex c2;
box b1;
}
I am Constructor
Value of a:5
value of b: 6
I am Constructor
Bye I am Box
Bye Bye I am Destructor
Bye Bye I am Destructor
7.2 Copy Constructor
Before this topic you must know about reference variables:
The copy constructor is a constructor that creates an object by initializing it with an object of the same class, which has been created previously.
The copy constructor is used to:
01. Initialize one object from another of the same type.
02. Copy an object to pass it as an argument to a function.
03. Copy an object to return it from a function.
If a copy constructor is not defined in a class, the compiler itself defines one.If the class has pointer variables and has some dynamic memory allocations, then it is a must to have a copy constructor.
The compiler always make two constructors:-
1. Default Constructor
The default constructor is the constructor which doesn't take any argument. It has no parameter.
Syntax:
class_name(parameter1, parameter2, ...)
{
// constructor Definition
}
#include <iostream>
using namespace std;
class Cube
{
public:
int side;
Cube()
{
side = 10;
}
};
int main()
{
Cube c;
cout << c.side;
}
2. Copy Constructor
#include <iostream>
using namespace std;
class complex
{
public:
int a, b;
complex(int x, int y) //constructor
{
a = x;
b = y;
}
complex(int x) //Parameterized Constructor
{
a = x;
}
complex() //Default constructor
{
}
};
int main()
{
complex c1(2, 3);
complex c2(c1); //copy c1 into c2
cout << c2.a << " " << c2.b << endl;
}
Output:
2 3
#include <iostream>
using namespace std;
class complex
{
public:
int a, b;
complex(int x, int y) //constructor
{
a = x;
b = y;
}
complex(int x) //Parameterized Constructor
{
a = x;
}
complex() //Default constructor
{
}
complex(complex &c1) //Copy constructor
{
a = c1.a;
b = c1.b;
}
};
int main()
{
complex c1(2, 3);
complex c2(c1); //copy c1 into c2
cout << c2.a << " " << c2.b << endl;
}
output:
2 3
7.3 Dynamic Constructors
The dynamic constructor is used to allocate the memory to the objects at the run time. Memory is allocated at run time with the help of a 'new' operator.
By using this constructor, we can dynamically initialize the objects.
Example:
#include <iostream>
using namespace std;
class dynamicConstructor
{
int *ptr;
public:
dynamicConstructor()
{
ptr=new int;
*ptr=100;
}
dynamicConstructor(int v)
{
ptr=new int;
*ptr=v;
}
int display()
{
return(*ptr);
}
};
int main()
{
dynamicConstructor b1, b2(90);
cout<<"Value of b1 ptr is:"<<b1.display()<<endl;
cout<<"Value of b2 ptr is:"<<b2.display()<<endl;
}
output:
Value of b1 ptr is:100
Value of b2 ptr is:90
7.4 Parameterized Constructors
7.5 Constructors with Default Arguments
Comming Soon.............7.6 Constructor/Destructor with Static Members
#include <iostream>
using namespace std;
class man
{
static int no;
public:
man()
{
no++;
cout<<"no created="<<no<<'\n';
}
~man()
{
no--;
cout<<"no destroyed="<<no<<'\n';
}
};
int man::no=0;
int main()
{
man A,B,C;
cout<<"press any key"<<'\n';
}
OUTPUT:
By End OF Day All Topics will be Covered



Comments
Post a Comment