The three main access specifiers in C++ are <strong>public</strong>, <strong>private</strong>, and <strong>protected</strong>. They control the visibility of class members.
Click to reveal answer
beginner
What does the private access specifier do?
Members declared private can only be accessed within the same class. They are hidden from outside code and derived classes.
Click to reveal answer
intermediate
How does protected differ from private?
<code>protected</code> members are like <code>private</code> but can also be accessed by derived (child) classes, not just the class itself.
Click to reveal answer
beginner
What is the default access specifier for class members in C++?
By default, members of a class are private if no access specifier is given.
Click to reveal answer
beginner
What is the default access specifier for struct members in C++?
By default, members of a struct are public if no access specifier is given.
Click to reveal answer
Which access specifier allows members to be accessed from anywhere?
Apublic
Bprivate
Cprotected
Dinternal
✗ Incorrect
public members can be accessed from any part of the program.
If no access specifier is given in a class, what is the default?
Apublic
Bprivate
Cprotected
Dfriend
✗ Incorrect
Class members are private by default.
Which access specifier allows derived classes to access members but hides them from outside?
Apublic
Bstatic
Cprivate
Dprotected
✗ Incorrect
protected members are accessible in derived classes but not outside.
What is the default access specifier for members of a struct?
Apublic
Bprotected
Cprivate
Dnone
✗ Incorrect
Struct members are public by default.
Can private members be accessed directly by derived classes?
AOnly if friend class
BYes
CNo
DOnly if public inheritance
✗ Incorrect
Private members are not accessible by derived classes directly.
Explain the difference between public, private, and protected access specifiers in C++.
Think about who can see or use the class members.
You got /3 concepts.
What are the default access levels for class and struct members in C++?
Remember the difference between class and struct.
You got /2 concepts.
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
Step 1: Understand access specifiers
Public members are accessible from any part of the program, unlike private or protected.
Step 2: Compare with other specifiers
Private restricts access to inside the class only; protected allows access in derived classes.
Final Answer:
public -> Option C
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
Step 1: Recall C++ syntax for access specifiers
Access specifiers are labels followed by a colon, then members are declared below.
Step 2: Identify correct syntax
private:
int age; uses 'private:' followed by 'int age;' which is correct syntax.
Final Answer:
private:
int age; -> Option B
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
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).
Step 2: Use public getter method
The method getX() is public and returns x's value, so std::cout << t.getX() prints 5.
Final Answer:
5 -> Option A
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
Step 1: Check access to protected member inside Derived
Derived class can access protected members of Base, so d.show() works and prints 10.
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.
Final Answer:
Error: Cannot access protected member 'value' from main using object -> Option A
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
Step 1: Determine data access for derived classes only
Protected data is hidden from outside but accessible in derived classes, fitting the requirement.
Step 2: Determine function access for everyone
Functions accessible everywhere must be public.
Final Answer:
Data: protected, Functions: public -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.