Bird
Raised Fist0
C++programming~10 mins

Access specifiers in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Access specifiers
Start Class Definition
↓
Declare Members
↓
Apply Access Specifiers
↓
public
↓
Accessible
↓
Use Members in Code
↓
Check Access Rules
↓
Allow or Deny Access
↓
End
This flow shows how class members are declared with access specifiers and how access is checked during usage.
Execution Sample
C++
class MyClass {
public:
  int x;
private:
  int y;
};

MyClass obj;
obj.x = 5; // allowed
obj.y = 10; // error
This code defines a class with public and private members and shows which members can be accessed.
Execution Table
StepCode LineActionAccess CheckResult
1class MyClass {Start class definitionN/AClass created
2public:Set access to publicN/AMembers below are public
3int x;Declare member xPublicAccessible anywhere
4private:Set access to privateN/AMembers below are private
5int y;Declare member yPrivateAccessible only inside class
6};End class definitionN/AClass finalized
7MyClass obj;Create object objN/AObject created
8obj.x = 5;Access public member xAllowedValue 5 assigned
9obj.y = 10;Access private member yDeniedCompilation error
💡 Access to private member y is denied, stopping compilation.
Variable Tracker
VariableStartAfter Step 8After Step 9
obj.xundefined55
obj.yundefinedundefinedundefined (access denied)
Key Moments - 2 Insights
Why can we assign to obj.x but not to obj.y?
obj.x is declared public (see step 3 and 8), so it is accessible outside the class. obj.y is private (step 5), so access outside the class is denied (step 9).
What happens if we try to access a private member from outside the class?
The compiler stops the program with an error (step 9), because private members are only accessible inside the class.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the access level of member x?
Aprivate
Bpublic
Cprotected
Ddefault
💡 Hint
See step 3 and 8 in the execution_table where x is declared public and accessed successfully.
At which step does the program stop due to access violation?
AStep 7
BStep 8
CStep 9
DStep 6
💡 Hint
Check step 9 in the execution_table where accessing private member y is denied.
If member y was declared public instead of private, what would happen at step 9?
AValue 10 assigned successfully
BRuntime error occurs
CCompilation error remains
DMember y is inaccessible
💡 Hint
Refer to step 8 where public member x is assigned successfully; public y would behave the same.
Concept Snapshot
Access specifiers control who can use class members.
public: accessible anywhere.
private: accessible only inside the class.
protected: accessible inside class and subclasses.
Use them to protect data and control access.
Full Transcript
This visual trace shows how access specifiers in C++ control access to class members. The class MyClass has two members: x declared public and y declared private. When creating an object obj, accessing obj.x is allowed and assigns the value 5. However, accessing obj.y causes a compilation error because y is private and cannot be accessed outside the class. The variable tracker shows obj.x changes from undefined to 5, while obj.y remains inaccessible. Key moments clarify why public members are accessible and private members are not. The quiz tests understanding of access levels and error steps. Remember, access specifiers help protect data and define who can use class parts.

Practice

(1/5)
1. Which access specifier allows members of a class to be accessed from anywhere in the program?
easy
A. protected
B. private
C. public
D. internal

Solution

  1. Step 1: Understand access specifiers

    Public members are accessible from any part of the program, unlike private or protected.
  2. Step 2: Compare with other specifiers

    Private restricts access to inside the class only; protected allows access in derived classes.
  3. Final Answer:

    public -> Option C
  4. Quick Check:

    Access anywhere = public [OK]
Hint: Public means accessible everywhere, private means hidden [OK]
Common Mistakes:
  • Confusing private with public
  • Thinking protected is accessible everywhere
  • Assuming internal is a C++ specifier
2. Which of the following is the correct way to declare a private member variable int age inside a class?
easy
A. private int age;
B. private: int age;
C. int age private;
D. int private age;

Solution

  1. Step 1: Recall C++ syntax for access specifiers

    Access specifiers are labels followed by a colon, then members are declared below.
  2. Step 2: Identify correct syntax

    private: int age; uses 'private:' followed by 'int age;' which is correct syntax.
  3. Final Answer:

    private: int age; -> Option B
  4. Quick Check:

    Access specifier label + colon = correct [OK]
Hint: Access specifiers end with colon, members declared below [OK]
Common Mistakes:
  • Placing access specifier before type
  • Missing colon after access specifier
  • Trying to put access specifier after variable
3. What will be the output of this code?
class Test {
private:
    int x = 5;
public:
    int getX() { return x; }
};

int main() {
    Test t;
    // std::cout << t.x << std::endl; // Line A
    std::cout << t.getX() << std::endl;
    return 0;
}
medium
A. 5
B. Compilation error at Line A
C. 0
D. Runtime error

Solution

  1. Step 1: Understand private member access

    The variable x is private, so direct access like t.x is not allowed (Line A is commented out).
  2. Step 2: Use public getter method

    The method getX() is public and returns x's value, so std::cout << t.getX() prints 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Private hidden, public getter shows 5 [OK]
Hint: Private members need public methods to access outside [OK]
Common Mistakes:
  • Trying to access private variable directly
  • Expecting 0 as default value
  • Confusing compilation and runtime errors
4. Identify the error in this code snippet:
class Base {
protected:
    int value = 10;
};

class Derived : public Base {
public:
    void show() {
        std::cout << value << std::endl;
    }
};

int main() {
    Derived d;
    d.show();
    std::cout << d.value << std::endl;
    return 0;
}
medium
A. Error: Cannot access protected member 'value' from main using object
B. Error: Cannot access protected member 'value' inside Derived class
C. Error: Missing semicolon after class Derived
D. No error, code runs and prints 10 twice

Solution

  1. Step 1: Check access to protected member inside Derived

    Derived class can access protected members of Base, so d.show() works and prints 10.
  2. Step 2: Check access to protected member from main

    Main function cannot access protected members directly via object, so std::cout << d.value causes compilation error.
  3. Final Answer:

    Error: Cannot access protected member 'value' from main using object -> Option A
  4. Quick Check:

    Protected accessible in subclass, not outside [OK]
Hint: Protected members hidden outside class and subclasses [OK]
Common Mistakes:
  • Thinking protected is public
  • Assuming main can access protected members
  • Missing error on direct access in main
5. You want to design a class where some data is hidden from everyone except derived classes, and some functions are accessible everywhere. Which access specifiers should you use for data and functions respectively?
hard
A. Data: private, Functions: public
B. Data: protected, Functions: private
C. Data: public, Functions: protected
D. Data: protected, Functions: public

Solution

  1. Step 1: Determine data access for derived classes only

    Protected data is hidden from outside but accessible in derived classes, fitting the requirement.
  2. Step 2: Determine function access for everyone

    Functions accessible everywhere must be public.
  3. Final Answer:

    Data: protected, Functions: public -> Option D
  4. Quick Check:

    Protected data + public functions = correct design [OK]
Hint: Protected hides data, public shares functions [OK]
Common Mistakes:
  • Using private data blocks derived classes
  • Making functions private hides them outside
  • Confusing public and protected roles
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard