Tutorial on Inline Implementation in C++

By: Jagan Emailed: 1700 times Printed: 2204 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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. The inline implementation of the GetWeight() function, for example, looks like this:

inline int Cat::GetWeight()
return itsWeight;         // return the Weight data member

You can also put the definition of a function into the declaration of the class, which automatically makes that function inline. For example,

class Cat
int GetWeight() { return itsWeight; }    // inline
void SetWeight(int aWeight);

Note the syntax of the GetWeight() definition. The body of the inline function begins im-mediately after the declaration of the class method; there is no semicolon after the paren-theses. Like any function, the definition begins with an opening brace and ends with a closing brace. As usual, whitespace doesn't matter; you could have written the declaration as

class Cat
int GetWeight()
return itsWeight;
}                           // inline
void SetWeight(int aWeight);

The following two programs re-create the Cat class, but they put the declaration in CAT.HPP and the implementation of the functions in CAT.CPP. The second program below also changes the accessor functions and the Meow() function to inline.

Cat class declaration in CAT.HPP

1:  #include <iostream.h>
2:  class Cat
3:   {
4:  public:
5:   Cat (int initialAge);
6:   ~Cat();
7:    int GetAge() { return itsAge;}             // inline!
8:    void SetAge (int age) { itsAge = age;}     // inline!
9:    void Meow() { cout << "Meow.\n";}             // inline!
10:  private:
11: int itsAge;
12: };

Cat implementation in CAT.CPP.

1:   // Demonstrates inline functions
2:   // and inclusion of header files
4:   #include "cat.hpp"  // be sure to include the header files!
7:   Cat::Cat(int initialAge)   //constructor
8:   {
9:      itsAge = initialAge;
10:  }
12:  Cat::~Cat()             //destructor, takes no action
13:  {
14:  }
16:  // Create a cat, set its age, have it
17:  // meow, tell us its age, then meow again.
18:  int main()
19:  {
20:     Cat Frisky(5);
21:     Frisky.Meow();
22:     cout << "Frisky is a cat who is " ;
23:     cout << Frisky.GetAge() << " years old.\n";
24:     Frisky.Meow();
25:     Frisky.SetAge(7);
26:     cout << "Now Frisky is " ;
27:     cout << Frisky.GetAge() << " years old.\n";
28:      return 0;
29: }

Output: Meow.
Frisky is a cat who is 5 years old.
Now Frisky is 7 years old.

Analysis: Three of the methods are written inline in the declaration file and the declaration has been separated into CAT.HPP.
GetAge() is declared in line 7, and its inline implementation is provided. Lines 8 and 9 provide more inline functions, but the functionality of these functions is unchanged from the previous "outline" implementations.

Line 4 shows #include "cat.hpp", which brings in the listings from CAT.HPP. By including cat.hpp, you have told the precompiler to read cat.hpp into the file as if it had been typed there, starting on line 5.

This technique allows you to put your declarations into a different file from your implementation, yet have that declaration available when the compiler needs it. This is a very common technique in C++ programming. Typically, class declarations are in an .HPP file that is then #included into the associated CPP file.

Lines 18-29 making these functions inline doesn't change their performance.

C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


Be the first one to add a comment

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Jagan
Importing a Certificate for the CA example using keytool in Java
The if-then-else Statement in Java
WHERE Clauses in SQL
The TryCatchFinally Interface in JSP
assert() Function Example program in C
Guidelines for Struts Application Development
Using printf function in C
File Inclusion in C
Using Multiple Message Resource Bundles in Struts
While and For Loops in C
Arrays sample program in C
Controlling Page Navigation in JSF - Static and Dynamic Navigation
Using cout.fill() in C++
Types of Profiles in J2ME
DateField sample program in J2ME

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
Using cout in C++
Demonstration of Prefix and Postfix operators in C++
Passing parameters to a function by value in C++
The Birth, history and need for C++
Tutorial on Classes and Members in C++
Constructors and Destructors in C++
const Member Functions in C++
Where to Put Class Declarations and Method Definitions in C++
Tutorial on Inline Implementation in C++
Classes with Other Classes as Member Data in C++
Looping with the keyword goto in C++
continue and break statements in C++
for Loops in C++
Multiple statements in for loops in C++
Using switch Statements in C++
Most Emailed Articles (in C++)
while (1) Loops in C++
How to handle Exceptions in C++
Character String Literals in C++
Calculating total based on the given quantity and price in C++
A Brief History of C++
Difference between Procedural, Structured, and Object-Oriented Programming
C++ and Object-Oriented Programming
Getting Started with C++
Using cout in C++
Using Comments in a C++ Program
Using functions in C++
How to use Enumerated constants in C++
Demonstration of Prefix and Postfix operators in C++
The if Statement in C++
Advanced if Statements in C++