×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

C++ Friend Function | Find output programs | Set 1

This section contains the C++ find output programs with their explanations on C++ Friend Function (set 1).
Submitted by Nidhi, on June 18, 2020

Program 1:

#include <iostream>
using namespace std;

class Sample {
    int A, B;
    friend void fun();
};

void fun()
{
    Sample S;

    S.A = 10;
    S.B = 20;

    cout << S.A << " " << S.B << endl;
}

int main()
{
    fun();
    return 0;
}

Output:

10 20

Explanation:

Here, we created a class Sample that contains private data members A and B.

As we know that, we cannot access the private members outside the class. Here, we defined a non-member function as a friend function that can access private members.

Then, we set the values into A and B and printed.

Program 2:

#include <iostream>
using namespace std;

class Sample {
    int A, B;
    void fun();
};

friend void fun()
{
    Sample S;

    S.A = 10;
    S.B = 20;

    cout << S.A << " " << S.B << endl;
}

int main()
{

    fun();

    return 0;
}

Output:

main.cpp:9:1: error: ‘friend’ used outside of class
 friend void fun()
 ^~~~~~
main.cpp: In function ‘void fun()’:
main.cpp:13:7: error: ‘int Sample::A’ is private within this context
     S.A = 10;
       ^
main.cpp:5:9: note: declared private here
     int A, B;
         ^
main.cpp:14:7: error: ‘int Sample::B’ is private within this context
     S.B = 20;
       ^
main.cpp:5:12: note: declared private here
     int A, B;
            ^
main.cpp:16:15: error: ‘int Sample::A’ is private within this context
     cout << S.A << " " << S.B << endl;
               ^
main.cpp:5:9: note: declared private here
     int A, B;
         ^
main.cpp:16:29: error: ‘int Sample::B’ is private within this context
     cout << S.A << " " << S.B << endl;
                             ^
main.cpp:5:12: note: declared private here
     int A, B;
            ^

Explanation:

This code will generate an error because we are accessing the private members of class Sample in the function fun() but did not define fun() as a friend within the class. We need to define fun() as a friend inside the class.

class Sample
{
	int A,B;
	friend void fun();
};

Program 3:

#include <iostream>
using namespace std;

class Sample {
    int A, B;
    friend void fun();
};

friend void fun()
{
    Sample S;
    S.A = 10;
    S.B = 20;
    cout << S.A << " " << S.B << endl;
}

int main()
{
    fun();
    return 0;
}

Output:

main.cpp:9:1: error: ‘friend’ used outside of class
 friend void fun()
 ^~~~~~

Explanation:

This code will generate an error because we used friend keyword in the definition of the function fun(). We cannot use a friend keyword outside the class.

Program 4:

#include <iostream>
using namespace std;

class Sample1 {
    int A, B;

public:
    friend class Sample2;
};

class Sample2 {
    int X, Y;

    void fun1()
    {
        Sample1 S;
        S.A = 10;
        S.B = 20;

        cout << S.A << " " << S.B << endl;
    }
};

int main()
{
    Sample2 S;
    S.fun1();
    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:27:12: error: ‘void Sample2::fun1()’ is private within this context
     S.fun1();
            ^
main.cpp:14:10: note: declared private here
     void fun1()
          ^~~~

Explanation:

This code will generate an error, we are accessing the private member function fun1() in the main() function.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.