Section 6 : Friend+static+namespaces (again but more detailed) Flashcards
(25 cards)
What is a friend function in C++ and why is it used?
A friend function is a non-member function declared with the friend keyword inside a class. It has access to the class’s private and protected members.
Used to grant specific external functions access without exposing everything publicly.
What does it mean to declare a class as a friend?
A friend class can access the private and protected members of another class. This is useful when two classes work closely together and need internal access.
What does static mean for a variable declared inside a function?
It means the variable retains its value between function calls. It’s only initialized once and lives for the entire program duration.
What is a static class variable and how is it shared?
A static class variable is shared across all instances of a class. Only one copy exists in memory, and it must be defined outside the class using the :: operator.
What makes a method static in a class?
A static method belongs to the class, not an object. It can only access other static members and does not require object instantiation to be called.
Why are static members useful?
They are used to store global state across all objects, like counting instances, sharing configs, or utility methods that don’t depend on object data.
What is the purpose of a namespace in C++?
A namespace is a named scope used to group identifiers and avoid name collisions between libraries or modules.
How do you call a function from a namespace?
Use the scope resolution operator ::
Geometry::area(5);
What is namespace pollution and how to avoid it?
Namespace pollution occurs when too many names are pulled into the global scope (e.g., using namespace std;), increasing risk of conflicts. Prefer scoped access or selective using.
Why must both classes be defined before the friend function?
Because a friend function needs access to both classes’ private data, both class definitions must be known before the function is implemented.
List two advantages of friend functions.
Acts as a bridge between two classes by accessing private data.
Helps overload operators more flexibly.
List two disadvantages of friend functions.
Violates encapsulation by accessing private members from outside.
Cannot participate in runtime polymorphism (not virtual).
How do you access a function or variable from a namespace?
Use the scope resolution operator ::
namespace_name::function();
namespace_name::variable;
What does the using namespace directive mean?
It tells the compiler that all unqualified identifiers should be resolved in the specified namespace, so you don’t need to prefix them.
using namespace std; // allows cout instead of std::cout
How long does a using namespace statement remain active?
If used outside any function, it applies globally (for all functions).
If used inside a function, the namespace is available only within that function.
What happens if two namespaces define a function with the same name?
There’s no conflict unless both are used without qualification. If needed, call explicitly:
first_space::func();
second_space::func();
Given this code:
namespace A { void func() { cout «_space;“A”; } }
namespace B { void func() { cout «_space;“B”; } }
using namespace A;
int main() { func(); }
It will print A, because func() refers to A::func() due to the using directive.
Should you use using namespace std;?
Only in small, single-file programs (e.g., beginner or academic examples).
In large or production code, it’s discouraged because it can cause namespace pollution and name clashes.
What is meant by a “stream” in C++?
A stream is a sequence of bytes used for input or output operations — data flows into or out of a program.
What is an input stream?
A stream where data flows from the input device (e.g., keyboard) to the main memory.
C++ uses cin for this.
What is an output stream?
A stream where data flows from memory to an output device like a display screen.
C++ uses cout for this.
List and explain the 3 major C++ I/O header files.
include <iostream>: For standard input/output using cin, cout, cerr.</iostream>
What is cin?
cin is an object of the istream class used for reading user input (usually from keyboard) using»_space;.
What is cout?
cout is an object of the ostream class used to print output to the screen using «.