Write a C++ program using inline function to check maximum of two integer numbers

Assignment 22

Write a C++ program using class to check maximum of two integer numbers using Inline function and conditional operator.

Example 1- Default Inline function

Program Code for run:

#include <iostream>

using namespace std;

class Operation

{

        int n1, n2;

    public:

        void accept()  // Default inline function

        {

            cout << " Enter the value of n1 and n2 ";

            cin >> n1 >> n2;

        }

        void max() // Default inline function

        {

            cout << "Maximum Number from " << n1 << " and " << n2;

            cout << " are " << ((n1 > n2)?(n1):(n2)) << endl;

        }

};

int main()

{

    Operation obj;

    cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"<< endl;

    cout << " \t \t Demo of Default Inline function " << endl;

    cout << " \t \t Check Maximum of Two Integer Numbers " << endl;

    cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"<< endl;

    obj.accept();

    cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"<< endl;

    obj.max();

    cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"<< endl;

    return 0;

}

Output of Program:

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                         Demo of Default Inline function
                         Check Maximum of Two Integer Numbers
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  Enter the value of n1 and n2
  23
  45
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     Maximum Number from 23 and 45 are 45
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

 Example 2 - Inline function using Keyword inline

Program Code for run:

#include <iostream>

using namespace std;

class Operation

{

        int n1, n2;

    public:

        void accept();  // inline function declaration

        void max(); // inline function declaration

};

inline void Operation :: accept()  // Use inline Keyword

{

    cout << " Enter the value of n1 and n2 ";

    cin >> n1 >> n2;

}

inline void Operation :: max() // Use inline Keyword

{

    cout << "Maximum Number from " << n1 << " and " << n2;

    cout << " are " << ((n1 > n2)?(n1):(n2)) << endl;

}

int main()

{

    Operation obj;

    cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"<< endl;

    cout << " \t \t Inline function using inline Keyword " << endl;

    cout << " \t \t Check Maximum of Two Integer Numbers " << endl;

    cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"<< endl;

    obj.accept();

    cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"<< endl;

    obj.max();

    cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"<< endl;

    return 0;

}

Output of Program:

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

               Inline function using inline Keyword

               Check Maximum of Two Integer Numbers

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    Enter the value of n1 and n2

    23

    45

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    Maximum Number from 23 and 45 are 45

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Comments