Professional Documents
Culture Documents
Chapter 3
Constructors & Destructors
-by-
Prof. Bhandare P. S.
SVERI’s COE(Poly), Pandharpur
1
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Syllabus:
3.1 Concepts of Constructors, Types of constructors: Default,
Parameterized, Copy.
3.3 Destructors.
Assignment No 3.
List four types of constructors. (2M)
Write example code of constructor with default argument (example code
of constructor, only, not whole program). (2M)
Explain concept of overloaded constructor in a class with example. (4M)
Differentiate between constructor and destructor. (4M)
What is overloaded constructor in a class? Explain with example. (4M)
State any four characteristics of constructor. (4M)
Explain the concept of parameterized constructor with example. (4M)
Explain copy constructor with example. (4M)
Write any four rules to define constructor in a class. (4M)
What is destructor ? Give it’s syntax. How many destructors can be
defined in a single class ? (4M)
Declare a class simple interest having data members as principle amount,
rate of interest, no. of years. The constructor will have default value of
interest as 11.5%. Accept this data for two objects. Calculate and display
simple interest for each object.
Write a program to declare a class time having data member as hrs, min,
sec. Write a constructor to accept data and use display function for two
objects.
Define a class distance having data members as feet and inches. Write
default as well as parameterized constructor and accept data for 2 objects.
Display the accepted data.
2
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
The title of chapter Constructor & destructor gives idea about the new
concept defined in C++.
Central Idea behind including this chapter in subject is that the student
will importance of constructor & destructor for the classes in OOP.
Importance of chapter :
To study destructors.
3
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Introduction
We have seen, examples of classes being implemented.
In all the cases, we have used member functions such as putdata() and
setvalue() to provide initial values to the private member variables.
◦ E.g.
◦ A.input();
invokes the member function input, which assigns the initial values to the
data items of object A.
◦ x.getdata(100,299.95);
All these 'function call' statements are used with the appropriate objects
that have already been created.
Constructor
A constructor is a 'special' member function whose task is to initialize the
objects of its class.
4
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
◦ E.g.
not only creates the object Intl of type integer but also initializes its data
members m and n to zero.
◦ A a;
They do not have return types, not even void and therefore, and they
cannot return values.
They cannot be inherited, though a derived class can call the base class
construction.
They make 'implicit calls' to the operators new and delete when memory
allocation is required.
6
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Parameterized Constructors
The constructor integer(), defined above, initializes the data members of
all the objects to zero.
class integer
{
int m, n;
public:
integer(int x, int y); // parameterized constructor
………….
………….
};
integer :: integer(int x, int y)
{
m=x ;
n= y;
}
7
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
8
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
◦ class A
◦ {
◦ public:
◦ A(A);
◦ }
◦ is illegal.
◦ {
◦ public:
◦ A (A & ) ;
◦ };
As pointed out earlier, the missing arguments must be the trailing ones.
The default argument constructor can be called with either one argument or no
arguments.
9
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Copy Constructor
A copy constructor is used to declare and initialize an object from another
object.
For example, the statement
Integer i2(i1);
would define the object i2 and at the same time initialize it to the values of i1.
Another form of this statement is integer
i2 = i1;
The process of initializing through a copy constructor is known as copy
initialization.
A copy constructor takes a reference to an object of the same class as itself as
an argument
Destructors
A destructor, as the name implies, 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 the same
as the class name but is preceded by a tilde.
For example, the destructor for the class integer can be defined as shown
below:
~ integer(){ }
A destructor never takes any argument nor does it return any value.
It will be invoked implicitly by the compiler upon exit from the program for
block or function as the case may be to clean up storage that is no longer
accessible.
It is a good practice to declare destructors in a program since it releases
memory space for future use.
Whenever new is used to allocate memory in the constructors, we should use
delete to free that memory.
10
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Winter-15
List four types of constructors. (2M)
Write example code of constructor with default argument (example code of
constructor, only, not whole program). (2M)
Explain concept of overloaded constructor in a class with example.(4M)
Differentiate between constructor and destructor. (4M)
State any four characteristics of constructor. (4M)
Summer-15
Define constructor. State any two type of constructor. 2m
Define constructor overloading. 2m
Explain the concept of destructor in a class with example. (4M)
Explain any two types of constructor with syntax and example. (4M)
Explain the concept of parameterized constructor with example. (4M)
Explain the concept of parameterized constructor with example. (4M)
Explain copy constructor with example. (4M)
Winter-14
What is copy constructor ? 2M
Write any four rules to define constructor in a class. 4M
What is destructor ? Give it’s syntax. How many destructors can be defined in a
single class ? 4M
Explain constructor with default argument. 4M
Explain overloaded constructor in a class with suitable example. 4M
Summer-14
State the characteristics of destructor. 2M
What is parameterized constructor? Give the syntax & example of it. 4M
Illustrate the concept of constructor with default argument with suitable
example. 4M
State any four characteristics of constructor. 4M
Explain the concept of overloaded constructors in a class with suitable example.
4M
11
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Winter-13
What is a parameterized constructor used in C++ ? 2M
Describe the importance of destructors. 2M
What is default constructor ? 2M
What is meant by constructor overloading ? Explain the concept with proper
example. 4M
12
Subject: Object oriented programming (17432)