Lec 05 (Constructors Destructors)
Lec 05 (Constructors Destructors)
Course Code:CS1022
Lecture#5
Today’s Lecture
• Constructor – example program
• Constructor with arguments – example
program
• Constructor Overloading
• Destructor – example program
Example program – Distance class
• Data members
– Feet
– Inches
• Member functions
– void setdist(int ft, float in);
– void getdist();
– void initialize();
– void showdist();
Distance class – data member
initialization
• Distance class shows two ways to initialize the
data items in an object
– void initialize();
– void setdist(int ft, float in);
• Can an object be initialized whenever it is created,
without requiring a separate call to a member
function?
• Automatic initialization is carried out using a
special member function called a constructor.
• A constructor is a member function that is
executed automatically whenever an object is
created.
Constructors
• C++ requires a construct call for each
object it has created
• This ensure that object is initialized
properly before it is used
• If there is no constructor, the compiler
provides a default constructor that is, a
constructor with no parameters
• Name of constructor function is same as
name of class
A counter example
• Data member
– Count
• Member function
– Constructor
– void inc_count()
– int get_count()
Automatic initialization
• An object of type Counter is first created,
we want its count to be initialized to 0
• Options are
– set_count() function (call it with an argument
of 0)
– zero_count() function, to set count to 0.
• Such functions would need to be executed
every time we created a Counter object
Write a class with num and ch as data
member. A constructor with no parameter
initializes num to 0 and ch to ‘x’. A
constructor with two parameters initializes
data members with the given values and
member function ‘show’ displays the values
of data members.
Cont.
• A programmer may forget to initialize the
object after creating it
• It’s more reliable and convenient to cause
each object to initialize implicitly when it is
created
• In the Counter class, the constructor
Counter() is called automatically whenever a
new object of type Counter is created
• Counter c1, c2;
creates two objects. Constructor is called with
each object separately
Constructor Name
• First, constructor name must be same as the
name of class
– This is one way the compiler knows they are
constructors
• Second, no return type is used for
constructors
– Why not? Since the constructor is called
automatically by the system, there’s no program
for it to return anything to; a return value wouldn’t
make sense
– This is the second way the compiler knows they
are constructors
Initializer List
• One of the most common tasks a constructor
carries out is initializing data members
• In the Counter class the constructor must initialize
the count member to 0
• The initialization takes place following the member
function declarator but before the function body.
• Initialization in constructor’s function body
Counter()
{ count = 0; }
this is not the preferred approach
Cont.
• It’s preceded by a colon. The value is
placed in parentheses following the
member data.
Counter() : count(0)
{ }
• If multiple members must be initialized,
they’re separated by commas.
– someClass() : m1(7), m2(33), m3(4)
←initializer list { }
Constructor with default
arguments
Constructor with default arguments
Overloaded Constructors
• It’s convenient to be able to give variables of
type Distance a value when they are first
created
Distance dist2(11, 6.25);
• which defines an object, and initializes it to a
value of 11 for feet and 6.25 for inches.
• Distance dist1, dist2; then No-argument
constructor is called/invoked (the default
constructor)
• Since there are now two constructors with the
same name, Distance(), we say the
constructor is overloaded
Example
// C++ program to demonstrate int getAge() {
constructor overloading return age;
#include <iostream> }
using namespace std; };
Person1 Age = 20
Person2 Age = 45
Copy Constructor
When is it used?
• Programmer can use copy constructor explicitly to create
an object that is a copy of an existing object
• Compiler generates a call to copy constructor, when
object is passed as a value parameter
• By default, the compiler generates a copy constructor for
each class
– This default copy constructor makes a copy of an object
member by member
• If a class has dynamic data members this copy
constructor generated by the compiler is not adequate as
we’ll see in later chapters
– Then the programmer needs to write a proper copy constructor
18
The Default Copy Constructor
void main()
{
Distance dist1(10,5.5);
Distance dist2(dist1);//causes the default copy
constructor to perform a member-by-member
copy of dist1 into dist2
Distance dist3 = dist1;
}
19
Destructors
• Destructor is a function called implicitly when
an object is destroyed
• The name of the destructor for a class is the
tilde character (~) followed by the class
name
• No arguments and no return type for a
destructor
• The most common use of destructors is to
deallocate memory that was allocated for the
object by the constructor