Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Subject- Object Oriented Programming

Unit 1 – Fundamentals of OOP


Topic – Constructors and Destructors

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 1


CONSTRUCTOR
A member function with the same name as its class name is called Constructor.
It is used to initialize the object of that class with a legal initial value.
Example :
class student
{
private:
int rollno;
float marks;
public:
student( ) //Constructor
{
rollno=0; marks=0.0;
}
};

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 2


CONSTRUCTOR CALLING
A constructor can be called in two ways: implicitly and explicitly.

class student
class student
{
{
private:
private:
int rollno;
int rollno;
float marks;
float marks;
public:
public:
student( ) //Constructor
student( ) //Constructor
{ rollno=0; marks=0.0; }
{ rollno=0; marks=0.0; }
};
};
void main()
void main()
{
{
student s1 = student(); //explicit
student s1; //implicit
call
call
}
}
IMPLICIT CALL OF CONSTRUCTOR EXPLICIT CALL OF CONSTRUCTOR

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 3


FEATURES OF CONSTRUCTOR
 Constructor has the same name as its class.

 Constructor does not have any return type not even void.

 Constructor have to be defined only in the public section of the


class.

 Constructor is called automatically when the object of the class is


created.

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 4


TYPES OF CONSTRUCTOR
1. Default Constructor: A constructor that accepts no parameter is
called Default Constructor.

2. Parameterized Constructor: A constructor that accepts parameters


for its invocation is known as parameterized Constructor.

3. Copy Constructor: A copy constructor is a special constructor used to


create a new object as a copy of an existing object.

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 5


DEFAULT CONSTRUCTOR
 Default constructor takes no argument.
 It is used to initialize an object of a class with legal initial values.
Example :
class student
{
private:
int rollno;
float marks;
public:
student( ) // Default Constructor
{ rollno=0; marks=0.0; }
void display();
void enter();
};
void main()
{
student s1; //
default
constructor DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 6
PARAMETERISED CONSTRUCTOR
 Parametrized constructor
class stud
accepts arguments. {
 It initializes the data members of private: void display();
the object with the arguments int rollno; void enter();
passed to it. char name[20]; };
 When the object s1 is created, float marks;
public:
default constructor is called and void main()
stud( )
when the objects s2 is created, { rollno=0; marks=0.0; } {
parametrized constructor is stud s1;
called. stud(int roll, char nam[20],float stud s2(1, “RAM”, 350);
 When the object s3 is mar) stud s3=stud(2,”NEHA”,
{ rollno=roll; 400);
created, again the
strcpy(name, nam); }
parametrized constructor is
marks =mar;
called but this is explicit call of }
parametrized constructor.

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 7


COPY CONSTRUCTOR
Copy constructor accepts object as an argument.
It creates a new object and initializes its data members with data
members of the object passed to it as an argument.

Syntax : class_name( class_name & object_name)


{

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 8


COPY CONSTRUCTOR
EXAMPLE
class student student( student &s)
{
{
private:
int rollno; rollno= s.rollno;
char name[20]; strcpy(name, s.name);
float marks;
public: marks =s.marks;
student( ) }
{ };
rollno=0; marks=0.0; int main()
} {
student(int roll, char nam[20], float mar) student s1;
{ student s2( 1, “RAJESH”, 450);
rollno=roll; student s3(s2);
strcpy(name, nam); student s4(2, “MOHIT”,320);
marks =mar; student s5=s4;
} }

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 9


CONSTRUCTOR OVERLOADING
 Defining more than one constructors in a single class provided they all differ in
either type of arguments or number of argument is called constructor overloading.
 In previous example, in class student, there are three constructors:
1default constructor, 1 parametrized constructors and 1 copy constructor.

 We can define as many parametrized constructors as we want in a single class


provided they all differ in either number or type of arguments.

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 10


DESTRUCTOR
 A destructor is a member function whose Example :
name is same as the class name and is class student
preceded by tilde symbol(“~”). { private:
 It is automatically called by the compiler int rollno;
when an object goes out of scope. float marks;
 Destructors are usually used to deallocate public:
student( )
memory which was allocated to object
{ rollno=0; marks=0.0; }
when the object is destroyed.
~student()
 Note: In this program first the object s1 is
{ }
created. At that time constructor is called. };
When the program ends, s1 will also go void main()
out of scope. At that time destructor is {
called. student s1;
}

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 11


FEATURES OF DESTRUCTOR
It has the same name as that of class name and preceded by tilde
sign(~).
It is automatically called by the compiler when an object goes out of
scope.
Destructors are usually used to deallocate memory when the object
goes out of scope.
Destructor is always declared in the public section of the class.
We can have only one destructor in one class.

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 12


S. No. Constructor Destructor

1. Constructor helps to initialize the object of a class. Whereas destructor is used to destroy the instances.

It is declared as className( arguments if any )


2. Whereas it is declared as ~ className( no arguments ){ }.
{Constructor’s Body }.

3. Constructor can either accept arguments or not. While it can’t have any arguments.

A constructor is called when an instance or object


4. It is called while object of the class is freed or deleted.
of a class is created.

Constructor is used to allocate the memory to an While it is used to deallocate the memory of an object of a
5.
instance or object. class.

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

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

8. In a class, there can be multiple constructors. While in a class, there is always a single destructor.

There is a concept of copy constructor which is


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

10. They are often called in successive order. They are often called in reverse order of constructor.

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 13


Thank You..

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 14

You might also like