[go: up one dir, main page]

0% found this document useful (0 votes)
634 views21 pages

Abstraction & Encapsulation in C++

Here are the key points about encapsulation and abstraction in C++: - Encapsulation refers to binding together the data and functions that manipulate the data, and keeping these bound elements safe from outside interference and misuse. It prevents unauthorized access to an object's internal state. - Abstraction refers to exposing only essential aspects of the data or functionality to the user, hiding the implementation details. - Encapsulation is achieved through access specifiers like public, private, protected. Data members are declared as private and member functions as public to achieve encapsulation. - Abstraction can be implemented using abstract classes and interfaces which declare the essential methods but don't provide implementation. Concrete subclasses then implement the abstract methods.

Uploaded by

Deepam Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
634 views21 pages

Abstraction & Encapsulation in C++

Here are the key points about encapsulation and abstraction in C++: - Encapsulation refers to binding together the data and functions that manipulate the data, and keeping these bound elements safe from outside interference and misuse. It prevents unauthorized access to an object's internal state. - Abstraction refers to exposing only essential aspects of the data or functionality to the user, hiding the implementation details. - Encapsulation is achieved through access specifiers like public, private, protected. Data members are declared as private and member functions as public to achieve encapsulation. - Abstraction can be implemented using abstract classes and interfaces which declare the essential methods but don't provide implementation. Concrete subclasses then implement the abstract methods.

Uploaded by

Deepam Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Abstraction &

Encapsulation in C++
Abstraction
Data abstraction is one of the most essential and important feature of object oriented programming in C++.
Abstraction means displaying only essential information and hiding the details. Data abstraction refers to
providing only essential information about the data to the outside world, hiding the background details or
implementation.

Consider a real life example of a man driving a car. The man only knows that pressing the accelerators will
increase the speed of car or applying brakes will stop the car but he does not know about how on pressing
accelerator the speed is actually increasing, he does not know about the inner mechanism of the car or the
implementation of accelerator, brakes etc. in the car. This is what abstraction is.
Abstraction using Classes
We can implement Abstraction in C++ using classes. Class helps us to group data members and member
functions using available access specifiers. A Class can decide which data member will be visible to
outside world and which is not.
Abstraction in Header files
One more type of abstraction in C++ can be header files. For example, consider the pow() method present
in cmath header file. Whenever we need to calculate power of a number, we simply call the function pow()
present in the cmath header file and pass the numbers as arguments without knowing the underlying
algorithm according to which the function is actually calculating power of numbers.
Abstraction using Access Specifiers
 Members declared as public in a class, can be accessed from anywhere in the program.
 Members declared as private in a class, can be accessed only from within the class. They are not
allowed to be accessed from any part of code outside the class.

 We can easily implement abstraction using the above two features provided by access specifiers. Say,
the members that defines the internal implementation can be marked as private in a class. And the
important information needed to be given to the outside world can be marked as public. And these
public members can access the private members as they are inside the class.
Abstraction using Access Specifiers

You can see in the above program we


are not allowed to access the variables
a and b directly, however one can call
the function set() to set the values in a
and b and the function display() to
display the values of a and b.
Advantages of Data Abstraction
 Helps the user to avoid writing the low level code

 Avoids code duplication and increases reusability.

 Can change internal implementation of class independently without affecting the user.

 Helps to increase security of an application or program as only important details are provided to the

user.
Encapsulation
 In normal terms Encapsulation is defined as wrapping up of data and information under a single unit.
In Object Oriented Programming, Encapsulation is defined as binding together the data and the
functions that manipulates them.

 An example, in a company there are different sections like accounts, finance and sales section. The
finance section handles all the financial transactions and keep records of all the data related to
finance. Similarly the sales section handles all the sales related activities. Now there may arise a
situation when for some reason an official from finance section needs all the data about sales in a
particular month. In this case, he is not allowed to directly access the data of sales section. He will
first have to contact some other officer in the sales section and then request him to give the particular
data. This is what encapsulation is. Here the data of sales section and the employees that can
manipulate them are wrapped under a single name “sales section”.
Abstraction vs. Encapsulation
ABSTRACTION ENCAPSULATION
Abstraction is the process or method of gaining the While encapsulation is the process or method to
information. contain the information.
In abstraction, problems are solved at the design or While in encapsulation, problems are solved at the
interface level. implementation level.
Abstraction is the method of hiding the unwanted Whereas encapsulation is a method to hide the data in
information. a single entity or unit along with a method to protect
information from outside.
We can implement abstraction using abstract class and Whereas encapsulation can be implemented using by
interfaces. access modifier i.e. private, protected and public.
In abstraction, implementation complexities are hidden While in encapsulation, the data is hidden using
using abstract classes and interfaces. methods of getters and setters.
The objects that help to perform abstraction are Whereas the objects that result in encapsulation need
encapsulated. not be abstracted.
How would you explain the importance of encapsulation? How does encapsulation ensure
data security?
Encapsulation is one of the foundation pillars of OOP. In essence, Encapsulation means you should limit the access and
visibility of a variable or function to as little as possible. A variable that is required only within an if block should be
defined within the block, not at a higher level. A function of a class that will be called only from within the class should be
declared private. A variable that can be read but not modified by the outsiders should have a public getter function but
no public setter function (and the variable itself should be private to the class).
If you follow these principles, these will help you big time when debugging your code. When something turns out to
behave in a way that it is not supposed to, you can always quickly zoom in to the culprit code. For example, if a private
variable of a class has a value that it shouldn’t have, you can just focus on that class’s code instead of the entire code base
since you know for sure that this variable is not modifiable from the outside.
For a layman, think of it as a car with no locking system and no keys. Anyone could just walk to the car, open the door and
press the Start button on the dashboard to use it. If that car gets stolen, where do you start looking for the thief? It could be
anyone since everyone has got access to the start button. On the other hand, consider the same car with a locking system
and a pair of duplicate keys; one of which you keep with yourself and give one to a close friend. Now if the car goes
missing and you have your keys in your pocket, where do you start looking for it? Yep, You look for Brutus! So just keep
everything as much encapsulated as feasible (but no more!)
Why are we using encapsulation? If we are using it, then what is the benefit of using it?

When writing large object-oriented applications, you will often come across scenarios where you want to perform
validation on inputs, and scenarios where you might want a change made on one member variable to edit some other
internal values (for caching etc.).

A simple example is one where you have a object for a user, defined as

The user can enter a value of -10, which although is a valid number, but an invalid age. A
setter method could have logic which would allow you to catch such things.
Q 1 - Which among the following should be
encapsulated?
a) The data which is prone to change is near future
b) The data prone to change in long terms
c) The data which is intended to be changed
d) The data which belongs to some other class

Answer: a

Explanation: The data prone to change in near future is usually encapsulated so that it doesn’t get
changed accidentally. We encapsulate the data to hide the critical working of program from outside
world.
Q 2 - How can Encapsulation be achieved?
a) Using Access Specifiers
b) Using only private members
c) Using inheritance
d) Using Abstraction

Answer: a

Explanation: Using access specifiers we can achieve encapsulation. Using this we can in turn
implement data abstraction. It’s not necessary that we only use private access.
Q3 - Find which of the following uses
encapsulation?

Answer: c
Explanation: It is the class which uses both the data members and member functions being declared
inside a single unit. Only data members can be there in structures also. And the encapsulation can
only be illustrated if some data/operations are associated within class.
Q 4 – Identify the correct option?
a) This code is correct and supports encapsulation
b) May result in undesirable conditions
c) Have compile time error
d) This code violates encapsulation

Answer: d
Explanation: This code violates the encapsulation. By this
code we can get the address of the private member of the
class, hence we can change the value of private member,
which is against the rules.
Q 5. A phone is made up of many components like motherboard, camera,
sensors and etc. If the processor represents all the functioning of phone,
display shows the display only, and the phone is represented as a whole.
Which among the following have highest level of abstraction?

a) Motherboard
b) Display
c) Camera
d) Phone

Answer: d
Explanation: Phone as a whole have the highest level of abstraction. This is because the phone being a single unit
represents the whole system. Whereas motherboard, display and camera are its components.
Q 6. If two classes combine some private data members and provides public
member functions to access and manipulate those data members. Where is
abstraction used?

a) Using private access specifier for data members


b) Using class concept with both data members and member functions
c) Using public member functions to access and manipulate the data members
d) Data is not sufficient to decide what is being used

Answer: c
Explanation: It is the concept of hiding program complexity and actual working in background. Hence use of
public member functions illustrates abstraction here.
Q 7 – Encapsulation and abstraction
differ as ____________
a) Binding and Hiding respectively
b) Hiding and Binding respectively
c) Can be used any way
d) Hiding and hiding respectively

Answer: a
Explanation: Abstraction is hiding the complex code. For example, we directly use cout object in
C++ but we don’t know how is it actually implemented. Encapsulation is data binding, as in, we try
to combine a similar type of data and functions together.
Q 8 – Abstraction Principle includes
a) Use abstraction at its minimum
b) Use abstraction to avoid longer codes
c) Use abstraction whenever possible to avoid duplication
d) Use abstraction whenever possible to achieve OOP

Answer: c
Explanation: Abstraction principle includes use of abstraction to avoid duplication (usually of
code). It this way the program doesn’t contain any redundant functions and make the program
efficient.
Q 9 – Abstraction can apply to
a) Control and data
b) Only data
c) Only control
d) Classes

Answer: a
Explanation: Abstraction applies to both. Control abstraction involves use of subroutines and
control flow abstraction. Data abstraction involves handling pieces of data in meaningful ways.
Q 10 – In OOD, there is at least one extra step before you get
to the coding of algorithms. That step is the design of:

a) classes that are appropriate for the problem at hand


b) Member functions
c) Program control flow
d) The class’s interface

Answer: a
Explanation: Designing member functions and the class's interface are actually parts of designing
classes that are appropriate for the problem at hand. Designing program control is a software
design step that is needed regardless of whether the program is being written in a procedural
programming language or in an object-oriented programming language.

You might also like