Which of the following statements related to C++ is FALSE?
2023
Which of the following statements related to C++ is FALSE?
Answer: C. A member function cannot be defined as private. — In C++, every member function carries one of three access specifiers - public (callable from anywhere), protected (callable from the class and its derived…
- A.
A member function can be defined as public.
- B.
A member function can be defined inside the class.
- C.
A member function cannot be defined as private.
- D.
A member function can be defined outside the class.
Attempted by 410 students.
Show answer & explanation
Correct answer: C
In C++, every member function carries one of three access specifiers - public (callable from anywhere), protected (callable from the class and its derived classes), or private (callable only from inside the class itself or its friends) - exactly like a data member. A member function can also be written either inside the class body, where it is implicitly inline, or outside it using the ClassName::function scope-resolution syntax.
Checking each option against this rule: marking a function public, defining it inside the class body, and defining it outside the class with ClassName::function are all standard, permitted patterns. Marking a function private is equally permitted - private is one of the three specifiers a member function can carry, not an excluded one - so the option claiming a member function cannot be private is the one that contradicts the rule.
Public:
class C { public: void f(); };compiles, and f() is callable from outside the class.Defined inside the class:
class C { void f() { /* body */ } };is allowed, and the definition is implicitly inline.Private:
class C { private: void f(); };also compiles - a member function can be private; the specifier only restricts who may call it to the class and its friends.Defined outside the class:
void C::f() { /* body */ }is allowed - the scope-resolution operator ties the definition back to its declaration.
So the option asserting that a member function cannot be defined as private is the false one - private is a fully valid access specifier for member functions; it only narrows who may call the function, it does not forbid it.
A video solution is available for this question — log in and enroll to watch it.