0% found this document useful (0 votes)
5 views19 pages

21CSC101T: Abstraction and Encapsulation

The document discusses the concepts of abstraction and encapsulation in object-oriented programming, specifically using C++. Abstraction focuses on hiding implementation details while exposing essential features through classes and access specifiers, whereas encapsulation involves bundling data and functions together to protect and manage them. Examples illustrate how public and private members function within classes, emphasizing the importance of these concepts in software design.

Uploaded by

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

21CSC101T: Abstraction and Encapsulation

The document discusses the concepts of abstraction and encapsulation in object-oriented programming, specifically using C++. Abstraction focuses on hiding implementation details while exposing essential features through classes and access specifiers, whereas encapsulation involves bundling data and functions together to protect and manage them. Examples illustrate how public and private members function within classes, emphasizing the importance of these concepts in software design.

Uploaded by

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

21CSC101T

Object Oriented Design and Programming

Abstraction and
Encapsulation
Abstraction
• Abstraction is the process of providing only the essential details to the outside
world and hiding the internal details
• That is, representing only the essential details and hiding the implementation
details

• Separation of the interface and implementation details of the program

• Real life examples: AC, Car,..


Abstraction using Classes and Access
Specifiers
• Class helps us to group data members and member functions
• Access specifiers enforce restrictions on class members
• We can decide which data member will be visible to the outside world
and which is not
• Members declared as public can be accessed from anywhere in the
program

• Members declared as private can be accessed only from within the


class. They are not allowed to be accessed from any part of code
outside the class
Example of declaring Public members of C++ class
#include <iostream>
using namespace std;
class sample
{
public:
Output:
int a,b;
public: The values are:20 50
void display()
{
cout <<“The values are:”<< a << " " << b<<end;
}
};
int main()
{
sample s;
s.a=20;
s.b=50;
s.display();
}
Example of declaring Public members of C++ class
#include <iostream>
using namespace std;
class sample
{
public:
Output:
int a,b;
public: Enter the values:20 50
void get_val() 20 50
{
cout<<"Enter the values:";
cin >>a>>b;
}
void display()
{
cout<<a<<" "<<b<<endl;
}
};
int main()
{
sample s;
s.get_val();
s.display();
Private member example in which member data cannot be accessed outside the
class
• :

#include <iostream>
using namespace std;
class sample {
public: If you execute the above program, the private
int a, b; member function will not be accessible and
public: hence the following error message will appear
void get_val() like this in compiler:
{
cout<< "Enter Two values : ";
cin>>a>>b;
}
private:
void display()
{ cout << a << " " << b;
cout << endl;
}
};
int main()
{
sample s;
s.get_val();
s.display();
}
Abstraction is an Object-Oriented Programming
(OOP) concept that hides implementation details
and only shows essential features to the user. In
C++, we achieve abstraction using classes and
access modifiers (private, public).

Abstraction
Only necessary functions (setDetails(), displayDetails())
are exposed.
Private data (rollNo, name, marks) is hidden from direct
access.
class implementAbstraction
int main()
{
private: {
int a, b; implementAbstraction obj;
obj.set(10, 20);
public:
// method to set values of obj.display();
// private members return 0;
void set(int x, int y) }
{
a = x;
b = y;
} Output:
a = 10
void display()
{ b = 20
cout<<"a = " <<a << endl;
cout<<"b = " << b << endl;
}
};
Data Hiding → Private members : a, b cannot be accessed directly. That
is hidden from direct access.
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.

• Provides security, flexibility and easy maintainability


Encapsulation helps us in binding the data(instance variables) and the member
functions(that work on the instance variables) of a class.

Encapsulation is also useful in hiding the data(instance variables) of a class from


an illegal direct access.

Encapsulation also helps us to make a flexible code which is easy to change and
maintain.
Example

• The common example of encapsulation is Capsule.

• In capsule all medicine are encapsulated in side 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.

• Help protect our data. A client cannot change an Account's balance if we


encapsulate it.

• Encapsulated classes are easier to change.


Nesting of Member Functions
Private Member Functions

Note: s.read() will


not work

Note: A private member function cannot be accessed outside the class. So it cannot be
accessed inside the main function

You might also like