0% found this document useful (0 votes)
8 views

What is constructor

A constructor is a special member function in C++ that initializes objects of a class and has the same name as the class without a return type. There are three types of constructors: default, parameterized, and copy constructors, each serving different purposes for object initialization. A destructor, which also shares the class name but is prefixed with a tilde (~), is used to destroy objects and has specific properties such as no arguments and no return type.

Uploaded by

jazz202426
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

What is constructor

A constructor is a special member function in C++ that initializes objects of a class and has the same name as the class without a return type. There are three types of constructors: default, parameterized, and copy constructors, each serving different purposes for object initialization. A destructor, which also shares the class name but is prefixed with a tilde (~), is used to destroy objects and has specific properties such as no arguments and no return type.

Uploaded by

jazz202426
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

What is constructor?

A constructor is a member function of a class which initializes objects of a class. In


C++, Constructor is automatically called when object (instance of class) create. It is
special member function of the class.

How constructors are different from a normal member function?

A constructor is different from normal functions in following ways:


 Constructor has same name as the class itself
 Constructors don’t have return type
 A constructor is automatically called when an object is created.
 If we do not specify a constructor, C++ compiler generates a default constructor for
us (expects no parameters and has an empty body).
 A destructor should be declared in the public section of the class.

Types of Constructors
Constructors are of three types:

1. Default Constructor
2. Parametrized Constructor
3. Copy Constructor
Default Constructors:
Default constructor is the constructor which doesn't take any argument. It has no
parameter.
Syntax:

(inside the class)

class <class_name>

private:

//data member

Public:

class_name()

//body

};

(outside the class)


class <class_name>

private:

//data member

Public:

class_name();

};

Class_name :: class_name()

//body

}
Parameterized Constructor:

A constructor which has parameters is called parameterized constructor. It is used to provide


different values to distinct objects.

Syntax:

class <class_name>

private:

//data member

Public:

class_name(arguments);

};

Class_name :: class_name(arguments)

//body

}
Copy Constructor:

A copy constructor is a member function which initializes an object using another


object of the same class.

Syntax:

class <class_name>

private:

//data member

Public:

class_name(class_name &obj)

//body

};
Program:

#include<iostream.h>
#include<conio.h>
class integer
{
int x,y;
public:
integer(int a, int b)
{
x=a;
y=b;
}
integer(integer & cp)
{
x=cp.x;
y=cp.y;
}
void show()
{
cout<<”x is=”<<x;
cout<<”y is=”<<y;
}
};
void main()
{
integer ob(20,70);
integer cp=ob;
ob.show();
cp.show();
getch();
}
What is destructor?

Destructor is a member function which destructs or deletes an object. A


destructor is defined like constructor. It must have same name as class. But it is
prefixed with a tilde sign (~).

Syntax:

inside the class


~constructor-name()

outside the class


class_name::~constructor-name();

Properties of Destructor:

 Destructor function is automatically invoked when the objects are destroyed.


 It cannot be declared static or const.
 The destructor does not have arguments.
 It has no return type not even void.
 A destructor should be declared in the public section of the class.
 The programmer cannot access the address of destructor.
Program:

#include <iostream.h>
#include<conio.h>
class Employee
{
public:
Employee()
{
cout<<"Constructor Invoked"<<endl;
}
~Employee()
{
cout<<"Destructor Invoked"<<endl;
}
};
void main()
{
Employee e1; //creating an object of Employee
Employee e2; //creating an object of Employee
getch();
}

Output:

Constructor Invoked
Constructor Invoked
Destructor Invoked
Destructor Invoked
Similarity of Constructor and Destructor
1.They Both are called automatically.

→ Constructor :- Object is created.

→ Destructor:- Scope of Object is over.

2. No Return Type (Not Even Void).

3. Programmers cannot access addresses of constructors and destructors.

4. Same name as class name. Only difference(syntax-wise) is Destructor has ~


before it.

Difference between Constructor and Destructor


S.NO Constructor Destructor
1. Constructor helps to initialize the object of a Whereas destructor is used to
class. destroy the instances.
2. It is declared as Classname( arguments if any Whereas it is declared as ~
){Constructor’s Body }. ClassName( no arguments ){ };.
3. Constructor can either accept an arguments While it can’t have any arguments.
or not.
4. A constructor is called when an instance or It is called while object of the class is
object of a class is created. freed or deleted.
5. Constructor is used to allocate the memory While it is used to deallocate the
to an instance or object. memory of an object of a class.
6. Constructor can be overloaded. While it can’t be overloaded.
7. The constructor’s name is same as the class Here, it’s name is also same as the
name. class name preceded by tiled (~)
operator.
8. In a class, there can be multiple constructors. While in a class, there is always a
single destructor.
9. There is a concept of copy constructor which While here, there is no copy
is used to initialize an object from another constructor concept.
object.

You might also like