You are on page 1of 10

BOOP -4320702 Sem-2

Diploma in Computer Engineering (2nd Semester)

BASIC OBJECT ORIENTED ROGRAMMING (4320702)

Unit – 3

Constructor & Destructor

Computer Dept P a g e |1
BOOP -4320702 Term-212 Sem-2

3.1 Introduction to constructor and Destructor:


➢ Both constructors and destructors are the member function of the class.
➢ A constructor is a function that initializes the object of the class and allocates the
memory location for an object, the function has the name as the class name, known
for creating the object, called when the instance of the class created.
➢ Destructor also has the same name as the class name, denoted by tilted ~ symbol,
known for destroying the constructor, deallocates the memory location for created
by the constructor.
➢ One class can have more than one constructor but have one destructor.

3.2 Characteristics of constructor and destructor:


Characteristics of Constructor:
➢ They should be declared in the public section
➢ They do not have any return type, not even void
➢ They get automatically invoked when the objects are created
➢ They cannot be inherited though derived class can call the base class constructor
➢ Like other functions, they can have default arguments
➢ You cannot refer to their address
➢ Constructors cannot be virtual

Characteristics 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.
➢ An object of a class with a Destructor cannot become a member of the union.
➢ A destructor should be declared in the public section of the class.
➢ The programmer cannot access the address of destructor.

3.3 Define and initialize constructor:


➢ Constructor is a special member function which is invoked automatically when an
object is created to initialize the data members of that object.

Syntax:

KDP, PATAN Computer Dept P a g e |2


BOOP -4320702 Term-212 Sem-2
Example:

class Employee {
public:
int age;
//default constructor
Employee() {
//data member is defined with the help of default constructor
age = 50;
}};
int main() {
//object of class Employee declared
Employee e1;
//prints value assigned by default constructor
cout << e1.age;
return 0;
}

Types of Constructor
1. Default constructor
2. Parameterized constructor
3. Copy constructor

1. Default Constructor:
➢ The default constructor is the constructor which doesn't take any argument. It has
no parameter but a programmer can write some initialization statement there

➢ A default constructor is very important for initializing object members, that even if
we do not define a constructor explicitly, the compiler automatically provides a
default constructor implicitly.

#include<iostream>
using namespace std;
class item{
private:
int code;
int price;
public:
item ();
};

item :: item (){


code=1;
KDP, PATAN Computer Dept P a g e |3
BOOP -4320702 Term-212 Sem-2
price=100;
cout<<code<<endl;
cout<<price<<endl;
}
int main(){
item i1;
return 0;
}
Output: 1
100

3.4 Constructor with arguments (parameterized constructor):


➢ A default constructor does not have any parameter, but programmers can add and
use parameters within a constructor if required. This helps programmers to assign
initial values to an object at the time of creation.

#include<iostream>
using namespace std;
class item{
private:
int code;
int price;
public:
item (int, int);
};
item :: item (int p, int q){
code=p;
price=q;
cout<<code<<endl;
cout<<price<<endl;
}
int main(){
item i1=item(10,20); //explicit call
item i2 (1,2); //implicit call
return 0;
}
Output :
10
20
1
2

KDP, PATAN Computer Dept P a g e |4


BOOP -4320702 Term-212 Sem-2

3.5 Overloading constructor (multiple constructors) :


➢ Constructors can be overloaded in a similar way as function overloading.
➢ Overloaded constructors have the same name (name of the class) but the different
number of arguments. Depending upon the number and type of arguments passed,
the corresponding constructor is called.
#include<iostream>
using namespace std;
class item{
private:
int code;
int price;
float value;
public:
item();
item (int, int);
item(int,int,float);
};
item :: item(){
code=0;
price=0;
value=0;
cout<<"Default Constructor"<<endl;
cout<< code<<endl << price<<endl<< value<<endl;
}
item :: item (int p, int q){
code=p;
price=q;
cout<<"Two Argument Constructor"<<endl;
cout<< code<<endl << price<<endl;
}
item :: item (int p, int q, float r){
code=p;
price=q;
value=r;
cout<<"Three Argument Constructor"<<endl;
cout<< code<<endl << price<<endl<< value<<endl;
}
int main(){
item i1=item(10,20,11.10); //explicit call
item i2 (1,2); //implicit call
item i3;
KDP, PATAN Computer Dept P a g e |5
BOOP -4320702 Term-212 Sem-2
return 0;
}
Output:
Three Argument Constructor
10 20 11.1
Two Argument Constructor
12
Default Constructor
000

3.6 Array of object using constructors:


➢ Array of object is useful when we want to create more object of class.

#include<iostream>
using namespace std;

class item{
private:
int code;
int price;
public:
item ();
};

item :: item (){


code=1;
price=100;
cout<<code<<endl;
cout<<price<<endl;
}

int main(){
item i1[2];
return 0;
}
Output:
1 100
1 100

KDP, PATAN Computer Dept P a g e |6


BOOP -4320702 Term-212 Sem-2

3.7 Constructor with default arguments :


➢ Default arguments of the constructor are those which are provided in the constructor
declaration.
➢ If the values are not provided when calling the constructor the constructor uses the
default arguments automatically.

#include<iostream>
using namespace std;
class item{
private:
int code;
int price;
public:
item (int, int=22);
};
item :: item (int p, int q){
code=p;
price=q;
cout<<code<<endl;
cout<<price<<endl;
}
int main(){
item i1=item(10); //explicit call
item i2 (1,2); //implicit call
item i3 (11);
return 0;
}
Output:
10 22 1 2 11 22

3.8 copy constructor:


➢ These are special type of Constructors which takes an object as argument, and is used
to copy values of data members of one object into other object.
➢ A copy constructor is invoked when an existing object is passed as a parameter.
#include<iostream>
using namespace std;
class item{
private:
int code;
int price;
KDP, PATAN Computer Dept P a g e |7
BOOP -4320702 Term-212 Sem-2
public:
item ();
void show();
};
item :: item (){
code=1;
price=100;
}
void item :: show()
{ cout<<code<<endl;
cout<<price<<endl;
}
int main(){
item i1;
item i2=i1;
i2.show();
return 0;
}
Output:
1 100

3.9 Destructor:
➢ As the name implies, destructors are used to destroy the objects that have been created
by the constructor within the C++ program.
➢ Destructor names are same as the class name but they are preceded by a tilde (~).
➢ It is a good practice to declare the destructor after the end of using constructor.

#include<iostream>
using namespace std;
int c=0;
class test
{ public:
test(){
c++;
cout<<"\n Object Created"<<c<<endl;
}

KDP, PATAN Computer Dept P a g e |8


BOOP -4320702 Term-212 Sem-2
~test()
{
cout<<"\n Object Destroyed"<<c<<endl;
c--;
}
};
int main()
{
cout<<"\n In Main Block";
test a,b;
cout<<"\n In Block";
{
class test c;
}
cout<<"\n In Main Block Again";
return 0;
}
Output:
In Main Block
Object Created1
Object Created2
In Block
Object Created3
Object Destroyed3
In Main Block Again
Object Destroyed2
Object Destroyed1

KDP, PATAN Computer Dept P a g e |9


BOOP -4320702 Term-212 Sem-2

Be ready to do it in time!

Assignment-3

1. What is Constructor? Explain default constructor with example.

2. Discuss parameterized constructor with example.

3. Discuss overloading of constructor with example.

4. Discuss constructor with default arguments with example.

5. Explain copy constructor with example.

6. What is destructor? Explain with example.

7. Write characteristics of constructor and destructor.

KDP, PATAN Computer Dept P a g e | 10

You might also like