vinitsinghthakur46
19 views
34 slides
Oct 09, 2024
Slide 1 of 34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
About This Presentation
Dedicated Project file of Encapsulation
Size: 695.72 KB
Language: en
Added: Oct 09, 2024
Slides: 34 pages
Slide Content
By The Department of Computer Science & Engineering Patel college of science and technology Submitted By :-- Vinit Singh Pathir Anshu Prajapat Submitted To :-- Mr.Vikas Kalme Sir Session 2023-24
Topic Introduction What is Encapsulation? Real Life example of Encapsulation Advantages of Encapsulations Important to know Example of Encapsulation in C++ Types Of Encapsulation Why we use Encapsulation?
Introduction to Encapsulation
Introduction Encapsulation means binding (joining) a variables (data) with a function (Methods).
Introduction Encapsulation means binding (joining) a variables (data) with a function (Methods). E.g. When we encapsulate a class
Introduction Encapsulation means binding (joining) a variables (data) with a function (Methods). It keep the important but unnecessary data away from the user of the program E.g. When we encapsulate a class
Encapsulation in C++
Encapsulation in C++ is defined as the wrapping up of data and information in a single unit. What is Encapsulation in C++? In Object Oriented Programming (OOP), Encapsulation is defined as binding together the data and the functions that manipulate them.
Real life example of Encapsulation
Real Life Practical example of Encapsulation As we know ,in a company, there are different sections like the accounts section, finance section, sales section, etc. 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. This is what encapsulation means our computer programming
Why we need to use Encapsulation ?
Why we need to use Encapsulation ? Encapsulation is a way to restrict the direct access to some components of an object, so users cannot access state values for all of the variables of a particular object. Encapsulation can be used to hide both data members and data functions or methods associated with an instantiated class or object. The primary purpose of encapsulation is to hide an object’s internal representation or state from the outside. This concept is also known as information hiding.
Advantages Of Encapsulate
Advantages of Encapsulations Data Hiding : Restricts access to data members, hiding implementation details. Control : Provides better control over class attributes and methods. Security: Increases security of data . Flexibility : Allows changes to one part of the code without affecting others. Reusability: By defining one class, we can create many objects with consistent behavior.
Types Of Encapsulate
Types Of Encapsulation There are three basic types Encapsulation Member variable encapsulation, Function encapsulation and Class encapsulation Encapsulation Member variable encapsulati -on Function encapsulati -on Class encapsulati -on
Member Variable Encapsulation
Member Variable Encapsulation: In this type, data members (also known as attributes or fields) are encapsulated within a class.
Member Variable Encapsulation: Data members are typically private by default, ensuring that they are not directly accessible from outside the class. In this type, data members (also known as attributes or fields) are encapsulated within a class.
Member Variable Encapsulation: Data members are typically private by default, ensuring that they are not directly accessible from outside the class. To access these data members, we use setter and getter methods (also known as mutator and accessor methods). In this type, data members (also known as attributes or fields) are encapsulated within a class.
Function Encapsulation
Function Encapsulation: Function encapsulation refers to marking a method of code as private to prevent unauthorized access.
Function Encapsulation: Function encapsulation refers to marking a method of code as private to prevent unauthorized access. These private methods are hidden from external code.
Function Encapsulation: Function encapsulation refers to marking a method of code as private to prevent unauthorized access. These private methods are hidden from external code. It ensures that only authorized parts of the code can call specific functions.
Class Encapsulation
Class Encapsulation: Class encapsulation involves declaring an entire class as private to restrict user access.
Class Encapsulation: Class encapsulation involves declaring an entire class as private to restrict user access. This means that other classes cannot directly interact with the encapsulated class.
Class Encapsulation: Class encapsulation involves declaring an entire class as private to restrict user access. It allows hiding implementation details and organizing related data and methods together. This means that other classes cannot directly interact with the encapsulated class.
Example of Encapsulation in C++
Example of Encapsulation in C++ In this example: We calculate the area of a rectangle using the getArea () method. The length and breadth data members are private, encapsulating them within the Rectangle class. The public interface allows access to the getArea () method.
Important to know
Encapsulation is counted as one in four main pillar Of Object Oriented Programming (OOPs)
Additional Notes: Getter and setter functions provide controlled access to class members. Encapsulation is not just data hiding; it’s about bundling related fields and methods together. It improves readability, maintainability, and security.