Page 139 - DCAP404 _Object Oriented Programming
P. 139
Object-oriented Programming
Notes Self Assessment
Fill in the blanks:
11. C++ permits us to achieve this objective by passing arguments to the constructor function
when the ………………….. are created.
12. A static constructor should be defined as a ………………….. member function.
13. The ……………………….. is called every time an object goes out of scope or when explicitly
deleted by the programmer (using operator delete).
14. The constructors that can take arguments are called …………………………. constructors.
15. A destructor is called for a class object when that object passes out of scope or is
………………….. deleted.
6.8 Summary
A constructor is a member function of a class, having the same name as its class and which
is called automatically each time an object of that class it created.
It is used for initializing the member variables with desired initial values. A variable
(including structure and array type) in C++ may be initialized with a value at the time of
its declaration.
The responsibility of initialization may be shifted, however, to the compiler by including
a member function called constructor.
A class constructor, if defined, is called whenever a program created an objects of that
class. Constructors are public member unless otherwise there is a good reason against.
A constructor may take argument (s). A constructor may taking no argument(s) is known
as default constructor.
A constructor may also have parameter (s) or argument (s), which can be provided at the
time of creating an object of that class.
C++ classes are derived data types and so they have constructor (s). Copy constructor is
called whenever an instance of same type is assigned to another instance of the same class.
If a constructor is called with less number of arguments than required an error occurs.
Every time an object is created its constructor is invoked.
The functions that is automatically called when an object is no more required is known as
destructor. It is also a member function very much like constructors but with an opposite
intent.
6.9 Keywords
Constructor: A member function having the same name as its class and that initializes class
objects with legal initial values.
Copy Constructor: A constructor that initializes an object with the data values of another object.
Default Constructor: A constructor that takes no arguments.
Destructor: A member function having the same name as its class but preceded by ~ sign and
that deinitializes an object before it goes out of scope.
132 LOVELY PROFESSIONAL UNIVERSITY