0% found this document useful (0 votes)
160 views14 pages

Constructor and Destructor

Constructors and destructors are special member functions in C++ used to initialize and destroy objects respectively. Constructors are invoked whenever a new object is created and are used to initialize member variables, with different types including default, parameterized, and copy constructors. Destructors are called when an object goes out of scope and are used to perform cleanup operations before the object is destroyed.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views14 pages

Constructor and Destructor

Constructors and destructors are special member functions in C++ used to initialize and destroy objects respectively. Constructors are invoked whenever a new object is created and are used to initialize member variables, with different types including default, parameterized, and copy constructors. Destructors are called when an object goes out of scope and are used to perform cleanup operations before the object is destroyed.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CONSTRUCTOR AND DESTRUCTOR

INTRODUCTION
So far we saw that, we use member functions to assign the values to the objects. Example: X.getdata(10,20); After the objects are created, then the value initialization occurs But these function can not be used to initialize the member variables at the time of creation of the objects. C++ provides a special member function that is used to initialize the objects during their creation. That is known as the constructor.

INTRODUCTION
Similarly, another member function is used to destroy the objects. That is known as destructors.

CONSTRUCTORS
Its name is the same as the class name. The constructor is invoked whenever an object of its associated class is created. It is called constructors as its constructs/initializes the objects of the class. class integer{ int m,n; public: integer(void){m=n=0;}

};
If now any object is created it will be initialized automatically.

DEFAULT CONSTRUCTOR
Till now we are using this type of statement to create objects: student A;

Here also one constructor is called by default which accepts no parameters.


That is known as default constructor.

Ex: student::student()
If no constructor is defined explicitly the compiler supplies default constructor.

The constructor should be declared in the public section, and do not have any return types.

PARAMETERIZED CONSTRUCTORS
Rather than using 0s we can use other numbers to assign them as a value.

By passing arguments to the constructor this can be done.


Then the constructor is known as the parameterized constructors. integer::integer(int x,int y){ m=x; n=y;} We have to pass the initial values as arguments to the constructor function when an object is declared.

PARAMETERIZED CONSTRUCTORS
Passing parameters can be done in two ways:

By calling the constructor explicitly


Ex: integer I=integer(0,100); By calling the constructor implicitly

Ex: integer I(0,100); Consider this example:

PARAMETERIZED CONSTRUCTORS
class integer{ int m,n; public: integer(void){ m=n=0;} integer(int x, int y){ m=x; n=y;} integer(int p) { m=n=p;} void display(){ cout<<m and n<<m<<n;}}; int main(){ integer i1, i3(0,100); integer i2=integer(20); i1.display(); i2.display(); i3.display(); return 0; }

PARAMETERIZED CONSTRUCTORS
We can not pass object of the same class as parameters.

Rather we can pass reference of any object of its own class.


Ex: class A{

------------public: A(A); }; This is Wrong

Ex: class A{ ------------public: A(A&); };

This is correct. It is also known as copy constructor.

COPY CONSTRUCTOR
Copy constructor initializes the objects during their creation by using/copying the values of another objects of its own class. class integer{ int m,n; public: integer(void){ m=n=0;} integer(int x, int y){ m=x; n=y;} integer(integer &i) { m=i.m; n=i.n;} }; int main(){ integer i1, i2(10,20); integer i3(i2); return 0; }

DESTRUCTORS
It is used to destroy the objects that have been created by a constructor.

Its name is same as constructor but preceded by a tilde.


Ex: ~integer() { }

A destructor never passes or returns any arguments.


It is called implicitly by compiler during exit of the program.

DESTRUCTORS
int count =0; class alpha {

public: alpha(){ count++;


cout<<No of object created<<count;} ~ alpha() {cout<<No of object destroyed<<count; count --;} };

DESTRUCTORS
int main(){ cout<<\n Enter main;

alpha A1,A2, A3, A4;


{ cout<<\n Enter block 1; alpha A5; } { cout<<\n Enter block 2; alpha A6; }

cout<<\n Re- Enter main;


return 0; }

Output: No. of object created = 1 No. of object created = 2 No. of object created = 3 No. of object created = 4 Enter block1 No. of object created = 5 No. of object destroyed =5 Enter block2 No. of object created = 5 No. of object destroyed = 5 Re- Enter Main No. of object destroyed = 4 No. of object destroyed = 3 No. of object destroyed = 2 No. of object destroyed = 1

You might also like