constructors and destructors · 2/24/2017 · constructors and destructors ooc 4th sem, ‘b’...
TRANSCRIPT
Constructors and Destructors
OOC
4th Sem, ‘B’ Div
2016-17
Prof. Mouna M. Naravani
A constructor guarantees that an object created by the class will be initialized automatically.
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.
Default Constructor
➢Accepts no parameters.
➢Also called as Zero Constructor.
➢ If no such constructor is defined, then the compiler supplies a default 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.
➢ 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.
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.
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.
• The actual parameter, when specified, overrides the default value.
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)
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.
➢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.
➢ Constructors.cpp
➢ Destructors.cpp
➢ DestructoroutofScope.cpp
➢ beforeDestructor.cpp
➢ CopyConstructors.cpp
➢ OverloadedConstructors.cpp
➢ ParameterizedConstructors.cpp
References
➢Sourav Sahay, “Objected Oriented Programming with C++”
➢E Balagurusamy, “Objected Oriented Programming with C++”
➢P. B. Kotur, “Objected Oriented Programming with C++”