Section A — 1 Mark Questions
1. What is the primary purpose of an inline function? [1 Mark]
Answer: To reduce the function call overhead by replacing the function call with the actual function code.
2. Which keyword is used to declare an inline function? [1 Mark]
Answer: The inline keyword.
3. Does the inline keyword guarantee that a function will be inlined? [1 Mark]
Answer: No, it is a request to the compiler, which the compiler may ignore.
4. Can a function containing a static variable be inlined? [1 Mark]
Answer: Generally, no; compilers typically refuse to inline functions containing static variables.
5. What happens if a function definition is complex, like containing a loop or switch, regarding inlining? [1 Mark]
Answer: The compiler usually ignores the inline request for complex functions.
6. What is a friend function in C++? [1 Mark]
Answer: A non-member function that has access to the private and protected members of a class.
7. Which keyword is used to grant a non-member function access to a class? [1 Mark]
Answer: The friend keyword.
8. Can a friend function be a member of another class? [1 Mark]
Answer: Yes.
9. Does a friend function use the dot operator to access class members? [1 Mark]
Answer: No, it needs an object of the class to access its members.
10. Where is the friend declaration typically placed? [1 Mark]
Answer: Inside the body of the class definition.
11. Is a friend function subject to access specifiers like private or public? [1 Mark]
Answer: No, the friend declaration can be placed anywhere in the class.
12. Can a friend function be called like a normal function? [1 Mark]
Answer: Yes, it is called without using an object or class scope resolution operator.
13. If a function is defined inside a class, is it automatically inline? [1 Mark]
Answer: Yes, member functions defined inside a class are implicitly inline.
14. To make an outside function inline, where must the definition be provided? [1 Mark]
Answer: The definition must include the inline keyword and appear before the function is called.
15. Are friend functions inherited by derived classes? [1 Mark]
Answer: No, friendship is not inherited.
16. Can a friend function access the private members of multiple classes? [1 Mark]
Answer: Yes, if it is declared as a friend in each of those classes.
17. Does an inline function help in executing large functions faster? [1 Mark]
Answer: No, it often increases code size, which can degrade performance for large functions.
18. Is a friend function considered a member of the class it accesses? [1 Mark]
Answer: No, it is not a member function.
19. What is the main drawback of using excessive inline functions? [1 Mark]
Answer: Code bloat (increase in executable size).
20. Can you declare an inline function inside a header file? [1 Mark]
Answer: Yes, this is a common practice to allow the compiler to see the definition for inlining.
Section B — 2/3 Mark Questions
1. Explain the concept of implicit inlining. [2 Marks]
Answer: Any function defined inside a class declaration is automatically considered an inline function by the C++ compiler. This is called implicit inlining, and the compiler applies the inline optimization to these functions by default.
2. Why is friendship not reciprocal? [2 Marks]
Answer: If class A declares class B as a friend, class B can access class A's private members. However, class A does not automatically gain access to class B's private members unless class B explicitly declares class A as a friend.
3. What are the limitations on the use of inline functions? [3 Marks]
Answer: 1. It increases the binary executable size (code bloat). 2. It may not work for recursive functions. 3. It usually fails for complex functions containing loops, switch statements, or static variables.
4. How can you make an outside function inline? [2 Marks]
Answer: By prefixing the function definition with the inline keyword. The entire definition must be available to the compiler at the point where the function is called, which is usually achieved by placing it in the header file.
5. Differentiate between a member function and a friend function. [3 Marks]
Answer: 1. A member function is part of the class, while a friend function is a non-member. 2. A member function uses the dot operator with an object, whereas a friend function is called like a normal function. 3. A member function can be virtual, but a friend function cannot.
6. State two important properties of friend functions. [2 Marks]
Answer: 1. They are not members of the class. 2. They can access private and protected data of the class only if they are explicitly granted friendship.
7. Explain why the compiler might reject an inline request. [2 Marks]
Answer: The compiler acts as the final judge and will reject the request if the function is too large, contains complex control flow (loops, recursion), or if it determines that the overhead of copying the code is greater than the overhead of a standard function call.
8. Can a friend function be defined inside the class? [2 Marks]
Answer: Yes, a friend function can be defined directly within the class body. When defined inside the class, it is treated as an inline function by default.
9. How does the inline keyword affect function call stack overhead? [3 Marks]
Answer: In a standard call, the program pushes parameters onto the stack, jumps to the function, executes, and returns. By using inline, this overhead is removed because the compiler inserts the code directly into the calling location, eliminating the stack frame management for that call.
10. Is friendship inherited? Provide a short explanation. [2 Marks]
Answer: No, friendship is not inherited. If a base class has a friend function, that function does not automatically become a friend of the derived class; the derived class must grant friendship separately if needed.
Section C — 5 Mark Questions
1. Explain the mechanism and advantages of inline functions in C++. [5 Marks]
Answer:
Mechanism
When a function is marked as inline, the compiler replaces the function call with the actual code of the function definition. This happens during the compilation phase, and the compiler performs this substitution at every point where the function is called, effectively expanding the code.
Advantages
- Performance: It eliminates the time-consuming process of function call overhead, such as parameter passing and stack pointer manipulation.
- Efficiency: It can lead to faster execution for very small functions that are called frequently within a loop.
- Code Clarity: It allows developers to maintain the modularity of functions without sacrificing the performance of macros.
Important Considerations
Inline is a recommendation, not a command. If a function is complex, the compiler will ignore the request to avoid binary code bloat, which could negatively impact the instruction cache performance.
2. Discuss the role of the friend function in object-oriented programming. [5 Marks]
Answer:
Definition
A friend function is a special type of function that is not a member of a class but has full access to its private and protected data members. It bridges the gap between encapsulation and the need for external access.
Key Characteristics
- Granting Access: Access is granted by the class itself using the 'friend' keyword in the class definition.
- Independence: It is not scoped to the class and does not have a 'this' pointer. It must be passed class objects as arguments to interact with them.
- Versatility: A single friend function can be declared in multiple classes, allowing it to act as an operator or a utility that operates on objects of different types.
Application Example
Friend functions are commonly used for operator overloading, such as overloading the extraction (>>) and insertion (<<) operators, where the left operand is not an object of the class.
3. Describe the process of making outside functions inline and the impact of the scope resolution operator. [5 Marks]
Answer:
Process
To make an external function inline, one must use the keyword 'inline' in the function signature. The complete function definition must be placed in the header file rather than a separate source file to ensure the compiler can see the body during compilation.
Scope and Visibility
- Header Files: Since the function code is injected into the caller, the definition must be included in every translation unit where the function is called.
- Linker Errors: If an inline function is defined in multiple files without being declared inline, the linker may throw 'multiple definition' errors. The inline keyword prevents this by allowing the compiler to handle the multiple definitions safely.
- Scope Resolution: When defining a member function outside the class, using the class name followed by the scope resolution operator (::) connects the function to the class structure. However, to make this specific definition inline, the 'inline' keyword must be placed before the return type.
4. Compare and contrast Inline functions and Friend functions in terms of purpose and implementation. [5 Marks]
Answer:
| Feature | Inline Function | Friend Function |
|---|---|---|
| Purpose | Performance optimization | Controlled access to private data |
| Membership | Can be member or non-member | Always a non-member |
| Keyword | inline | friend |
| Implementation | Replaces call with code | Grants access permissions |
| Invocation | Normal function call syntax | Normal function call syntax |
Summary
While both are advanced features, they serve different domains of C++. Inline functions focus on reducing execution time by avoiding call overhead, whereas friend functions focus on breaking encapsulation in a controlled manner to allow external operations to manipulate internal state.
5. Explain why inline functions are not always effective. [5 Marks]
Answer:
The Problem of Code Bloat
The primary disadvantage of excessive inlining is code bloat. If a function contains 10 lines of code and is called in 100 places, the final binary will contain 1000 lines of code instead of a simple jump instruction, significantly increasing the executable size.
Why it Fails Performance
- Instruction Cache: Modern processors rely heavily on the instruction cache. A large executable may cause 'cache misses,' forcing the CPU to fetch code from slower main memory, which is much slower than the original function call overhead.
- Compiler Discretion: Compilers use heuristics to decide when to inline. If a function is too long, uses recursive calls (which cannot be expanded to infinity), or contains static variables (which maintain state across calls), the compiler will safely reject the inline attempt.
- Maintenance: Changing the logic of an inline function defined in a header file requires recompiling every source file that includes that header, which can significantly increase build times in large projects.
Therefore, inlining should only be used for small, performance-critical functions where the call overhead is legitimately the bottleneck.