ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Object Oriented Programming
    COMP2111
    Progress0 / 23 topics
    Topics
    1. Introduction to object oriented design2. History and advantages of object oriented design3. Introduction to object oriented programming concepts4. Classes and objects5. Data encapsulation6. Constructors and destructors7. Access modifiers8. Const vs non-const functions9. Static data members & functions10. Function overloading11. Operator overloading12. Identification of classes and their relationships13. Composition and aggregation14. Inheritance15. Multiple inheritance16. Polymorphism17. Abstract classes and interfaces18. Generic programming concepts19. Function & class templates20. Standard template library21. Object streams22. Data and object serialization using object streams23. Exception handling
    COMP2111›Data encapsulation
    Object Oriented ProgrammingTopic 5 of 23

    Data encapsulation

    2 minread
    290words
    Beginnerlevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time2 min
      Word count290
      Code examples0
      DifficultyBeginner