DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 1
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 2
Course Objectives
To describe programming, algorithm and its structure, sequences, loops and control flow structure.
To illustrate the structured and objective oriented programming, pointer declaration and initialization.
To demonstrate the applications of encapsulation and data hiding, specifies, constructors and inheritance.
To analyze various types of aggregation, composition, overriding, latch and C++
Course Topics / Major Contents
Introduction to object oriented design, history and advantages of object oriented design, introduction to object oriented
programming
concepts, classes, objects, data encapsulation, constructors, destructors, access modifiers, const vs non-const functions, static
data
members & functions, function overloading, operator overloading, identification of classes and their relationships, composition,
aggregation, inheritance, multiple inheritance, polymorphism, abstract classes and interfaces, generic programming concepts,
function & class templates, standard template library, object streams, data and object serialization using object streams,
exception
handling.
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY
Course Learning Outcomes (CLOs)
CLO’s CLO Description PLOs
PLO-1 (C2 -
CLO-1 Understand principles of object oriented paradigm.
Understand)
Identify the objects & their relationships to build object
CLO-2 PLO-3 (C3 - Apply)
oriented solution
Model a solution for a given problem using object oriented
CLO-3 PLO-3 (C3 - Apply)
principles
PLO-2 (C4 -
CLO-4 Examine an object oriented solution
Analyzing)
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY
RECOMMENDED BOOKS
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 5
Week twelve Topics
▶ Encapsulation
▶ Instance Variable Initializers
▶ Access Modifiers
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 6
Encapsulation
Encapsulation is one of the key features of object-oriented programming. It involves the bundling of data members and
functions inside a single class.
Bundling similar data members and functions inside a class together also helps in data hiding.
In C++, we can bundle data members and functions that operate together
inside a single class. For example,
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 7
Encapsulation
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 8
Encapsulation
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 9
Encapsulation
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 10
Encapsulation
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 11
Encapsulation
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 12
Encapsulation
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 13
Encapsulation
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 14
Encapsulation
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 15
Encapsulation
In C++, we can bundle data members and functions that operate together inside a single class. For
example,
In the above program, the function getArea() calculates the area of a rectangle. To calculate the area,
it needs length and breadth.
Hence, the data members (length and breadth) and the function getArea() are kept together in the
Rectangle class.
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 16
Encapsulation
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 17
Encapsulation
To calculate an area, we need two variables: length and breadth and a function: getArea(). Hence, we
bundled these variables and function inside a single class named Rectangle.
Here, the variables and functions can be accessed from other classes as well. Hence, this is not data
hiding.
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 18
Encapsulation
Data Hiding
Data hiding is a way of restricting the access of our data members by hiding the implementation
details. Encapsulation also provides a way for data hiding.
Example 2: C++ Data Hiding Using the private Specifier
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 19
Encapsulation
Example 2: C++ Data Hiding Using the private Specifier
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 20
Encapsulation
Example 2: C++ Data Hiding Using the private Specifier
Here, we have made the length and breadth variables private.
This means that these variables cannot be directly accessed
outside of the Rectangle class.
To access these private variables, we have used public
functions setLength(), getLength(), setBreadth(), and
getBreadth(). These are called getter and setter functions.
Making the variables private allowed us to restrict
unauthorized access from outside the class. This is data hiding
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 21
Encapsulation
Example 2: C++ Data Hiding Using the private Specifier
If we try to access the variables from the main() class, we will get an error.
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 22
Encapsulation
Reference Books
C++ How To Program 10th Edition Deitel & Deitel
Objet oriented Programming in C++ 3rd Edition By Robert Lafore
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 23
Thanks
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 24