Constructors and Destructors · 2/24/2017 · Constructors and Destructors OOC 4th Sem, ‘B’...
Transcript of Constructors and Destructors · 2/24/2017 · Constructors and Destructors OOC 4th Sem, ‘B’...
![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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/1.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/2.jpg)
![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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/3.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/4.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/5.jpg)
![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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/6.jpg)
![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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/7.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/8.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/9.jpg)
➢ 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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/10.jpg)
![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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/11.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/12.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/13.jpg)
• 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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/14.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/15.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/16.jpg)
➢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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/17.jpg)
➢ 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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/18.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5edae51409ac2c67fa687b63/html5/thumbnails/19.jpg)