inline function in c++ - InTerestinG321

inline function in c++


Inline function:
This is an important feature of c++, this function is commonly used with class, the function is called using a keyword inline.

EX:
#include<iostream>
Using namespace std;
Inline int max(int x,int y)
{
Return a>b?a:b;
}
Int main()
{
Cout<<max(10,10);
Cout<<””<<max(90,30);
Return 0;
}
 
Remember if a function cannot be inlined, that function is called a normal function.

If an inline function is class member function:

#include<iostream>
Using namespace std;
Class myclass
{
Int a,int b;
Public:
Void read(int i,int j);
Void show();
};
 Inline void myclass::read(int I,int j)
{
a=i;
b=j;
}
Inline void myclass::show()
{
Cout<<a<<”\t”<<b<<”\t”;
}
Int main()
{
Myclass x;
x.read(10,20);
x.show();
return 0;
}

Define the inline function within a class:
 When a function is defined inside a class definition it automatically becomes inline function, not necessary to use inline keyword with the declaration.

EX:
#include<iostream>
Using namespace std;
Class myclass
{
int a,b;
public:
void read(int x,int y)
{
a=x;
b=y;
}
Void show()
{
Cout<<a<<”\t”<<b<<”t”;
}
};
Int main()
{
Myclass x;
x.read(20,30);
x.show();
return 0;
}



Class and Object with example
    

Function overloading

General syntax

Friend function 

2 comments: