Friend Class in C++ - learnit

Home Top Ad

Post Top Ad

Saturday, May 23, 2020

Friend Class in C++

Friend Class in C++

C++ Programming and Friend Class in C++

C++ is a powerful general-purpose programming language. It can be used to develop operating systems, browsers, games, and so on. C++ supports different ways of programming like procedural, object-oriented, functional, and so on. This makes C++ powerful as well as flexible.


Why Learn C++

-C++ is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance.


-After learning C++, it will be much easier to learn other programming languages like Java, Python, etc.


-C++ helps you to understand the internal architecture of a computer, how computer stores and retrieves information



Friend Class in C++

Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class.


Friend Function Like friend class, a friend function can be given special grant to access private and protected members. A friend function can be:

    a) A method of another class

    b) A global function.


Example

#include<iostream>
#include<string>
using namespace std;
class test{
friend class test1;
private:
int a,b,total;
};
class test1{
public:void additional(test c)
{
cout<<"Please input Number A";
cin>>c.a;
cout<<"Please input Number B";
cin>> c.b;
c.total=c.a+c.b;
cout<<c.total;
}
};
int main()
{
test obj;
test1 obj1;
obj1.additional(obj);
}

Input:
Please input Number A 40
Please input Number B 50
Out Put:
90

Please Watching My Video is Below

Reference
http://en.wikipedia.org/wiki/Friend_class
http://en.wikipedia.org/wiki/Friend_function
http://www.cprogramming.com/tutorial/friends.html
http://www.parashift.com/c++-faq/friends-and-encap.html
https://www.geeksforgeeks.org/friend-class-function-cpp

No comments:

Post a Comment

Post Top Ad