Introduction
This
article contains question on C++ concepts based on WWWH format. When there are more
lots things (say in life) or in Studies then we have to break it into small
things, and here I had applied the technique called as basically WWWH Funda which is
nothing but divide and rule.
Background
Readers should be familiar with basic concepts of object oriented
programming.
Using the code
For simplicity we consider one example for generating the questions on inline
function & macro using WWWH technique. Using this technique we may generate
more questions to learn & understand the concept of inline function &
macro.
Following are the questions
1. What is an inline function?
An inline function is a function whose code gets substituted in lieu of the
actual call to the function.
2. What is the purpose of inline functions?
In some cases, inline functions make a compute-bound application run faster.
In a broad sense, the idea behind inline functions is to insert the code of a
called function at the point where the function is called.
3. Are there any special rules about inlining?
Yes, here are a few rules about inlining.
- Any source file that contains usage of an inline function must contain the
function's definition.
- An inline function must be identically defined everywhere. The easy way to
do this is to define it once, preferably in the class header file, and include
the definition as needed.
main()
cannot be inline.
4. How to define a function as "inline"?
Following is syntax to define a function as inline -
inline return-type function-name(arguments);
inline int MyFunction();
5. How to make a function as a default inline function without specifying
inline keyword?
Simple, just define that function inside the class definition.
class Demo {
public:
int num;
intMyNumber() {
return num; }
6. Does inline functions are virtual functions?
No, you cannot define the
inline functions as virtual functions.
7. Why inline functions are not virtual functions?
Inline functions are not
virtual functions because they are bound at the compile time, where as virtual
functions are bound at run-time.
8. Which functions are treated as inline function?
Compiler-generated
default constructor, copy constructors, destructors, and assignment operators
are treated as inline.
9. How the inline functions can speed up the processing
Inline functions can speed up processing in 3 ways -
- Eliminating the overhead associated with the function call instruction.
- Eliminating the overhead associated with the pushing and popping parameters.
- Allowing the compiler to optimize the code outside the function call more
efficiently.
10. Which are the situations where inline function is not working?
Some
situations where inline function may not work -
- For
function returning values, if a loop, a switch or goto exists.
- for function not returning values, if a return statement exists.
- if functions contains static variables.
- if inline
functions are recursive.
- if function contains large
code.
- if function is virtual as binding takes place at
compile time.
11. What is a macro?
A macro is a name that will be substituted by a specified text wherever the
name appears in the source file. this replacement is done by the
pre-processor.
12. What is the disadvantages of macro over inline function?
- Macro is not really a function and so usual error
checking does not occur during compilation.
- Macro is expanded by preprocessor while inline function
is parsed by compiler.
- Expressions passed as argument to inline functions are
evaluated once. In some cases, expressions passed as arguments to
macros can be evaluated more than once.
13. What are the side effects of using macro instead of inline function?
The use of macro involves literal substitution of the macro-name and its
parameters by the macro-body and arguments. when a macro is used in an
arithmetic expression, unexpected results may be produced due to operator
precedence.
Following code show the side effects of macros
#include <stdio.h>
#define SQUARE(x) x * x
#define SQR(x) ((x) * (x))
void main(void) {
int num1 = 10, num2 = 2;
int wrongsqr, rightsqr;
wrongsqr = SQUARE(num1 + num2);
rightsqr = SQR(num1 + num2);
printf(" Wrong SQR = %d \n", wrongsqr);
printf(" Right SQR = %d \n", rightsqr);
}
The output is as follows -
Wrong SQR = 32
Right SQR = 144
This is what I developed such questions on inline function & macro, you
can generate more questions based on this question set.
References used for this articles
- Object Oriented Programming with C++ - By E. Balagurusamy
- C++ FAQ (2nd Edition) - Marshall Cline