Friend function

Friend function

A friend function is used in object-oriented programming to allow access to private or protected data in a class from outside the class. Normally a function which is not a member of a class cannot access such information; neither can an external class. Occasionally such access will be advantageous for the programmer; under these circumstances, the function or external class can be declared as a friend of the class using the keyword "friend." The function or external class will then have access to all information – public, private or protected – within the class.

This procedure should be used with caution. If too many functions or external classes are declared as friends of a class with protected or private data, necessary data security may be compromised, as well as the encapsulation of separate classes in object-oriented programming.

Example

The following is an example of friend function's usage. The function show() is a friend of classes A and B which is used to display the private members of A and B. Instead of writing a separate function in each of the classes only one friend function can be used to display the data items of both the classes.
#include using namespace std;

class B; // Forward declaration of class B in order for example to compileclass A{private: int a;public: void A() { a=0; } friend void show(A& x, B& y);};

class B{private: int b;public: void B() { b=0; } friend void show(A& x, B& y);};

void show(A& x, B& y){ cout << "A::a=" << x.a << endl; cout << "B::b=" << y.b << endl;}

int main(){ A a; B b; show(a,b);}

ee also

*friend class

References

*An Introduction to Object-Oriented Programming in C++ by Graham M. Seed

External links

* [http://www.codersource.net/cpp_tutorial_friend.html C++ friend function tutorial] at CoderSource.net
* [http://www.cplusplus.com/doc/tutorial/inheritance.html C++ friendship and inheritance tutorial] at cplusplus.com


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Friend-Funktion — Unter einer Friend Funktion oder Friend Methode versteht man in der objektorientierten Programmierung eine Methode (also eine Funktion oder eine Prozedur), der man erlaubt, auf private oder geschützte Daten (private oder protected) einer anderen… …   Deutsch Wikipedia

  • Friend class — In object oriented programming to allow access to private or protected data of a class in another class, the latter class is declared as a friend class.PurposeA friend class has full access to the private data members of a class without being a… …   Wikipedia

  • function — n 1 Function, office, duty, province are comparable when they mean the act, acts, activities, or operations expected of a person or thing by virtue of his or its nature, structure, status, or position. Function is the most comprehensive of these… …   New Dictionary of Synonyms

  • function — [fuŋk′shən] n. [OFr < L functio < pp. of fungi, to perform < IE base * bheug , to enjoy > Sans bhuṅktē, (he) enjoys] 1. the normal or characteristic action of anything; esp., any of the natural, specialized actions of a system, organ …   English World dictionary

  • function — I (New American Roget s College Thesaurus) n. faculty, office, duty, role, province; observance. See agency, business, rite. v. i. operate, serve. See action, use. II (Roget s IV) n. 1. [Use] Syn. employment, capacity, office, duty, faculty, role …   English dictionary for students

  • Wigner's friend — is a thought experiment proposed by the physicist Eugene Wigner; it is an extension of the Schrödinger s cat experiment designed as a point of departure for discussing the mind body problem in Quantum mechanics. The thought experiment The Wigner… …   Wikipedia

  • Imaginary friend — Imaginary friends, also known as imaginary companions , are pretend characters often created by children. Imaginary friends often function as (or perform a tutelary function) when they are engaged by the child in play activityFact|date=February… …   Wikipedia

  • Oh My Friend — For the First time in the telugu film industry oh my friend movie has online piracy protection called ORNB s MovieGuard Directed by Venu Sriram …   Wikipedia

  • Mitr, My Friend — Directed by Revathi Starring Shobhana, Nasir Abdullah, Preeti Vissa, Matt Phillips, Blake Ormsby, Shirley Bennett, Brian G. Linebaugh, Janine Pibal …   Wikipedia

  • Rvachev function — In mathematics, an R function or Rvachev function is a function whose sign can change if and only if the sign of one of its arguments changes, that is, if its sign is determined solely by its arguments.Typically, the function and its arguments… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”