Making an outside function inline:
Functions define inside class are by Default always inline (so they also have benefits and drawback of inline functions ) .
But it is good practice to define member function outside class .
So to define inline function outside class you just have to mention inline in its prototype .
- class item{
- public:
- void test(void);
- }
- inline void item::test(){
- func.. def here ..
- }
Related post : CLass and Object in C++ -1
Nesting Of Member Function :-
A Member function can be called by using its name inside another member function of same class.This is known as nesting of member function .
- // Making an outside function inline
- // nesting of member Function
- #include<iostream>
- using namespace std;
- class item{
- int a,b,sum;
- public:
- void getdata(int x,int y);
- void add(void);
- void print(void){ //Bydefault it is inline as define within class
- cout<<" a :- "<<a;
- cout<<" b :- "<<b;
- cout<<" sum :- "<<sum;
- }
- };
- inline void item::getdata(int x,int y){ //mention inline in func prototype
- a=x;
- b=y;
- add(); //calling other member fun ,this is called nesting of func
- }
- void item::add(void){ //it is not inline
- sum=a+b;
- }
- main(){
- item x;
- x.getdata(5,7);
- x.print();
- }
Output :
No comments:
Post a Comment
THANKS FOR UR GREAT COMMENT