CPP Operators: Previous                                                  Next: CPP Classes and Objects

C++ provides following functions:

  1. inline functions
  2. friend functions
  3. function overloading

1. Inline function :

  • This function is basically used to save memory and increase the speed of execution program. If we call a same function 2 times this decrease the execution speed.
  • What inline function does, it simply copies the code where call have been made, so that compiler doesn’t need to jump everytime whenever call is made. This improves speed.
  • inline function is same as normal function except the keyword ‘inline’ which is used before the return datatype of function.
  • Syntax :
 inline (  )
_ _ _ _
                                                inline int sum(int num1,  int num2)
                                                                return (num1+num2);

Drawback of inline function are:

  • It doesn’t work with large instruction function.
  • It doesn’t work with static variables
  • It doesn’t work with recursive function.
2. friend function :
  • friend functions have a rights to access the private data members of the class, although it is not member of that class.
  • friend function can be declared anywhere in the program with keyword ‘friend’.
  • Private data members of a class can be accessed by using dot operator.
  • All friend functions can be placed in a same class that class is known as friend class.
  • Syntax:
friend (argument list);
3. Function overloading :
  • Overloading can be defined as term overloaded functions of a class.
  • Polymorphism contains features of function overloading in which we can make more than one function with same name but they must be different in their signatures.
  • Signature contains :
    • of arguments passed to function
    • Sequence of arguments passed
    • Data type of arguments.
  • For example we have function named “ sum “, so overloading can be possible in following conditions:
    • sum(int a , int b);
    • sum(int a, int b, int c);
    • sum(float a, float b);
    • sum(int a, float b);
    • sum(float a, float b, flaot c);
Polymorphism contains three parts :
    1. function overloading
    2. function overriding
    3. operator overloading

    1. Function Overloading: Described above

    2. function overriding : works in inheritance described later.
    3. Operator overloading :

    • Operators are symbol which gives logical or arithmetic results when applied on operands.
    • Operator overloading provides a facilitly to programmer to change the meaning of operator that are predefined.
    • Every operator that is supported by C++ can be overloaded except the following :
      • Sizeof operator
      • Conditional operator (?:)
      • Scope resolution operator(::)
    • Example of function overloading :
    class f_overloading
                    public :
                                    void sum(int a, int b)
                                                    int total=0;
                                                    cout<<”sum of two numbers is = “<
                                    void sum(int a, int b, int c)
                                                    int total=0;
                                                    cout<<”sum of three numbers is= “<
                    void main( )
                                    f_overloading overload1;
     Example of function overloading

    Default arguments in functions :

    • The number of arguments passed in function definition must be same as number of arguments or values passed to that function when called.
    • For example :
    void sum(int a, int b);
    • When ‘sum’ named function is called then two arguments are need to be given. If one argument is given then this will give an error. To overcome this problem default arguments are used.
    • Default argument contains default value given at function definition. And that argument value need not be given when function is called.
    • For eg:
    int sum(int a, int b, int c)
    When function is called following statements are valid :
    • sum(10,20);
    • sum(10,20,30);
    • in case 1, value of third argument is missing so the compiler will take default value which is 10.
    • In case 2,valuep of c variable will get overridden by 30.
    CPP Operators: Previous                                                  Next: CPP Classes and Objects