Which of the functions can only be called by another function that is a member of its class

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Member Function: It is a function that can be declared as members of a class. It is usually declared inside the class definition and works on data members of the same class. It can have access to private, public, and protected data members of the same class. This function is declared as shown below:

    C++

    class memberdemo {

    private:

        {

        public:

            {

                void check();

            }

            trial::void check();

        }

    }

    Non-member Function: The function which is declared outside the class is known as the non-member function of that class. Below is the difference between the two:

    • The member function can appear outside of the class body (for instance, in the implementation file). But, this approach is followed, the member function must be qualified by the name of its class. This is to identify that function is a member of a particular class. But a non-member function always appears outside of a class.
    • Another difference between member functions and non-member functions is how they are called (or invoked) in the main routine.

    Program 1:

    C++

    #include <iostream>

    using namespace std;

    class A {

        int a;

    public:

        void memberfn(int x)

        {

            a = x;

            cout << "Member function inside"

                 << " class declared\n";

        }

        void memberfn2();

    };

    void A::memberfn2()

    {

        cout << "Member function but declared "

             << " outside the class\n";

    }

    void nonmemberfn()

    {

        cout << "Non-member function\n";

    }

    int main()

    {

        A obj;

        obj.memberfn(5);

        obj.memberfn2();

        nonmemberfn();

        return 0;

    }

    Output:

    Member function inside class declared Member function but declared outside the class Non-member function

    Explanation: From the above program, there are three types of cases:

    • A member function is declared and defined in the class and called using the object of the class.
    • A member function is declared in the class but defined outside the class and is called using the object of the class.
    • A non-member function that is declared outside the class but called a normal function inside the main function.

    Now, the question here arises whether it is possible to call a non-member function inside a member function in a program or not. The answer is YES. Below is the program to illustrate how to call a non-member function inside a member function:

    Program 2:

    C++

    #include <bits/stdc++.h>

    using namespace std;

    int fact(int n)

    {

        if (n == 0 || n == 1)

            return 1;

        else

            return n * fact(n - 1);

    }

    class example {

        int x;

    public:

        void set(int x) { this->x = x; }

        int find() { return fact(x); }

    };

    int main()

    {

        example obj;

        obj.set(5);

        cout << obj.find();

        return 0;

    }

    Explanation: A non-member function can be called inside a member function but the condition is that the non-member function must be declared before the member function. In the above example, the same approach is followed and it becomes possible to invoke a non-member function thus the answer is the factorial of 5 i.e. 120.


    When a member function of a class is called the?

    Member functions are operators and functions that are declared as members of a class. Member functions do not include operators and functions declared with the friend specifier. These are called friends of a class. You can declare a member function as static ; this is called a static member function.

    Which type of member function may only be called from a function that is a member of the same class?

    A private member function may only be called from a function that is a member of the same class.

    Which type of function is not a member of a class but has access to the private members of the class?

    friend functions A friend function is a function that isn't a member of a class but has access to the class's private and protected members.

    Which of the following is accessed by a member function of a class?

    Which of the following is accessed by a member function of a class? Explanation: A member function of a class can access all the members of its class whether they are private, protected or public.

    Toplist

    Neuester Beitrag

    Stichworte