Lecture 3-Week3 - Access Specifier, Constructor, Destructor
Lecture 3-Week3 - Access Specifier, Constructor, Destructor
(CS1143)
Week 3
2
Outline
3
Access Modifiers
5
6
Access Modifiers for Data Members
The modifiers for data members are normally set to private for
emphasis (although no modifier means private).
This means that the data members are not accessible directly, they
must be accessed through the member functions.
We can also set them to public or protected if we want.
7
Access Modifiers for Member Functions
8
Example Private Data Member (P1)
9
Example Public Data Member (P2)
10
Example Protected Data Member (P3)
11
Example Private Member Function (P4)
12
Group Modifier Access
We have used only one keyword, private, and one keyword, public, in
the whole class definition.
This is referred to as group modification.
A modifier is valid until we encounter a new one.
13
Outline
14
Constructor and Destructor
We should first create the object and initialize its data members.
Creation is done when a special member function named a
constructor is called
Initialization is when the body of a constructor is executed.
15
16
Constructor Declaration
17
Constructor Definition
18
Constructor Definition
19
Constructor Definition Contd..
20
Default Constructor
21
Outline
22
Destructor
23
Destructor Declaration
24
Destruction Definition
25
Creating and Destroying Objects
26
27