6
CONSTUCTOR & DESTRUCTORS CONSTUCTOR & DESTRUCTORS OBJECT ORIENTED OBJECT ORIENTED PROGRAMMING PROGRAMMING

constructor and destructor-object oriented programming

Embed Size (px)

DESCRIPTION

definition of constructor and destructor, types of constructor, etc.

Citation preview

Page 1: constructor and destructor-object oriented programming

CONSTUCTOR & CONSTUCTOR & DESTRUCTORSDESTRUCTORS

OBJECT ORIENTED PROGRAMMINGOBJECT ORIENTED PROGRAMMING

Page 2: constructor and destructor-object oriented programming

INDEX

Concept of constructor, types of constructorConcept of constructor, types of constructor

Overloaded constructor

Destructor

1

2

3

Page 3: constructor and destructor-object oriented programming

CONCEPT OF CONSTRUCTOR

• A constructor is a special member function whose task is to initialize the objects of that class.

• It is special because its name is the same as the class name.

• Constructor is invoked whenever an object of the associated class is created.

• It is called constructor because it constructs the values of data members of the class.

Page 4: constructor and destructor-object oriented programming

TYPES OF CONSTRUCTOR

• Default constructor:A constructor that accepts no parameters is called the default constructor.

• Parameterized constructor:The constructor that can take arguments is called as parameterized constructor.

• Copy constructor:a constructor can accept a reference to its own class as a parameter which is called as copy constructor.

Page 5: constructor and destructor-object oriented programming

OVERLOADED CONSTRUCTOR

• When more than one constructor function is defined in a class, we say the constructor is overloaded.

Page 6: constructor and destructor-object oriented programming

DESTRUCTOR

• 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 also a special member function whose name is the same as the class name but is preceded by a tilde(~).

• Example: the destructor for class integer can be defined as shown below

~integer() {}