0% found this document useful (0 votes)
18 views12 pages

Constructors in CPP

Chapter 3 discusses constructors and destructors in C++. It explains the types of constructors: default, parameterized, and copy constructors, along with the concept of constructor overloading. Additionally, it describes destructors, their naming convention, and their role in cleaning up resources when objects are no longer needed.

Uploaded by

revathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views12 pages

Constructors in CPP

Chapter 3 discusses constructors and destructors in C++. It explains the types of constructors: default, parameterized, and copy constructors, along with the concept of constructor overloading. Additionally, it describes destructors, their naming convention, and their role in cleaning up resources when objects are no longer needed.

Uploaded by

revathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Chapter 3

Constructors & Destructors


Constructors
• A class constructor is a special member function of
a class that is executed whenever we create new
objects of that class.
• A constructor will have exact same name as the
class and it does not have any return type at all, not
even void.
• Constructors can be very useful for setting initial
values for certain member variables.
• Constructors can be defined either inside the class
definition or outside class definition using class
name and scope resolution :: operator.
class A {
int i;
public: A(); //Constructor declared
};
A::A() // Constructor definition
{
i=1;
}
Types of Constructors
Constructors are of three types :
• Default Constructor
• Parameterized Constructor
• Copy Constructor
Default constructor
• Default constructor is the constructor which doesn't
take any argument.
• If the programmer does not specify the constructor
in the program then compiler provides the default
constructor automatically.
• Default constructor get called automatically when
objects of class get created.
• We don’t need to call default constructor explicitly.
Like using object name and (.) operator
Parameterized Constructor
• These are the constructors with parameter. Using
this Constructor you can provide different values to
data members of different objects, by passing the
appropriate values as argument.
• This constructor is can’t be called using object name
and (.) operator.
• We have to call this constructor when object is
created & by specifying appropriate parameters
Copy constructor
The copy constructor is a constructor
which creates an object by initializing it
with an object of the same class, which
has been created previously.
The copy constructor is used to:

Initialize one object from another of the
same type.

copy an object to pass it as an argument
to a function.
Multiple constructor in
class/constructor overloading
C++ permits us to use all these constructor in the
same class.
Example
class Integer
{
int m,n;
public:
integer(); // default constructor
integer(int a,int b); //parameterized constructor
integer(integer); // copy constructor
When more than one constructor function
is defined in the class it is known as
Constructor overloading
Constructor with default
argument

It is possible to define constructor with default argument.
Eg.
complex(int a,int b=0);

When we call that constructor it is not necessary to pass
two argument to the constructor.

It will automatically consider default argument.

If you have specified the default argument and you are
passing required parameter to constructor( two value)
then it will overwrite that default argument.
Destructor

A destructor is used to destroy the objects that
have been created by a constructor.

Like a constructor the destructor is a member
function whose name is same as the class
name but is preceded by a tilde sing (~).
Eg. ~student();

Destructor never takes any argument nor does
it return any value

It will be involved implicitly by the compiler
when control get exit from the program.

It clans up storage that is no longer
accessible.

You might also like