what is inline function

Upload: ravi-sun

Post on 09-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 What is Inline Function

    1/22

    What is Inline Function?

    Inline functions are functions where the call is made to inline functions. The actual code

    then gets placed in the calling program.

    Reason for the need of Inline Function:

    Normally, a function call transfers the control from the calling program to the function

    and after the execution of the program returns the control back to the calling program

    after the function call. These concepts of function saved program space and memoryspace are used because the function is stored only in one place and is only executed when

    it is called. This concept of function execution may be time consuming since the registers

    and other processes must be saved before the function gets called.

    The extra time needed and the process of saving is valid for larger functions. If thefunction is short, the programmer may wish to place the code of the function in the

    calling program in order for it to be executed. This type of function is best handled by the

    inline function. In this situation, the programmer may be wondering why not write theshort code repeatedly inside the program wherever needed instead of going for inline

    function? Although this could accomplish the task, the problem lies in the loss of clarity

    of the program. If the programmer repeats the same code many times, there will be a lossof clarity in the program. The alternative approach is to allow inline functions to achieve

    the same purpose, with the concept of functions.

    What happens when an inline function is written?

    The inline function takes the format as a normal function but when it is compiled it iscompiled as inline code. The function is placed separately as inline function, thus adding

    readability to the source program. When the program is compiled, the code present in

    function body is replaced in the place of function call.

    General Format of inline Function:

    The general format of inline function is as follows:

    inline datatype function_name(arguments)

  • 8/8/2019 What is Inline Function

    2/22

    The keyword inline specified in the above example, designates the function as inline

    function. For example, if a programmer wishes to have a function named exforsys withreturn value as integer and with no arguments as inline it is written as follows:

    inline int exforsys( )

    Example:

    The concept of inline functions:

    #include int exforsys(int);void main( ){int x;cout >x;cout

  • 8/8/2019 What is Inline Function

    3/22

    The output would be the same even when the inline function is written solely as a

    function. The concept, however, is different. When the program is compiled, the code

    present in the inline function exforsys( ) is replaced in the place of function call in thecalling program. The concept of inline function is used in this example because the

    function is a small line of code.

    The above example, when compiled, would have the structure as follows:

    #include int exforsys(int);void main( ){int x;cout >x;

    //The exforsys(x) gets replaced with code return 5*x1;cout

  • 8/8/2019 What is Inline Function

    4/22

    A friend function is used for accessing the non-public members of a class. A class can

    allow non-member functions and other classes to access its own private data, by making

    them friends. Thus, a friend function is an ordinary function or a member of anotherclass.

    How to define and use Friend Function in C++:

    The friend function is written as any other normal function, except the function

    declaration of these functions is preceded with the keyword friend. The friend functionmust have the class to which it is declared as friend passed to it in argument.

    Some important points to note while using friend functions in C++:

    The keyword friend is placed only in the function declaration ofthe friend function and not in the function definition..

    It is possible to declare a function as friend in any number ofclasses..

    When a class is declared as a friend, the friend class has accessto the private data of the class that made this a friend..

    A friend function, even though it is not a member function, wouldhave the rights to access the private members of the class..

    It is possible to declare the friend function as either privateor public..

    The function can be invoked without the use of an object. Thefriend function has its argument as objects, seen in examplebelow.

    Example to understand the friend function:

    #include

    class exforsys{private:int a,b;public:void test(){a=100;b=200;}

  • 8/8/2019 What is Inline Function

    5/22

    friend int compute(exforsys e1)

    //Friend Function Declaration with keyword friend and with the object of class exforsys towhich it is friend passed to it};

    int compute(exforsys e1){

    //Friend Function Definition which has access to private datareturn int(e1.a+e2.b)-5;}

    main(){exforsys e;e.test();cout

  • 8/8/2019 What is Inline Function

    6/22

    static return_data_type fucntionname()

    //Static function defined with keyword static{statement1;

    //Statements for execution inside static functionstatement2;....................}

    For example if a function exforsys returning nothing is to bedeclared as staic function it is done as follows:

    static void exforsys(){

    ........;

    .......;}

    Accessing Static Function:

    A normal member function is accessed using the object and an operator called the dot

    member access operator. The functions declared static or static functions are accessedusing only the class name and the scope resolution operator, unlike in normal member

    functions where these are not used.

    Example:

    The declaration of static member function and how to access static member function:

    #include class example{

    private:static int sum; //Static dataint x;public:

    example() //Constructor of the class{sum=sum+1;x=sum;}

  • 8/8/2019 What is Inline Function

    7/22

    ~example() //Destructor of the class{sum=sum-1;}

    static void exforsys()

    //Static function exforsys( ) defined with keyword static{cout

  • 8/8/2019 What is Inline Function

    8/22

    In the above example, the function exforsys() is defined as static function and the

    integer data type sum is declared as static data type. Four objects e1, e2, e3 and e4 are

    created for the class example. The constructor of the class example increments the sumby 1 and the destructor of the class decrements sum by 1.

    The static function is accessed using the class name example and the scope resolution

    operator:: as

    example::exforsys();

    But the normal member function number() is accessed using the object name and the dot

    member access operator as

    e1.number()e2.number()

    e3.number()e4.number()

    The first time the static function exforsys() is called, there was one object created and

    thus, the sum is incremented by 1 in the constructor printing the result of sum as 1.When

    the static function exforsys() is called the second time, there were three more objectse2,e3 and e4 created which results in the sum incremented thrice from 1 in the

    constructor of the corresponding class example, resulting in the value of sum as 4, which

    is displayed in the second result. Applying the above explanation, it is clear that the static

    function operates on the class and not in object. To access static function the programmercan use the class name, followed by the scope resolution operator, as seen in example

    above.

    What is Pure Virtual Function:

    Pure Virtual Function is a Virtual function with no body.

  • 8/8/2019 What is Inline Function

    9/22

    Declaration of Pure Virtual Function:

    Since pure virtual function has no body, the programmer must add the notation =0 for

    declaration of the pure virtual function in the base class.

    General Syntax of Pure Virtual Function takes the form:

    class classname //This denotes the base class of C++ virtual function{public:

    virtual void virtualfunctioname() = 0 //This denotes the pure virtual function in

    C++};

    The other concept of pure virtual function remains the same as described in the previous

    section of virtual function.

    To understand the declaration and usage of Pure Virtual Function, refer to this example:

    class Exforsys{public:

    virtual void example()=0; //Denotes pure virtual Function Definition};

    class Exf1:public Exforsys{public:void example()

    {cout

  • 8/8/2019 What is Inline Function

    10/22

  • 8/8/2019 What is Inline Function

    11/22

    In the above example, there are two derived classes Exf1 and Exf2 from the base classExforsys. As shown in the above diagram, the Training class is derived from both of the

    derived classes Exf1 and Exf2. In this scenario, if a user has a member function in the

    class Training where the user wants to access the data or member functions of the classExforsys it would result in error if it is performed like this:

    class Exforsys{protected:int x;};

    class Exf1:public Exforsys

    { };

    class Exf2:public Exforsys{ };

    class Training:public Exf1,public Exf2{public:int example(){return x;}};

    The above program results in a compile time error as the member function example() ofclass Training tries to access member data x of class Exforsys. This results in an error

    because the derived classes Exf1 and Exf2 (derived from base class Exforsys) create

    copies of Exforsys called subobjects.

  • 8/8/2019 What is Inline Function

    12/22

    This means that each of the subobjects have Exforsys member data and member

    functions and each have one copy of member data x. When the member function of theclass Training tries to access member data x, confusion arises as to which of the two

    copies it must access since it derived from both derived classes, resulting in a compiletime error.

    When this occurs, Virtual base class is used. Both of the derived classes Exf1 and Exf2are created as virtual base classes, meaning they should share a common subobject in

    their base class.

    For Example:

    class Exforsys{protected:int x;;

    class Exf1:virtual public Exforsys{ };

    class Exf2:virtual public Exforsys{ };

    class Training:public Exf1,public Exf2{public:int example(){return x;}};

    C++ Polymorphism

    Introduction

    Polymorphism is the ability to use an operator or function in different ways.

    Polymorphism gives different meanings or functions to the operators or functions. Poly,

    referring to many, signifies the many uses of these operators and functions. A single

  • 8/8/2019 What is Inline Function

    13/22

    function usage or an operator functioning in many ways can be called polymorphism.

    Polymorphism refers to codes, operations or objects that behave differently in different

    contexts.

    Below is a simple example of the above concept of polymorphism:

    6 + 10

    The above refers to integer addition.

    The same + operator can be used with different meanings with strings:

    "Exforsys" + "Training"

    The same + operator can also be used for floating point addition:

    7.15 + 3.78

    Polymorphism is a powerful feature of the object oriented programming language C++. Asingle operator + behaves differently in different contexts such as integer, float or strings

    referring the concept ofpolymorphism. The above concept leads to operatoroverloading.

    The concept of overloading is also a branch ofpolymorphism. When the exiting operatoror function operates on new data type it is overloaded. This feature of polymorphism

    leads to the concept ofvirtual methods.

    Polymorphism refers to the ability to call different functions by using only one type offunction call. Suppose a programmer wants to code vehicles of different shapes such as

    circles, squares, rectangles, etc. One way to define each of these classes is to have a

    member function for each that makes vehicles of each shape. Another convenient

    approach the programmer can take is to define a base class named Shape and then createan instance of that class. The programmer can have array that hold pointers to all

  • 8/8/2019 What is Inline Function

    14/22

    different objects of the vehicle followed by a simple loop structure to make the vehicle,

    as per the shape desired, by inserting pointers into the defined array. This approach leads

    to different functions executed by the same function call. Polymorphism is used to givedifferent meanings to the same concept. This is the basis forVirtual function

    implementation.

    In polymorphism, a single function or an operator functioning in many ways dependsupon the usage to function properly. In order for this to occur, the following conditions

    must apply:

    All different classes must be derived from a single base class. In the above

    example, the shapes of vehicles (circle, triangle, rectangle) are from the single

    base class called Shape. The member function must be declared virtual in the base class. In the above

    example, the member function for making the vehicle should be made as virtual tothe base class.

    Features and Advantages of the concept of Polymorphism:

    Applications are Easily Extendable:

    Once an application is written using the concept of polymorphism, it can easily be

    extended, providing new objects that conform to the original interface. It is unnecessary

    to recompile original programs by adding new types. Only re-linking is necessary toexhibit the new changes along with the old application. This is the greatest achievement

    of C++ object-oriented programming. In programming language, there has always been aneed for adding and customizing. By utilizing the concept of polymorphism, time and

    work effort is reduced in addition to making future maintenance easier.

    Helps in reusability of code.

    Provides easier maintenance of applications. Helps in achieving robustness in applications.

    Types of Polymorphism:

    C++ provides three different types of polymorphism.

    Virtual functions

  • 8/8/2019 What is Inline Function

    15/22

  • 8/8/2019 What is Inline Function

    16/22

    Encapsulation is the process of combining data and functions into a single unit called

    class. Using the method of encapsulation, the programmer cannot directly access the data.

    Data is only accessible through the functions present inside the class. Data encapsulationled to the important concept of data hiding. Data hiding is the implementation details of a

    class that are hidden from the user. The concept of restricted access led programmers to

    write specialized functions or methods for performing the operations on hidden membersof the class. Attention must be paid to ensure that the class is designed properly.

    Neither too much access nor too much control must be placed on the operations in order

    to make the class user friendly. Hiding the implementation details and providing

    restrictive access leads to the concept of abstract data type. Encapsulation leads to theconcept of data hiding, but the concept of encapsulation must not be restricted to

    information hiding. Encapsulation clearly represents the ability to bundle related data and

    functionality within a single, autonomous entity called a class.

    For instance:

    class Exforsys{public:int sample();int example(char *se)int endfunc();.........

    ......... //Other member functions

    private:int x;float sq;..........

    ......... //Other data members};

    In the above example, the data members integer x, float sq and other data members and

    member functions sample(),example(char* se),endfunc() and other member functions arebundled and put inside a single autonomous entity called class Exforsys. This exemplifies

    the concept of Encapsulation. This special feature is available in object-oriented languageC++ but not available in procedural language C. There are advantages of using thisencapsulated approach in C++. One advantage is that it reduces human errors. The data

    and functions bundled inside the class take total control of maintenance and thus human

    errors are reduced. It is clear from the above example that the encapsulated objects act as

    a black box for other parts of the program through interaction. Although encapsulatedobjects provide functionality, the calling objects will not know the implementation

    details. This enhances the security of the application.

  • 8/8/2019 What is Inline Function

    17/22

    The key strength behind Data Encapsulation in C++ is that the keywords or the access

    specifiers can be placed in the class declaration as public, protected or private. A classplaced after the keyword public is accessible to all the users of the class. The elements

    placed after the keyword private are accessible only to the methods of the class. Inbetween the public and the private access specifiers, there exists the protected access

    specifier. Elements placed after the keyword protected are accessible only to the methodsof the class or classes derived from that class.

    The concept of encapsulation shows that a non-member function cannot access an

    object's private or protected data. This adds security, but in some cases the programmermight require an unrelated function to operate on an object of two different classes. The

    programmer is then able to utilize the concept of friend functions. Encapsulation alone is

    a powerful feature that leads to information hiding, abstract data type and friendfunctions.

    Features and Advantages of the concept of Encapsulation:

    * Makes Maintenance of Application Easier:

    Complex and critical applications are difficult to maintain. The cost associated withmaintaining the application is higher than that of developing the application properly. Toresolve this maintenance difficulty, the object-oriented programming language C++

    created the concept of encapsulation which bundles data and related functions together as

    a unit called class. Thus, making maintenance much easier on the class level.

    * Improves the Understandability of the Application

    * Enhanced Security:

    There are numerous reasons for the enhancement of security using the concept ofEncapsulation in C++. The access specifier acts as the key strength behind the concept of

    security and provides access to members of class as needed by users. This prevents

  • 8/8/2019 What is Inline Function

    18/22

    unauthorized access. If an application needs to be extended or customized in later stages

    of development, the task of adding new functions becomes easier without breaking

    existing code or applications, there by giving an additional security to existingapplication.

    C++ Operator Overloading Part II

    Operator overloading is a very important aspect of object-oriented programming. Binary

    operators can be overloaded in a similar manner as unary operators. In this C++ tutorial,you will learn about Binary Operating Overloading explained along with syntax and

    example.

    Operator Overloading Binary Operators

    Binary operators, when overloaded, are given new functionality. The function defined for

    binary operator overloading, as with unary operator overloading, can be member function

    or friend function.

    The difference is in the number of arguments used by the function. In the case of binary

    operator overloading, when the function is a member function then the number ofarguments used by the operator member function is one (see below example). When the

    function defined for the binary operator overloading is a friend function, then it uses two

    arguments.

    Binary operator overloading, as in unary operator overloading, is performed using akeyword operator.

    Binary operator overloading example:

    #include class Exforsys

    {private:int x;int y;

    public:

    Exforsys() //Constructor

  • 8/8/2019 What is Inline Function

    19/22

    { x=0; y=0; }

    void getvalue( ) //Member Function for Inputting Values{cout > x;cout > y;}

    void displayvalue( ) //Member Function for Outputting Values{cout

  • 8/8/2019 What is Inline Function

    20/22

  • 8/8/2019 What is Inline Function

    21/22

    C++ Abstraction

    Abstraction is one of the most powerful and vital features provided by object-oriented C+

    + programming language. Modularity is very important in any programming language, itprovides flexibility to users for using the programming language. This aspect is well

    achieved with high performance by the concept of abstraction in C++. In object-orientedprogramming language the programmer can abstract both data and code when needed.

    What is Abstraction

    The concept of abstraction relates to the idea of hiding data that are not needed forpresentation. The main idea behind data abstraction is to give a clear separation between

    properties of data type and the associated implementation details. This separation is

    achieved in order that the properties of the abstract data type are visible to the userinterface and the implementation details are hidden. Thus, abstraction forms the basic

    platform for the creation of user-defined data types called objects. Data abstraction is the

    process of refining data to its essential form. An Abstract Data Type is defined as a datatype that is defined in terms of the operations that it supports and not in terms of its

    structure or implementation.

    In object-oriented programming language C++, it is possible to create and provide aninterface that accesses only certain elements of data types. The programmer can decide

    which user to give or grant access to and hide the other details. This concept is called data

    hiding which is similar in concept to data abstraction.

    How Types of Abstraction Differs:

    There are two broad types of abstraction; functionalabstraction and data abstraction.

    The main difference between functional abstraction and data abstraction is that functional

    abstraction refers to a function that can be used without taking into account how thefunction is implemented. Data abstraction refers to the data that can be used without

    taking into account how the data are stored. There is also a difference in the way the

    access takes place in functional abstraction and data abstraction. In functional abstraction,access to the function is provided through a specific interface defined to invoke the

    function. In contrast, in data abstraction, access to the data is provided through a specific

    set of operations defined to examine and manipulate the data. For instance, when a

    programmer is using C++ standard data types, this means that users are using the conceptof data abstraction. When using data types, the users are not concerned with how the data

    is stored but they are concerned with what operations are provided and what properties

    are supported.

    Reasons for the need of Abstraction

  • 8/8/2019 What is Inline Function

    22/22

    Flexibility in approach:

    By hiding data or abstracting details that are not needed for presentation, the programmer

    achieves greater flexibility in approach.

    Enhanced Security:

    Abstraction gives access to data or details that are needed by users and hide theimplementation details, giving enhanced security to application.

    Easier Replacement:

    With the concept of abstraction in object-oriented programming language, it is possible to

    replace code without recompilation. This makes the process easier and saves time for

    users.

    Modular Approach:

    In object-oriented programming language C++, the abstraction concept helps users to

    divide the project application into modules and test each of them separately. Then all

    modules are integrated and ultimately tested together. This approach makes theapplication development easier.

    There are various ways of achieving abstraction in object-oriented programming

    language C++. One approach is to take modular based code that is broken apart into

    smaller segments, known as functions. This functional or modular approach helps the

    code to be reused again and again when needed. For example, a programmer might writea function for computing an average and another programmer might write a function for

    computing salary. These functions can be reused when needed, by anyone. The modular

    based approach helps to centralize all data of a similar type, under the control of a typemodule. Defining module types allow the module to be an abstract data type. In many

    other programming languages, there is a small drawback associated with the approach to

    accessing module type.