Lecture 6
Lecture 6
OBJECT ORIENTED
PROGRAMMING
MEHREEN TAHIR
Constructors
• At object declaration time, all data members
have garbage value
• Only public data members can be initialized at
object declaration time
• Constructor can initialize all data members
Types
• Default Constructor
– Also called no argument constructor
– A constructor with default parameter values
• Parameterized Constructor OR Overloaded
• Constructor
– Simple Parameterized Constructor
– Copy Constructor
• Initializer list Constructor
Initializer List Constructor
• Constructor initialize private data members
– Through Assignment
• For initialization using assignment the data member is
created first and then initialized
– Through Initializer list
• The initial value is used to initialize the data member
(as it is created)
Initializer List Constructor
• When a constructor is used to initialize other
members, these other members can be
initialized directly, without resorting to
statements in its body
• This is done by inserting, before the
constructor's body, a colon and a list of
initializations for class members
Default Copy Constructors
• Suppose we declare and initialize box object
firstBox with this statement
• Box B1(10, 15, 10);
• Now we want to create another box object ,
identical to the first.
• We want to initialize 2nd box object with first
• Box B2=B1
Default Copy Constructors
• The compiler generates a default version of
what is referred to as a copy constructor
• A copy constructor creates an object of a class
by initializing it with an existing object of the
same class.
• The default version of copy constructor
creates new object by copying existing object
member by member.
Default Copy Constructors
• You can use the copy constructor in this way
for any data types
• The default copy constructor is fine for simple
classes
• But for many classes that have pointer as
members may produce undesirable results
User Defined Copy Constructor
Destructors
• Every Class has a Destructor
• If you do not explicitly provide a destructor,
the compiler creates an “empty” destructor
• It works implicitly
Destructors
• To de allocate anything that was allocated
during the lifetime of an object
• If you have not allocated anything in the
lifetime of an object, there is no point in
having a destructor in your class
Destructors
• A class’s destructor is called implicitly when an
object is destroyed.
• An automatic object is destroyed when
program execution leaves the scope in which
that object was instantiated
– Scope refers to the life time of an object
– For example: The function that created an object
is finished, it will automatically call its destructor
Destructors
• A default destructor provided by the compiler
do nothing
• Only one destructor per function class
• No arguments no return values
• Public member function automatically called
when an object is destroyed
Destructors
• Has no return type
• Takes no arguments
• Only 1 destructor is allowed per class (i.e., it
cannot be overloaded)
• ERRORS in destructors??