Encapsulation C++ Piller of OOP it is the important piller
an7539661
126 views
15 slides
Apr 02, 2024
Slide 1 of 15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
About This Presentation
This i ppt of oop leacture piller of oop
Size: 134.84 KB
Language: en
Added: Apr 02, 2024
Slides: 15 pages
Slide Content
CS Lecturer: Bibi Maryam
Encapsulation
Encapsulation Def. The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must declare class variables/attributes as private (cannot be accessed from outside the class). If you want others to read or modify the value of a private member, you can provide public get and set methods.
Encapsulation’s Important points : Encapsulation involves combining similar data and functions into a single unit called a class . By encapsulating these functions and data, we protect that data from change. This concept is also known as wrapping data or information hiding . In Object Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulate them.
Two Important properties of Encapsulation: Data Protection: Encapsulation protects the internal state of an object by keeping its data members private. Access to and modification of these data members is restricted to the class’s public methods, ensuring controlled and secure data manipulation . Information Hiding: Encapsulation hides the internal implementation details of a class from external code. Only the public interface of the class is accessible, providing abstraction and simplifying the usage of the class while allowing the internal implementation to be modified without impacting external code.
A real-life example of Encapsulation In a company, there are different sections like the accounts section, finance section, sales section, etc. Now , The finance section handles all the financial transactions and keeps records of all the data related to finance. Similarly, the sales section handles all the sales-related activities and keeps records of all the sales. Now there may arise a situation when for some reason an official from the 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 the sales section. He will first have to contact some other officer in the sales section and then request him to give the particular data.
Features of Encapsulation: Below are the features of encapsulation : We can not access any function from the class directly. We need an object to access that function that is using the member variables of that class. The function which we are making inside the class must use only member variables, only then it is called encapsulation . If we don’t make a function inside the class which is using the member variable of the class then we can’t call it encapsulation. Encapsulation improves readability, maintainability, and security by grouping data and methods together. It helps to control the modification of our data members.
Example: #include <iostream> using namespace std; // declaring class class Circle { // access modifier private: // Data Member float area; float radius; public: void getRadius() { cout << "Enter radius\n"; cin >> radius; } void findArea() { area = 3.14 * radius * radius; cout << "Area of circle=" << area; } }; int main() { // creating instance(object) of class Circle cir; cir.getRadius(); // calling function cir.findArea(); // calling function } Output: Enter radius Area of circle=0
Role of Access Specifiers in Encapsulation: Access specifiers facilitate Data Hiding in C++ programs by restricting access to the class member functions and data members. There are three types of access specifiers in C++: Private: Private access specifier means that the member function or data member can only be accessed by other member functions of the same class. Protected: A protected access specifier means that the member function or data member can be accessed by other member functions of the same class or by derived classes. Public: Public access specifier means that the member function or data member can be accessed by any code. By default , all data members and member functions of a class are made private by the compiler.
Points to Consider: As we have study, access specifiers play an important role in implementing encapsulation in C++. The process of implementing encapsulation can be sub-divided into two steps: Creating a class to encapsulate all the data and methods into a single unit. Hiding relevant data using access specifiers.
Getter & Setter methods: The getter function is used to retrieve the variable value and the setter function is used to set the variable value . Remember: You can directly access public member variables, but private member variables are not accessible. Therefore, we need getter functions.
Access Private Members: To access a private attribute, use public "get" and "set" methods : #include <iostream> using namespace std; class Employee { private : // Private attribute int salary; public : // Setter void setSalary( int s) { salary = s; } // Getter int getSalary() { return salary; } }; int main() { Employee myObj; myObj.setSalary( 50000 ); cout << myObj.getSalary(); return ; }
Why Encapsulation? It is considered good practice to declare your class attributes as private (as often as you can). Encapsulation ensures better control of your data, because you (or others) can change one part of the code without affecting other parts. Increased security of data.