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›Access modifiers
    Object Oriented ProgrammingTopic 7 of 23

    Access modifiers

    2 minread
    299words
    Beginnerlevel

    Access Modifiers

    Access modifiers in C++ are keywords used to set the access level of class members (variables and functions). They control who can access the members of a class from outside or inside the class.

    There are three main access modifiers in C++:


    1. Public

    • Members declared as public are accessible from anywhere in the program where the object is visible.
    • These are often used for functions that need to be called from outside the class.
    class Student {
    public:
        int rollNo;           // Can be accessed directly
        void display() {
            cout << rollNo;
        }
    };
    

    2. Private

    • Members declared as private are accessible only inside the class.
    • They cannot be accessed directly from outside the class.
    • This is used for data protection (encapsulation).
    class Student {
    private:
        int marks;           // Cannot be accessed from outside
    public:
        void setMarks(int m) {
            marks = m;
        }
        int getMarks() {
            return marks;
        }
    };
    

    In this example, marks can only be accessed using setMarks() and getMarks().


    3. Protected

    • Members declared as protected are like private, but they can also be accessed in derived (child) classes.
    • Commonly used in inheritance.
    class Person {
    protected:
        string name;
    };
    
    class Student : public Person {
    public:
        void setName(string n) {
            name = n;      // Allowed because name is protected
        }
    };
    

    Summary Table:

    Access Modifier Accessible Within Class Accessible Outside Class Accessible in Derived Class
    public Yes Yes Yes
    private Yes No No
    protected Yes No Yes

    Why Access Modifiers Are Important:

    • Help protect data from unauthorized access or modification
    • Allow abstraction and encapsulation
    • Control how other parts of the program interact with a class

    Using access modifiers properly makes the code more secure, organized, and easier to manage.

    Previous topic 6
    Constructors and destructors
    Next topic 8
    Const vs non-const functions

    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 count299
      Code examples0
      DifficultyBeginner