You are on page 1of 50

1

Chapter 4:
CONSTRUCTORS,
DESTRUCTORS, FRIENDS
(part 1)
DCS5088 :: Chapter 4 (part 1) 2
Objectives
At the end of this lecture, students should
be able to :
Explain what are constructors and destructors
Implement the various types of constructors
default, parameterized, overloaded, initialization
list, copy constructors
Implement destructors
DCS5088 :: Chapter 4 (part 1) 3
4.1 Constructor
Definition
A constructor is a member (method) that is called
automatically each time when an object is created.
The constructor allocates sufficient memory space
for the object.
C++ will automatically supply a constructor if it is not
supplied in the program.
DCS5088 :: Chapter 4 (part 1) 4
4.1.1 Characteristics of a Constructor
1. Constructor has the same name as the class.
class Staff
{
public:
Staff ( ); // constructor declaration

};
DCS5088 :: Chapter 4 (part 1) 5
4.1.1 Characteristics of a Constructor (cont)
2. Constructors purpose is to allocate memory
for object creation.
3. It is called automatically when objects are
created, this means we dont have to
invoke it like functions.
4. A constructor does not return a value.
5. An argument can be sent to the constructor.

DCS5088 :: Chapter 4 (part 1) 6
Example (constructor)
#include<iostream>
using namespace std;
class Numbers
{ private: int n1, n2;

public:

Numbers()
{ cout<<"This is the constructor"<<endl;
n1 = n2 = 0;
}

} ;

Constructor
Continue.
DCS5088 :: Chapter 4 (part 1) 7
Example (constructor)- (cont)
void main()
{ Numbers A, B


}
Numbers()
{ cout<<"This is the constructor"<<endl;
n1 = n2 = 0;
}
First call by First
object (A)

Second call by
Second object (B)

DCS5088 :: Chapter 4 (part 1) 8
Initialization of Objects values
n1

n2

0
0
n1

n2

0
0
A
B
DCS5088 :: Chapter 4 (part 1) 9
Output:
Numbers A, B
Call constructor twice
in the order of object
creation.

This is the constructor
This is the constructor
Output:
DCS5088 :: Chapter 4 (part 1) 10
4.2 Default constructor
A constructor with empty arguments.
Whenever a new object is created, the
default constructor is invoked.
The previous example uses this.
DCS5088 :: Chapter 4 (part 1) 11
4.3 Parameterized Constructor
A constructor can have many
parameters.
If the constructor receives a parameter
when the object is created, the
parameter must be included with the
declaration of the object.
DCS5088 :: Chapter 4 (part 1) 12
Example (Parameterized Constructor)
#include<iostream>
using namespace std;
class Numbers
{ private: int n1, n2;

public:

Numbers(int a, int b)
{ n1=a; n2=b;
cout<<"This is the parameterized constructor"<<endl;
}

} ;
Continue.
DCS5088 :: Chapter 4 (part 1) 13
Example (Parameterized Constructor) (cont)
void main()
{ Numbers Ob(10, 18);

}
Numbers(int a, int b)
{ n1=a; n2=b;
cout<<"This is the parameterized constructor"<<endl;
}
10 is copied into a,
18 is copied into b
DCS5088 :: Chapter 4 (part 1) 14
Initialization of Objects values
n1

n2

10
18
Ob
DCS5088 :: Chapter 4 (part 1) 15
Output:
Numbers Ob(10, 18);

Call parameterized
constructor

This is the parameterized constructor

Output:
DCS5088 :: Chapter 4 (part 1) 16
4.4 Constructors with initialization list
Applies to default constructors and also
parameterized constructor
The value is entered after an argument list for
function, starting with single point symbol ( : ).

DCS5088 :: Chapter 4 (part 1) 17
Example (Parameterized Constructor with Initialization
list)
#include<iostream>
using namespace std;
class Numbers
{ private: int n1, n2;

public:

Numbers(int a, int b) : n1(a), n2(b)
{ cout<<"This is the parameterized constructor"<<endl;

}
} ;
Similar to earlier
parameterized constructor

DCS5088 :: Chapter 4 (part 1) 18
Exercise 1
Given a class below, create a parameterized
constructor with initialization list

class DoubleNum
{ private: double a, b;



};
DCS5088 :: Chapter 4 (part 1) 19
4.5 Overloaded Constructors
Constructors can also be overloaded.
You only have to list down all the
different version of constructor needed to
be used in the class.
The main purpose of overloading a
constructor is to provide options or give
the object initial value or not.
DCS5088 :: Chapter 4 (part 1) 20
Example (Overloaded Constructors)
#include<iostream>
using namespace std;
class Numbers
{ private: int n1, n2;

public:
Numbers(int a, int b) : n1(a), n2(b)
{ cout<<"This is the parameterized constructor"<<endl;

}

Numbers( ) : n1(0), n2(0)
{cout<<"This is the default constructor"<<endl; }
} ;

Continue.
DCS5088 :: Chapter 4 (part 1) 21
Example (Overloaded Constructors) (cont)
void main()
{ Numbers Ab, Cd(3,5);

}
Default constructor will
be called first
Parameterized constructor
will be called next
DCS5088 :: Chapter 4 (part 1) 22
Initialization of Objects values
n1

n2

0
0
n1

n2

3
5
Ab
Cd
DCS5088 :: Chapter 4 (part 1) 23
Output:
Numbers Ab, Cd(3,5);

Default constructor
called first followed
by parameterized
constructor

This is the default constructor
This is the parameterized constructor
Output:
DCS5088 :: Chapter 4 (part 1) 24
4.6 Copy Constructors
Copy constructors are a specialized form of a
constructor used to construct a copy of an object from
an object of the same type.
A copy constructor is supposed to construct a new
object which is initialized to a copy of an existent
object.
The copied constructor takes a reference to a const
parameter.
It is const to guarantee that the copy constructor
doesn't change it.
DCS5088 :: Chapter 4 (part 1) 25
4.6 Copy Constructors (cont..)
If you don't provide your own copy
constructor, C++ generates one for you.
The copy constructor provided by C++
performs a member-by-member copy of
each data member.
DCS5088 :: Chapter 4 (part 1)
26
#include<iostream>
using namespace std;
class base
{ private:
int val;
public:
base(int x=0)
{
val=x;
cout<<endl<<"Constructor; value:"<<val;
}

~base( )
{ cout<<endl<<"Destructor; value:"<<val; }

base(const base &rc)
{ cout<<endl<<"Copy constructor"; val=rc.val + 10; }
};

Continue.
Copy Constructor
DCS5088 :: Chapter 4 (part 1)
27
int main( )
{
base b1;
//creates a new object newobj1 based on an existing class b1
base newobj1 = b1; //calls copy constructor

//another way to create a new object newobj2 based on existing
//object b1
base newobj2 (b1); //calls copy constructor
return 0;
}

DCS5088 :: Chapter 4 (part 1) 28
Output
DCS5088 :: Chapter 4 (part 1) 29
Exercise 2
Create a class called Box
Declare a private data member: int length
Define a mutator and accessor function
Create overloaded constructors (default and
parameterized). In the default constructor,
initialize the length with 10 using initialization
list
Create a copy constructor
DCS5088 :: Chapter 4 (part 1) 30
Exercise 3
Given a class (in the next slide) and
sample output (in the next two slides), in
the main() :
Declare an array of 3 objects
Set the objects to values 20.0, 30.0, 40.0 by
using the appropriate function.
Display the values of all the objects using
the appropriate function
DCS5088 :: Chapter 4 (part 1)
31
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class Marks
{
private:
double point;
public:
Marks()
{ point = 0.00;}

Marks(double p)
{ point = p;}

void setPoint(double p)
{ point = p; }

double getPoint()
{ return point; }
};
DCS5088 :: Chapter 4 (part 1) 32
Sample Output
DCS5088 :: Chapter 4 (part 1)
33
//Answer:
int main( )
{
Marks m1[3] ;
double val=20;

for(int i=0; i<3; i++)
{ m1[i].setPoint(val+(i*10));
cout<<fixed<<showpoint<<setprecision(2)<<
m1[i].getPoint()<<endl;

}
return 0;
}
DCS5088 :: Chapter 4 (part 1) 34
Exercise 4
Using the class at Exercise 3, identify the output if you
have the following:

int main()
{ Marks m, n1[3] = { 10.5, 20.5, 59.1 };
cout<<fixed<<showpoint<<setprecision(2);
cout<<m.getPoint()<<endl;
cout<<n1[2].getPoint()*2<<endl;
return 0;

}
DCS5088 :: Chapter 4 (part 1) 35
4.7 Destructors
Definition
The destructor is a function that is called
automatically when an object is destroyed or
goes out of scope.
The destructor reclaims the memory that is
allocated to the object.
DCS5088 :: Chapter 4 (part 1) 36
4.7.1 Characteristics of destructor
A destructor has the same name as the class,
but it has a tilde (~ ) in front of it as shown in
the following example.
Does not return a value and therefore it does not
have a type specifier.
Only one destructor in a class allowed.

DCS5088 :: Chapter 4 (part 1) 37
Example (Destructor)
#include<iostream>
using namespace std;
class Numbers
{ private: int n1, n2;

public:
Numbers(int a, int b) : n1(a), n2(b)
{ cout<<"This is the parameterized constructor"<<endl;

}

Numbers() : n1(0), n2(0)
{cout<<"This is the default constructor"<<endl; }


Continue.
DCS5088 :: Chapter 4 (part 1) 38
Example (Destructor) (cont)
~Numbers()
{ cout<<"Object Destructed"<<endl;
cout<<n1<<" "<<n2<<endl;
}
} ; //end of class


Destructor
Continue.
DCS5088 :: Chapter 4 (part 1) 39
Example (Destructor) (cont)
void main()
{ Numbers Ab, Cd(3,5);

}
Recall:
1) Ab calls default constructor
2) Cd calls parameterized constructor

DCS5088 :: Chapter 4 (part 1) 40
Initialization of Objects values
n1

n2

0
0
n1

n2

3
5
Ab
Cd
DCS5088 :: Chapter 4 (part 1) 41
Recall Output:
Numbers Ab, Cd(3,5);

Default constructor
called first. Next,
parameterized
constructor

This is the default constructor
This is the parameterized constructor
Output:
DCS5088 :: Chapter 4 (part 1) 42
Example (Destructor) (cont)
void main()
{ Numbers Ab, Cd(3,5);

}
At this point object goes out of scope,
therefore they are deallocated by destructor
Destructors are called twice (2 objects) in
reverse order of object creation.
DCS5088 :: Chapter 4 (part 1) 43
Order of Destruction
n1

n2

0
0
n1

n2

3
5
Ab
Cd
Second,
Destructor
called for Ab
First,
Destructor
called for Cd
DCS5088 :: Chapter 4 (part 1) 44
Extra Output due to Destructor
This is the default constructor
This is the parameterized constructor
Object Destructed
3 5

Output:
~Numbers()
{ cout<<"Object Destructed"<<endl;
cout<<n1<<" "<<n2<<endl;
}
Cd destructed
DCS5088 :: Chapter 4 (part 1) 45
Extra Output due to Destructor
This is the default constructor
This is the parameterized constructor
Object Destructed
3 5
Object Destructed
0 0


Output:
~Numbers()
{ cout<<"Object Destructed"<<endl;
cout<<n1<<" "<<n2<<endl;
}
Ab destructed
DCS5088 :: Chapter 4 (part 1) 46
Exercise 5
What is the output of the following
program (slide 47, 48, 49)?
DCS5088 :: Chapter 4 (part 1)
47
#include<iostream>
using namespace std;
class base
{ private:
int val;
public:
base(int x=0)
{
val=x;
cout<<endl<<"Constructor; value:"<<val;
}

~base( )
{ cout<<endl<<"Destructor; value:"<<val; }

base(const base &rc)
{ cout<<endl<<"Copy constructor";
val = rc.val + 10;
}

Continue.
DCS5088 :: Chapter 4 (part 1)
48
void setVal(int n)
{ val = n; }

void display( )
{ cout<<\n val = <<val<<endl; }

}; //end of class

void abc(base obj)
{ cout<<"obj in function abc ::";
obj.display();
obj.setVal(10);
cout<<"modified obj in function abc ::";
obj.display();
}


Note: You should realize that
abc()is NOT a member
function.

The obj is a new object of class
base. When this function is
called, obj will have a copy of
object from the calling function..
When this function ends, the memory for obj
will be deallocated. Thus, destructor will be
called once due to obj goes out of scope
DCS5088 :: Chapter 4 (part 1)
49
int main( )
{ base b1,b2(105);

abc(b2);
cout<<"b2 :";
b2.display();

cout<<"b1 :";
b1.display();

}
DCS5088 :: Chapter 4 (part 1) 50
Answer (output)

You might also like