ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP2111Data Encapsulation
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP2111

    Object Oriented Programming

    Progress0 / 23 done
    Topics23
    1Introduction to object oriented design2History and advantages of object oriented design3Introduction to object oriented programming concepts4Classes and objects5Data encapsulation6Constructors and destructors7Access modifiers8Const vs non-const functions9Static data members & functions10Function overloading11Operator overloading12Identification of classes and their relationships13Composition and aggregation14Inheritance15Multiple inheritance16Polymorphism17Abstract classes and interfaces18Generic programming concepts19Function & class templates20Standard template library21Object streams22Data and object serialization using object streams23Exception handling
    2 min read
    Topic 5 of 23
    Topic Study Note Illustration
    05

    Data encapsulation

    Data Encapsulation

    Data Encapsulation

    Data encapsulation is one of the core principles of Object-Oriented Programming. It means combining data (variables) and functions (methods) that work on that data into a single unit—a class. It also involves hiding the internal details of how the class works and only allowing controlled access to the data.


    Why Encapsulation is Important:

    1. Protects data from unwanted access or changes
    2. Controls how data is accessed or modified
    3. Improves security and reliability of the program
    4. Makes the code easier to maintain and update

    How Encapsulation is Done in C++:

    1. Keep data members private (so they cannot be accessed directly from outside the class)
    2. Use public methods (getters and setters) to allow controlled access

    Example:

    class BankAccount {
    private:
        int balance;  // Private data member
    
    public:
        void setBalance(int amount) {
            if (amount >= 0) {
                balance = amount;
            }
        }
    
        int getBalance() {
            return balance;
        }
    };
    

    In this example:

    • balance is private, so it cannot be accessed or changed directly from outside.
    • setBalance() and getBalance() are public functions that control how the balance is changed or viewed.
    • This is encapsulation—hiding the internal data and giving access through methods.

    Access Specifiers Used in Encapsulation:

    • private: Only accessible within the class.
    • public: Accessible from outside the class.
    • protected: Accessible in the class and derived classes.

    Benefits of Encapsulation:

    • Data security: Prevents accidental or unauthorized changes.
    • Easy maintenance: Changing internal code doesn't affect outside code as long as the public interface remains the same.
    • Flexibility: You can change how data is stored or validated without affecting the rest of the program.

    Encapsulation helps keep code safe, clean, and well-structured, which is especially important in large or complex projects.

    ← Previous topic 4
    Classes and objects
    Next topic 6 →
    Constructors and destructors

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    2mremaining
    290words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Object Oriented Programming