Saturday 26 July 2014

CLass and Object in C++ -2

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 .
  1.         class item{
  2.                public:
  3.                   void test(void);
  4.               }
  5. inline void item::test(){
  6.          func.. def here ..
  7. }          

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 .

  1. // Making an  outside function inline
  2. // nesting of member Function
  3. #include<iostream>
  4. using namespace std;
  5. class item{
  6.         int a,b,sum;
  7.         public:
  8.                 void getdata(int x,int y);
  9.                 void add(void);
  10.                 void print(void){ //Bydefault it is inline as define within class
  11.                cout<<" a :- "<<a;
  12.                cout<<" b :- "<<b;
  13.                cout<<" sum :- "<<sum;
  14.         }
  15. };
  16. inline void item::getdata(int x,int y){  //mention inline in func prototype
  17.         a=x;
  18.         b=y;
  19.         add(); //calling other member fun ,this is called nesting of func
  20. }
  21. void item::add(void){ //it is not inline
  22.   sum=a+b;     
  23. }
  24. main(){
  25.         item x;
  26.         x.getdata(5,7);
  27.         x.print(); 
  28. }

Output :
nesting of member function in c++ class

No comments:

Post a Comment

THANKS FOR UR GREAT COMMENT

Blogger Widgets