0% found this document useful (0 votes)
23 views6 pages

Constructor and Destructor

Uploaded by

kinzasarfaraz521
Copyright
© © All Rights Reserved
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)
23 views6 pages

Constructor and Destructor

Uploaded by

kinzasarfaraz521
Copyright
© © All Rights Reserved
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/ 6

Constructor and Destructor

Constructor:
A constructor is a member function of a class that has the same name as
the class name. It helps to initialize the object of a class. It can either
accept the arguments or not. It is used to allocate the memory to an
object of the class. It is called whenever an instance of the class is
created. It can be defined manually with arguments or without
arguments. There can be many constructors in a class. It can be
overloaded but it can not be inherited or virtual. There is a concept of
copy constructor which is used to initialize an object from another object.
Syntax:
ClassName()
{
//Constructor's Body
}

Destructor:
Like a constructor, Destructor is also a member function of a class that
has the same name as the class name preceded by a tilde(~) operator. It
helps to deallocate the memory of an object. It is called while the object
of the class is freed or deleted. In a class, there is always a single
destructor without any parameters so it can’t be overloaded. It is always
called in the reverse order of the constructor. if a class is inherited by
another class and both the classes have a destructor then the destructor
of the child class is called first, followed by the destructor of the parent
or base class.
Syntax:
~ClassName()
{
//Destructor's Body
}
Note: If we do not specify any access modifiers for the members inside
the class then by default the access modifier for the members will be
Private.

Example/Implementation of Constructor and Destructor:


#include <iostream>
using namespace std;

class Z
{
public:
// constructor
Z()
{
cout<<"Constructor called"<<endl;
}

// destructor
~Z()
{
cout<<"Destructor called"<<endl;
}
};

int main()
{
Z z1; // Constructor Called
int a = 1;
if(a==1)
{
Z z2; // Constructor Called
} // Destructor Called for z2
} // Destructor called for z1

Output:
Constructor called
Constructor called
Destructor called
Destructor called

Difference between Constructor and Destructor in C++ :

S.
No. Constructor Destructor

Constructor helps to initialize the object Whereas destructor is used to


1.
of a class. destroy the instances.

It is declared as className( arguments Whereas it is declared as ~


2.
if any ){Constructor’s Body }. className( no arguments ){ }.

Constructor can either accept arguments While it can’t have any


3.
or not. arguments.

A constructor is called when an instance It is called while object of the


4.
or object of a class is created. class is freed or deleted.

Constructor is used to allocate the While it is used to deallocate the


5.
memory to an instance or object. memory of an object of a class.

6. Constructor can be overloaded. While it can’t be overloaded.

Here, its name is also same as the


The constructor’s name is same as the
7. class name preceded by the tiled
class name.
(~) operator.

In a class, there can be multiple While in a class, there is always a


8.
constructors. single destructor.

There is a concept of copy constructor


While here, there is no copy
9. which is used to initialize an object from
destructor concept.
another object.

They are often called in reverse


10. They are often called in successive order.
order of constructor.
Constructor Overloading in C++

In C++, we can have more than one constructor in a class with same
name, as long as each has a different list of arguments. This concept is
known as Constructor Overloading and is quite similar to function
overloading.

• Overloaded constructors essentially have the same name (exact


name of the class) and different by number and type of
arguments.
• A constructor is called depending upon the number and type of
arguments passed.
• While creating the object, arguments must be passed to let
compiler know, which constructor needs to be called.

//C++ program to illustrate Constructor overloading


#include <iostream>
using namespace std;

class construct
{

public:
float area;

// Constructor with no parameters


construct()
{
area = 0;
}

// Constructor with two parameters


construct(int a, int b)
{
area = a * b;
}
void disp()
{
cout<< area<< endl;
}
};

int main()
{
// Constructor Overloading
// with two different constructors
// of class name
construct o;
construct o2( 10, 20);

o.disp();
o2.disp();
return 1;
}

Output:
0
200

Default Constructor
A constructor without any arguments or with the default value for every
argument is said to be the Default constructor.
A constructor that has zero parameter list or in other sense, a constructor
that accepts no arguments is called a zero-argument constructor or
default constructor.
If default constructor is not defined in the source code by the
programmer, then the compiler defines the default constructor implicitly
during compilation.
If the default constructor is defined explicitly in the program by the
programmer, then the compiler will not define the constructor implicitly,
but it calls the constructor implicitly.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy