CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C++ DETAILS
Question :
I want a program on INLINE function and demonstrate the program with INLINE function and without INLINE function.Because i want to know clearly the difference between INLINE FUNCTION AND FUNCTION

Posted by: satish on 10/16/2007

Contact satish  Contact satish
Category C++ Interview Questions
Rating (4.0) By 3 users
Added on 10/16/2007
Views 2944
Rate it!
Answers:

Inline functions get copied to every locations that make a call to the function. That is the entire function body (all statements) are inlined into source code at every calling location.

Inline functions should only be used if the body of the function is only a few lines of code.



 Posted by: Shellman2000    

Contact Shellman2000  Contact Shellman2000

In C++ the inlines are like macros. they also do Code Expansion.

There are two problems with the use of preprocessor macros in
C++.
The first is:
a macro looks like a function call,but doesn?t always act like one. This can bury difficult-to-find bugs.
The second is :
The preprocessor has no
permission to access class member data. This means preprocessormacros cannot be used as class member functions.


C++implements the macro as inline function, which is a true function in
every sense. Any behavior you expect from an ordinary function,
you get from an inline function. The only difference is that an inline
function is expanded in place, like a preprocessor macro, so the
overhead of the function call is eliminated.

#include<iostream>
using namespace std;
#define Add(x,y) {x+y;}
inline int add(int,int){ int c=x+y; return c;}

int main()
{
int x=4,y=6;
cout<<"This is macro"<<Add(10,12);
cout<<"This is inline"<<add(10,12);
cout<<"This is macro"<<Add(x++,++y);
cout<<"This is inline"<<Add(x++,++y);
return 0;
}
try it and test the result.
if you are running this in linux.Then check after preprocessing.
use the command
CC -E filename.c




 Posted by: Sree    

Contact Sree  Contact Sree

In C++ the inlines are like macros. they also do Code Expansion.

There are two problems with the use of preprocessor macros in
C++.
The first is:
a macro looks like a function call,but doesn?t always act like one. This can bury difficult-to-find bugs.
The second is :
The preprocessor has no
permission to access class member data. This means preprocessormacros cannot be used as class member functions.


C++implements the macro as inline function, which is a true function in
every sense. Any behavior you expect from an ordinary function,
you get from an inline function. The only difference is that an inline
function is expanded in place, like a preprocessor macro, so the
overhead of the function call is eliminated.

#include<iostream>
using namespace std;
#define Add(x,y) {x+y;}
inline int add(int,int){ int c=x+y; return c;}

int main()
{
int x=4,y=6;
cout<<"This is macro"<<Add(10,12);
cout<<"This is inline"<<add(10,12);
cout<<"This is macro"<<Add(x++,++y);
cout<<"This is inline"<<Add(x++,++y);
return 0;
}
try it and test the result.
if you are running this in linux.Then check after preprocessing.
use the command
CC -E filename.c




 Posted by: Sree    

Contact Sree  Contact Sree

In C++ the inlines are like macros. they also do Code Expansion.

There are two problems with the use of preprocessor macros in
C++.
The first is:
a macro looks like a function call,but doesn?t always act like one. This can bury difficult-to-find bugs.
The second is :
The preprocessor has no
permission to access class member data. This means preprocessormacros cannot be used as class member functions.


C++implements the macro as inline function, which is a true function in
every sense. Any behavior you expect from an ordinary function,
you get from an inline function. The only difference is that an inline
function is expanded in place, like a preprocessor macro, so the
overhead of the function call is eliminated.

#include<iostream>
using namespace std;
#define Add(x,y) {x+y;}
inline int add(int,int){ int c=x+y; return c;}

int main()
{
int x=4,y=6;
cout<<"This is macro"<<Add(10,12);
cout<<"This is inline"<<add(10,12);
cout<<"This is macro"<<Add(x++,++y);
cout<<"This is inline"<<Add(x++,++y);
return 0;
}
try it and test the result.
if you are running this in linux.Then check after preprocessing.
use the command
CC -E filename.c




 Posted by: Sree    

Contact Sree  Contact Sree

the inline function is not command like a
function.It is just like a request to decrease the comprehensibility of the code.
the syntax is:
inline function-header name
{
arguments
}



 Posted by: ramesh    

Contact ramesh  Contact ramesh



#include<iostream.h>
class Demo
{
public:
Demo(){}

void display()
{
cout<<"This is an inline function"<<endl;
}
};
void display()
{
cout<<"This is a regular function"<<endl;
}



void main()
{
display();
Demo().display();
}

observe that a function is inline if it is a member function to a class and is written within the class.If a function isn't a member of a class it is said to be a regular function.
In the above program,"Demo().display()" calls the inline display() function and "display()" calls the regular display() function.



 Posted by: sai santhosh.M    

Contact sai santhosh.M  Contact sai santhosh.M

Difference between INLINE function
and a normal function is we can find it in execution of a program containing a normal fn and INLINE fn .when we execute this pgm when you call a inline function the entire code written in the function will be substituted there..where as when you call a normal function the control jumps
to the line of that function and it executes.
NOTE:the code of the INLINE function should be very small
SYNTAX:
inline return type fn()
{
statments

}
void main()
{
fn();
}



 Posted by: kishore reddy    

Contact kishore reddy  Contact kishore reddy


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
What is a friend function & its advantage?
View Answer
Where we use reference and where we use pointers ?Write their differences and advantages and disadvantages .
View Answer
what are the disadvantages of copy constructor??
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C++ Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/12309/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.97