Lecture 4-Week4-Parameter and Copy Constructor
Lecture 4-Week4-Parameter and Copy Constructor
Programming
(SE1143)
Week 4
Parameter Constructor
Copy Constructors
Instance Members
Class Members
2
Constructors
3
Parameter Constructor
4
Copy Constructor
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
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
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
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
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
28
Explicit Use of this Pointer
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
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
36
Declaring Static Member Functions
37
Defining Static Member Functions
38
Calling Static Member Functions
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