Click here to Skip to main content
15,886,676 members
Articles / Programming Languages / C++
Article

WWWH (What When Why & How) on C++

Rate me:
Please Sign up or sign in to vote.
3.10/5 (18 votes)
26 Feb 2004CPOL3 min read 116.1K   1K   27   13
This article contains most of the questions/answers for various concepts of C++ such as Constructors, Destructors, Virtual Functions & Destructors, Operator overloading and many more

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);

//
// Example to define a function as inline        
       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.

//  For example
    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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
Dinanath has total IT experience 9+ years

Comments and Discussions

 
Generala fine article Pin
avenger_sb2527-May-04 22:54
avenger_sb2527-May-04 22:54 
GeneralCopy Constructors Pin
S. Senthil Kumar5-Mar-04 22:36
S. Senthil Kumar5-Mar-04 22:36 
GeneralRe: Copy Constructors Pin
bhalchan26-Jul-05 7:45
bhalchan26-Jul-05 7:45 
GeneralRe: Copy Constructors Pin
S. Senthil Kumar26-Jul-05 9:00
S. Senthil Kumar26-Jul-05 9:00 
QuestionWhy should you prefer ++i to i++? Pin
John R. Shaw3-Mar-04 9:34
John R. Shaw3-Mar-04 9:34 
Generalvery concise and useful article Pin
rockonedge1-Mar-04 19:32
rockonedge1-Mar-04 19:32 
GeneralYou can have inline virtuals Pin
Jason.King.Work@gmail.com27-Feb-04 7:35
Jason.King.Work@gmail.com27-Feb-04 7:35 
GeneralRe: You can have inline virtuals Pin
PrabhaGovind5-Jul-07 20:19
PrabhaGovind5-Jul-07 20:19 
for Q6 your answer was 'No, you cannot define the inline functions as virtual functions.'.....which is wrong....check my article on Virtual inline functions..
http://prabhagovind.wordpress.com/2007/07/05/virtual-inline-functions/[^]

Prabha Govind
If dirt were dollars, I wouldn't worry anymore

GeneralMisleading title Pin
KevinHall27-Feb-04 5:27
KevinHall27-Feb-04 5:27 
GeneralRe: Misleading title Pin
KevinHall27-Feb-04 5:35
KevinHall27-Feb-04 5:35 
GeneralRe: Misleading title Pin
Dinanath28-Feb-04 19:08
Dinanath28-Feb-04 19:08 
Generalinline is not necessarily inline Pin
Martyn Pearson26-Feb-04 23:00
Martyn Pearson26-Feb-04 23:00 
GeneralRe: inline is not necessarily inline Pin
Dinanath28-Feb-04 19:24
Dinanath28-Feb-04 19:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.