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 PortalCOMP2111Access Modifiers
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 7 of 23
    Topic Study Note Illustration
    07

    Access modifiers

    Access Modifiers

    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

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

    No headings found

    Study Toolkit

    Subject Portal

    Object Oriented Programming