Dec 3, 2011

Inline function example, a sample program c++

Inline function examples :-
recommended article :- Inline Function

Lets look out one of the simple example :-
-------------------------------------------------------------------
// inline_functions_inline.cpp
#include 
#include 

inline char toupper( char a ) {
   return ((a >= 'a' && a <= 'z') ? a-('a'-'A') : a );
}

int main() {
   printf_s("Enter a character: ");
   char ch = toupper( getc(stdin) );
   printf_s( "%c", ch );
}
-------------------------------------------------------------------
Now  moving to example of using inline function inside a class :-
-------------------------------------------------------------------
// Inline_Member Function example
class CBankAccount
{
public:
    CBankAccount(double initial_balance) { balance = initial_balance; }
    double GetBalance();
    double Deposit( double Amount );
    double Withdraw( double Amount );
private:
    double balance;
};

inline double CBankAccount::GetBalance()
{
    return balance;
}

inline double CBankAccount::Deposit( double Amount )
{
    return ( balance += Amount );
}

inline double CBankAccount::Withdraw( double Amount )
{
    return ( balance -= Amount );
}

int main()
{
 CBankAccount objAccount;
 objAccount.Deposit(1000);
}
-------------------------------------------------------------------
Just as you can ask the compiler to make a regular function inline, you can make class methods inline. The keyword inline appears before the return value. In the class declaration, the functions were declared without the inline keyword. The inline keyword can be specified in the class declaration; the result is the same.
 Inline functions are best used for small functions such as accessing private data members. The main purpose of these one- or two-line "accessor" functions is to return state information about objects; short functions are sensitive to the overhead of function calls. Longer functions spend proportionately less time in the calling/returning sequence and benefit less from inlining.
 Read More :- Guidelines for using Inline function.

Check this out
----------------------------------------------------
harvard crimson tickets
hofstra pride tickets
houston baptist huskies tickets
----------------------------------------------------



2 comments:

The inline keyword can be specified in the class declaration; the result is the same.

Post a Comment