constructors and destructors · 2/24/2017  · constructors and destructors ooc 4th sem, ‘b’...

19
Constructors and Destructors OOC 4 th Sem, ‘B’ Div 2016-17 Prof. Mouna M. Naravani

Upload: others

Post on 31-May-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor

Constructors and Destructors

OOC

4th Sem, ‘B’ Div

2016-17

Prof. Mouna M. Naravani

Page 2: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor
Page 3: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor

A constructor guarantees that an object created by the class will be initialized automatically.

Page 4: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor

Ex: create an object

integer int1;

➢Object int1 is created, and also initializes its data members m and n to zero.

➢There is no need to write any statement to invoke the constructor function.

➢ If a normal member function is defined for zero initialization, we would need to invoke

this function for each of the objects separately.

➢This would be very inconvenient, if there are a large number of objects.

Page 5: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor
Page 6: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor
Page 7: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor

Default Constructor

➢Accepts no parameters.

➢Also called as Zero Constructor.

➢ If no such constructor is defined, then the compiler supplies a default constructor.

Page 8: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor

Parameterized Constructor

➢A constructor that takes arguments or parameters are called Parameterized constructors.

➢We can pass the arguments to constructor function when object are created.

Page 9: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor

➢ We must pass the initial values as arguments to the constructor function when an object

is declared.

➢ This can be done in two ways:

By calling the constructor explicitly.

By calling the constructor implicitly.

By calling the constructor explicitly

integer int1 = integer(10, 100);

➢ This statement creates an integer object int1 and passes the values 10 and 100 to it.

By calling the constructor implicitly

integer int1(10, 100);

➢ Also called as short hand method, shorter, better and easy to implement.

Page 10: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor
Page 11: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor

Copy Constructor

➢Copy Constructor is used to declare and initialize an object from another object.

➢Ex:

integer I2 ( I1 );

➢This would define an object I2 and at the same time initialize it to the values of I1.

➢Another form:

integer I2 = I1;//it simply assigns the values of I1 to I2, member-by-memeber

➢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.

Page 12: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor

Multiple Constructors in a Class

class integer

{

int m, n;

public:

integer()

{

m=0; n=0;

}

integer(int a, int b)

{

m=a; n=b;

}

integer(integer & i)

{

m = i.m; n=i.n;

};

}

//Default Constructor

//Parameterized Constructor

//Copy Constructor

1. Object declarations:

integer I1;

- invokes default constructor and set both

m and n of I1 to 0.

2. Object declarations:

integer I2(20, 40);

- invokes parameterized constructor and

set both m and n of I2 to 20 and 40

respectively.

3. Object declarations:

integer I3(I2);

- invokes copy constructor which copies

the values of I2 into I3.

Page 13: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor

• The actual parameter, when specified, overrides the default value.

Page 14: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor

Default Constructor

A :: A()

Default Argument Constructor

A :: A(int = 0)

➢ The default argument constructor can be called with either one argument or no

arguments.

➢ When called with no arguments, it becomes a default constructor.

➢ When both these forms are used in a class, it causes ambiguity for a statement such

as:

A a;

The ambiguity is whether to call A::A() or A::A(int = 0)

Page 15: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor

Destructors

➢Used to destroy the objects that have been created by a constructor.

➢The destructor is a member function whose name is the same as the class name but is

preceded by a tilde( ~).

➢Destructor of class integer can be defined as;

~integer() { }

➢ Destructor never takes any argument nor does it return any value.

➢ It will be invoked implicitly by the compiler upon exit from the program to clean up

storage that is no longer accessible. (No need to call it explicitly)

➢Destructors releases memory space for future use.

➢Destructors destroy the objects in the reverse order of creation.

Page 16: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor

➢Whenever new is used to allocate memory in the constructors, we should use delete to

free that memory.

➢This is required because when the pointers to objects go out of scope, a destructor is not

called implicitly.

Page 17: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor

➢ Constructors.cpp

➢ Destructors.cpp

➢ DestructoroutofScope.cpp

➢ beforeDestructor.cpp

➢ CopyConstructors.cpp

➢ OverloadedConstructors.cpp

➢ ParameterizedConstructors.cpp

Page 18: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor

References

➢Sourav Sahay, “Objected Oriented Programming with C++”

➢E Balagurusamy, “Objected Oriented Programming with C++”

➢P. B. Kotur, “Objected Oriented Programming with C++”

Page 19: Constructors and Destructors · 2/24/2017  · Constructors and Destructors OOC 4th Sem, ‘B’ Div 2016-17 ... 3. Object declarations: integer I3(I2); - invokes copy constructor