Session 8
Session 8
Session 8
• Internal implementation can be changed without affecting the user level code
class implementAbstraction
int main()
{
private: {
int a, b; implementAbstraction obj;
obj.set(10, 20);
public: obj.display();
return 0;
// method to set values of
}
// private members
void set(int x, int y)
{
a = x;
Output:
b = y;
} a = 10 b = 20
void display()
{
cout<<"a = " <<a << endl;
cout<<"b = " << b << endl;
}
};
Application of Abstraction
• For example, when you send an email to someone you just click send and
you get the success message, what actually happens when you click send,
how data is transmitted over network to the recipient is hidden from you
ENCAPSULATION
• DATA ENCAPSULATION
– Encapsulation is defined as wrapping up of data and information under a
single unit.
– In OOP, Encapsulation is defined as binding together the data and the
functions that manipulates them.
– Combining data and functions into a single unit called class and the process is
known as Encapsulation.
Encapsulation also helps us to make a flexible code which is easy to change and
maintain.
Example
• The common example of encapsulation is Capsule.
• Real-life Example:
– In an organization, there are different sections like the Finance Section, Sales section, etc…
– If an official from Finance section needs all the data about sales in a particular month, he
is not allowed to directly access the data of sales section. He has to contact some other
officer in Sales section
ENCAPSULATION
ADVANTAGES
• Encapsulated classes reduce complexity.
Note: A private member function cannot be accessed outside the class. So it cannot be
accessed inside the main function
MCQ
1. Wrapping data and its related functionality into a single entity is known as
_____________
a) Abstraction
b) Encapsulation
c) Polymorphism
d) Modularity