Member function

It is a function which has its definition or prototype within the class definition like any other variable. 

2 ways to define a member function:

i. Inside the class definition-  Member funtion can be defined directly.

ii. Outside the class definition- Here, we use the scope resolution :: operator along with class name and function name.

Example:

Note: We use cpp editor, and cpp editor pad do not uses .h extension in the header file and it produces an error. This is why I don't use .h in the programs
 plzz you must include .h in your program header file( if you use another editor ).

//C++ program to define member function

#include<iostream>

using namespace std;


class smash

{

   public:

       string name;

       string field;

   void printname();


   void printfield()    //defining member function inside class definition

   {

     cout<< "Field of interest is "<<field;

   }

};


void smash::printname()  //defining member function outside class definition using scope resolution operator ::

{

  cout<<"Smasher name is "<<name;

}

//main function

int main()

{

  smash r1;

  r1.name = "Dip";

  r1.field = "Coding";

  r1.printname();  //call printname

  cout<<endl;    

  r1.printfield();  //call printfield

  

  return 0;

}

OUTPUT:



  

What is inline function?

Inline function is used in order to eliminate the time of calls to a function. An inline function is a function that is expanded in line when it is invoked thus saving time. The compiler replaces the function call with the corresponding function code that reduces the overhead of function calls.

Please Note: Inlining is only a request to the compiler, not a command. The compiler may not perform inlining if:

i. A function contains any loop, static variables, a switch command or goto statement,

ii. A function is recursive.

iii. For a function not returning values, if a return statement exists. 

Syntax:

inline return_type function_name(parameters)

{

  function-body

}

Example:

  //a simple program to demonstrate the use of inline function.

#include <iostream> 

using namespace std; 

inline int area(int l, int b) 

  return l*b; 

int main() 

{  

  cout << "The area of rectangle is: " << area(13, 14) << "\n"; 

  return 0; 

OUTPUT:



Inline  function and classes:

All the functions, defined inside the class, are implicitly inline. If you want to explicitly declare inline function in the class then just declare the function inside the class and define it outside the class using inline prefix. 

Example: 

//program to demonstrate Inline function using classes

#include <iostream> 

using namespace std; 

class college 

  public: 

    string name, principal;

    void printprincipal();  //function declaration

    void printname() 

  { 

     cout << "Name of the Institution: " << name; 

  } 

}; 

inline void college::printprincipal()   //use inline prefix

  cout << "Name of the Principal: " << principal; 

int main() 

  college c; 

  c.name = "Government Polytechnic, Khutri"; 

  c.principal="Dr. Umesh Kumar"; 

  c.printname(); 

  cout << endl; 

  c.printprincipal(); 

  return 0; 


OUTPUT:


Merit: 

1. It quicken the program by avoiding function calling overhead.

2. It save overhead of return call from a function and also the overhead of puch/pop variables on the stack when function is called(

3. It increases locality of reference by utilizing instruction cache.


Demerit: 

1. It may cause cache miss.

2. When used in a header, it makes your header file larger with information (which users don’t care.)

3. May cause thrashing in memory (leads to performance degradation).

4. Sometimes not useful for example in embedded system where large executable size is not preferred at all due to     memory constraints.


Friend function in OOPs

A friend function is a feature in OOPs which allows us to access private and protected data of a class.

Characteristics of friend function:-

 i. It is not in the scope of class and hence, can't be called by objects of the class.

ii. It should be declared inside the class using 'friend' keyword. However, it can be defined anywhere in      the program but the 'friend' keyword  must not be used.

iii. It can be called like a normal function without using the object. It has class objects as arguments.

iv. It cannot access the member names directly and has to use an object name and dot membership               operator with the member name.

v.  It can be declared either in the private or the public part.

Syntax:

class class_name    

{    

    friend data_type function_name(argument/s);           

};   

Example 1

// C++ program to demonstrate the working of friend function

#include <iostream>    

using namespace std;    

class water    

{    

    private:    

        int vol;    

    public:          

        friend int printVol(water);  //friend function    

};    

//friend function definition

int printVol(water w)    

{    

   w.vol = 5;    

    return w.vol;    

}    

int main()    

{    

    water w;    

    cout<<"Volume of water: "<< printVol(w)<<endl;    

    return 0;    

}    

OUTPUT:


Now, let's learn a more meaningful use of friend function by operating on objects of two different classes.


Example 2

  //program to multiply members of two different classes using friend function

#include <iostream>

using namespace std;

class class2;

class class1 

{  

    private:

    int num1;

    public:

    friend int mul(class1, class2);         // friend function declaration

};

class class2

{

     private:

     int num2;

     public:

     friend int mul(class1, class2);        // friend function declaration

};

// access members of both classes

int mul(class1 obj1, class2 obj2) 

{

    obj1.num1=3;

    obj2.num2=4;

    return (obj1.num1 * obj2.num2);

}

int main()

{

    class1 obj1;

    class2 obj2;

    cout << "Product of the two no.s: " << mul(obj1, obj2);

    return 0;

}


OUTPUT:



Example 3

//A simple and complete C++ program to demonstrate global friend

#include <iostream>

using namespace std;

class abc 

{

  private:

    char ch='R';

    int num=11;

  public:

    friend void disp(abc obj);

};

//Global Function

void disp(abc obj)

{

   cout<<obj.ch<<endl; 

   cout<<obj.num<<endl;

}

int main() 

{

   abc obj;

   disp(obj);

   return 0;

}


OUTPUT:

A global friend function allows you to access all the private and protected members of the class, unlike a normal friend function that allows you to access only specific members of the class.


Friend Class :

A friend class is a class that can access the private and protected members of a class where it is declared as friend. It is required we want a particular class to access the private and protected members of another class.

Syntax:

 class A

{

    friend class B;  //declaration of B as a friend class of A

};

   class B //definition of friend class B

{

  .....

};

Example

//C++ program tp demonstrate friend class

include <iostream>

using namespace std;

class cube

{

friend class cuboid;      // declaring cuboid as friend class

int side;

public:

cube ( int a )

{

side = a;

}

};

class cuboid

{

int length;

int breadth;

int height;

public:

int getVolume()

{

return length * breadth * height;

}

void shape( cube s )

{

length = s.side;

breadth = s.side;

height = s.side;

}

};

int main()

{

cube Cube(4);

cuboid Cuboid;

Cuboid.shape(Cube);

cout << Cuboid.getVolume() << endl;

return 0;

}


OUTPUT:


Here cube is a friend class, we can access all members of class cuboid from inside cube. However, we cannot access members of class cube from inside cuboid. It is because friend relation in OOPs (C++) is only granted, not taken....

  Array in oops for go to NEXT