0% found this document useful (0 votes)
6 views

Lecture 3-Week3 - Access Specifier, Constructor, Destructor

Uploaded by

yasoobvogue
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 3-Week3 - Access Specifier, Constructor, Destructor

Uploaded by

yasoobvogue
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Object Oriented

Programming
(SE1143)
Week 3

Department of Software Engineering


Capital University of Science and Technology (CUST)
Outline

 Use of access specifiers (access modifiers)


 Concept of Constructor and Object Instantiation
 Importance of Destructor

2
Outline

 Use of access specifiers (access modifiers)


 Concept of Constructor and Object Instantiation
 Importance of Destructor

3
Access Modifiers

 The access modifier determines how a class can be accessed.


 The designer of a class can apply an access modifier to the
declaration of a member (i.e. data member or member
function) to control access to that member.
 C++ uses three modifiers
 private
 protected
 public
 The declaration of data members and member functions in a
class is by default private.
 When there is no access modifier for a member, it is private
by default.
4
Access Modifiers Contd…

 When a member is private, it can only be accessed inside


the class.
 These members and member functions cannot be accessed directly
for retrieving or changing.
 They can be accessed only through member functions.
 When a member is public, it can be accessed from anywhere
(inside the same class, inside the subclasses, and in the
application).
 When a member is protected, it can be accessed inside the
same class and inside the subclass but cannot be accessed
from anywhere.
 We will study subclasses later.

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
 To operate on the data members, the application must use member
functions, which means that the declaration of member functions
usually must be set to public.
 Sometimes the modifier of a member function must be set to private,
such as when a member function must help other member functions
but is not allowed to be called by functions outside the class.

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

 Use of access specifiers (access modifiers)


 Concept of Constructor and Object Instantiation
 Importance of Destructor

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.

 When we do not need an object anymore, the object should be


cleaned up and the memory occupied by the object should be
recycled.
 Cleanup is automatically done when another special member
function named a destructor is called.

15
16
Constructor Declaration

 A constructor is a member function of the class, which means it must be


declared in the class definition.
 A constructor has no return value, its name is the same as the name of
the class, and it cannot have the const qualifier because the constructor
initializes the value of the data Members
 All constructors of the class are normally public, so the application can
call any of the constructors to initialize an object of the class.
 The following shows how we add the declaration of three constructors
to our Circle class.

17
Constructor Definition

 The main difference between the definition of a constructor and


definition of other member functions is that a constructor can have
an initialization list after the header to initialize the data members.
 The initialization list is put after the header and before the body of
the constructor and it starts with a colon.
 If we need to initialize more than one data member, the initialization
of each data member must be separated by a comma from other
data members
 We can think of each initialization as an assignment statement that
assigns the parameter to the data member,
dataMember = parameter

18
Constructor Definition

19
Constructor Definition Contd..

 Another important point is that a constant data member of an object


must be initialized when the object is created.
 C++ allows us to initialize it in the initialization section of a
constructor.
 The body of a constructor can also be used for additional processing,
such as validating a parameter, opening files if needed, or even
printing a message to verify that the constructor was called.

20
Default Constructor

 The default constructor is a constructor with no parameters.


 It is used to create objects with each data member for all objects set
to some literal values or default values.
 We must have at least one of Default or Parameter constructors in
our class.
 If we write neither, the system provides a default constructor,
referred to as a synthetic default constructor, that initializes each
member to what is left over as garbage in the system.

21
Outline

 Use of access specifiers (access modifiers)


 Concept of Constructor and Object Instantiation
 Importance of Destructor

22
Destructor

 Like a constructor, a destructor has two special characteristics.


 First, the name of the destructor is the name of the class preceded by
a tilde symbol (~)
 Second, like a constructor, a destructor cannot have a return value
(not even void) because it returns nothing.
 A destructor is guaranteed to be automatically called and executed
by the system when the object instantiated from the class goes out of
scope.
 For example if we have instantiated five objects from the class, the
destructor is automatically called five times to guarantee that all
objects are cleaned up.
 A destructor can take no arguments.

23
Destructor Declaration

24
Destruction Definition

 The definition of a destructor is similar to the definition of the other


three member functions,
 But it must have a tilde (~) in front of the first name.
 A destructor should be public like all constructors.

25
Creating and Destroying Objects

 Calling a constructor creates an object.


 When the constructor is executed, it initializes the data members.
 When a destructor is executed, the data members are cleaned up.
 A destructor is automatically called and executed by the system

26
27

You might also like