You are here

C++ inline function program

Inline function: You can make a function inline by writing the keyword inline before defining the function.

C++ programming code

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int check_digit(char);
  6.  
  7. int main() {
  8.   char c;
  9.    
  10.   cout << "Enter a character\n";
  11.   cin >> c;                                
  12.    
  13.   if ( check_digit(c) ) {
  14.     cout << c << " is a digit.\n";
  15.   }
  16.   else {
  17.     cout << c << " isn't a digit.\n";
  18.   }
  19.  
  20.   return 0;
  21. }
  22.  
  23. inline int check_digit(char c) {
  24.   if ( c >= '0' && c <= '9' ) {
  25.     return 1;
  26.   }
  27.   else {
  28.     return 0;
  29.   }
  30. }

C++ programming code for inline function using class

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class programming {
  6.   char c;
  7.  
  8.   public:
  9.     void input();
  10.     void output();
  11. };
  12.  
  13. inline void programming::input() {
  14.   cin >> c;
  15. }
  16.  
  17. void programming::output() {
  18.   cout << c << endl;
  19. }
  20.  
  21. int main() {
  22.   programming object;
  23.  
  24.   object.input();
  25.   object.output();
  26.  
  27.   return 0;
  28. }