C++ Laguage



Admission Enquiry Form

  

Inline Function in C++




Function Defintion Outside the Class and Use of Inline Keyword in C++:

Defining function outside the class with scope resolution(::) operator.

#include<iostream>
using namespace std;
class Maths
{
int num1,num2,res;
public:
void input();
void sum();
};
void Maths::input()
{
cout<<"Enter first number ";
cin>>num1;
cout<<"Enter second number ";
cin>>num2;
}
void Maths::sum()
{
res=num1+num2;
cout<<"\nResult is "<<res;
}
int main()
{
Maths mt;
mt.input();
mt.sum();
}


Output:

Enter first number 100
Enter second number 200

Result is 300




Defining function outside the class with inline keyword.

#include<iostream>
using namespace std;
class Demo
{
int num;
public:
void show();
};
inline void Demo::show()
{
num=100;
cout<<"Show function definition outside the class "<<num;
}

int main()
{
Demo dm;
dm.show();
}


Output:

Show function definition outside the class 100