After
discussing the c++ class and objects in detail now we will move toward the
design principles of object oriented programming.
Design Principles of Object Oriented Programming:
There are 5 design principle of object
oriented programming as
(1) Information hiding
(2) Encapsulation
(3) Abstraction
(4) Inheritance
(5) Polymorphism
These 5 concepts are the back bone of object
oriented programming and we can say its foundation is based on these
principles.
I know it is
very difficult for the newbies to grasp these concepts so I will try to explain
these principles in details and by giving relevant examples from the real life.
Let’s first
explain the information hiding.
Information Hiding
Information
hiding is the technique in which we try to hide the attributes of the object
from outside world.
According to
this technique information in whatever form is stored inside the object and it
is hidden from the outside world and only object itself manipulate its state or
data and we cannot directly access them. We need an interface to access them.
Information
hiding is achieved by using access specifies a topic we will study in details
in upcoming tutorials.
Examples of Information Hiding
Information Hiding has plenty of examples
related to the real life.
Suppose myself jawad tariq is an object. Human
is a very complicated object because it has every requirement which is needed for an object. Now I use information hiding technique as you
the outer world do not know about my personal life. You do not know from where
i belong, you do not know my marital status and you do not know either I have
kids or not. This is because I have made all these information hidden. This
information is saved inside my brain at some part of it and you the outer world
have no direct access to it. I have not put a label on my forehead which tell
you what is my age or what is marital status etc. You can only access to my
private data using the interface I have provided you .Consider that I have provided you an interface to find my age
"What is your age" you can
access to my private data indirectly by using that interface. So object
has made all its information hidden so other objects cannot directly manipulate
their states.
Information Hiding Advantages
(1) Information hiding detail simplifies
the modal by hiding complex details and implementation from you.
(2) Information hiding technique will not
allow the user to manipulate states of object directly hence this technique
avoids the object to go into the invalid state.
Let me
elaborate my second point with some examples.
Consider a class student for sake of simplicity considers it has only
one attribute age. Now what will happen if user enters a
negative value for age? User may know
that age is always in positive integer but he may accidently or unintentionally
press negative key. So this will leads the object in to invalid state. Here
information hiding is useful as it do not give direct access to the user to
manipulate the data. User can only manipulate the data using an interface or a
member function. Now you may have a question that how member function will act
to reduce chances of object to go into invalid state. I will explain this when
we will go further into object oriented programming. At that moment we are
only discussing the concepts of object oriented programming which I think are
very necessary for you to feel comfortable with object oriented programming in
c++.
0 comments:
Post a Comment