0% found this document useful (0 votes)
13 views41 pages

Lecture 4-Week4-Parameter and Copy Constructor

Uploaded by

yasoobvogue
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views41 pages

Lecture 4-Week4-Parameter and Copy Constructor

Uploaded by

yasoobvogue
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Object Oriented

Programming
(SE1143)
Week 4

Department of Software Engineering


Capital University of Science and Technology (CUST)
Outline

 Parameter Constructor
 Copy Constructors
 Instance Members
 Class Members

2
Constructors

 We discussed no parameter constructor (default) last week.


 Circle ();
 We can have two other types of constructors called
 Parameter Constructor
 Copy Constructor
 The following figure shows the declaration of a parameter
constructor and a copy constructor along with a default constructor

3
Parameter Constructor

 A parameter constructor initializes the data members of each


instance with specified values.
 The parameter constructor can be overloaded, which means that we
can have several parameter constructors each with a different
signature (different parameters).
 The advantage of the parameter constructor is that we can initialize
the data members of each object with a specific value.
 For example, if we use a parameter constructor, the radius of one
circle can be initialized to 3.1, another one to 4.6, and so on.

4
Copy Constructor

 Sometimes we want to initialize each data member of an object to


the same value as a corresponding data member of a previously
created object.
 We can use a copy constructor in this case.
 After calling the copy constructor, the source and the destination
objects have exactly the same value for each data member.
 The copy constructor has only one parameter that receives the
source object by reference.
 We cannot overload the copy constructor because the parameter list
is fixed and we cannot have an alternative form.

5
Definition of a Parameter and Copy
Constructor

6
Object Construction and Destruction

7
A Complete Program

8
9
10
11
12
Output

13
Description

 The application creates three objects, circle1, circle2, and circle3,


using a parameter constructor, a copy constructor, and a default
constructor.
 Note that the application does not call the destructor but the system
calls it when the object goes out of scope.
 The interesting point is that the objects are destroyed in the reverse
order in which they are constructed.
 The last created object is destroyed first; the first created object is
destroyed last.
 This is because the objects are created in stack memory. In a stack,
the last item inserted is the first item that can be removed.

14
Instance Members and
Class Members

15
Instance Members and Class
Members
 When we design a class, we can have two groups of
members:
 instance members
 Instance Data Members
 Instance Member Functions
 class members (static members)
 Class Data Members
 Class Member Functions

16
Instance Data
Members

17
Instance Data Members

 An instance data member defines the attributes of an


instance (object)
 These data members belong exclusively to the
corresponding instance and cannot be accessed by other
instances.
 Separate regions of memory are assigned for each object
and each region stores possibly different values for each
data member.
 Encapsulation

18
Access Modifier for Instance Data
Member
 It makes more sense for instance data members to be private.
 If we make the data members of an instance member public, they
can be directly accessed by the application without calling an
instance member function.
 In object-oriented programming we want the objects to apply their
behaviors on their attributes.
 We must make the instance data members private so that they can
be accessed only through instance member functions.

19
20
Instance Member
Functions

21
Instance Member Functions

 An instance member function defines one of the behaviors


that can be applied on the instance data members of an
object.
 There is only one copy of each instance member function in
memory and it must be shared by all instances.

22
23
Access Modifier for Instance
Member Functions
 The access modifier for an instance member function is normally
public and allows access from outside the class (the application)
 If the member function is supposed to be used only by other instance
member functions within the class, we can make it private.

24
Instance Member Function Selectors

 An application (for example, the main function) can call an instance


member function to operate on an instance.
 This call must be done through the instance.
 The application must first construct an instance and then let the
instance call the instance member function.
 The C++ language defines two operators, called member selector
operators, for this purpose.

25
this pointer

 If there is only one copy of a member function, how can that function
be used by one object at one time and by another object at another
time?
 C++ adds a pointer (a variable that holds the address of an object) to
each member function.
 The function code is applied to the data members of the object
pointed to by the this pointer.

26
27
Hidden Parameter

 How does an instance member function get a this pointer?


 It is added as a parameter to the instance member function by the
compiler as shown below:

28
Explicit Use of this Pointer

 We can use the “this” pointer in our program to refer to a data


member
 The this pointer cannot be used in the initialization list of a
constructor because at that point, the host object has not been
constructed; however, it can be used in the body of the constructor if
needed.

29
Class Data Member

30
Class Data Member or Static Data
Member
 A Class data member is a data member that belongs to the class.
 It is shared by all the instances of a class.
 We use the keyword “static” to declare a class data member

31
Example

 We want to keep track of the number of current instances in our


program.
 We can create a static data member, called count, which is initialized
to 0.
 All constructors increment this static member every time an instance
is created.
 The destructor decrements it when an object is destroyed (goes out
of scope).

32
Declaring Static Data Members

33
Initializing Static Data Members

 A static data member does not belong to any instance, which means
it cannot be initialized in a constructor.
 So it must be initialized after the class definition.

34
Class Member Function

35
Static (or class) Member Functions

 Since a static data member is normally private, we need a


public member function to access it.
 Although this can be done by an instance member function,
normally we use a static member function for this purpose.
 A static member function is not associated with any
instance.

36
Declaring Static Member Functions

 A static member function, like a static data member, belongs to the


class.
 It should be declared inside the class but must be qualified with the
keyword static.

37
Defining Static Member Functions

 There is no difference between the definition of a static member


function and an instance member function.
 We cannot use the const qualifier because there is no host object.

38
Calling Static Member Functions

 A static member function can be called either through an instance or


through the class
 To call a static member function through an instance, we use the
same syntax we use to call an instance member function
 To call a static member function through the class, we use the name
of the class and the class resolution operator (::).

39
Calling Static Member Functions
Contd..
 We cannot use a static member function to access an instance data
member because a static member function does not have the hidden
this pointer, which defines the instance that needs to be referenced.
 On the other hand, an instance member function can be used to
access static data members (the this pointer is not used), but we
usually avoid this.
 A good practice is to use instance member functions to access
instance data members and static member functions to access static
data members.

40
Example

41

You might also like