You are on page 1of 4

• The keyword friend is placed only in the function declaration of the friend function

and not in the function definition..


• It is possible to declare a function as friend in any number of classes..
• When a class is declared as a friend, the friend class has access to the private data
of the class that made this a friend..
• A friend function, even though it is not a member function, would have the rights to
access the private members of the class..
• It is possible to declare the friend function as either private or public..
• The function can be invoked without the use of an object. The friend function has its
argument as objects, seen in example below.
Example to understand the friend function:

#include
class exforsys
{
private:
int a,b;
public:
void test()
{
a=100;
b=200;
}
friend int compute(exforsys e1)
//Friend Function Declaration with keyword friend and with the object of class exforsys to
which it is friend passed to it
};
int compute(exforsys e1)
{
//Friend Function Definition which has access to private data
return int(e1.a+e2.b)-5;
}

main()
{
exforsys e;
e.test();
cout<<"The result is:"<<compute(e);
</compute(e);
//Calling of Friend Function with object as argument.
}

Friend Class

class Storage 10 m_dValue = dValue; { m_bDisplayIntFirst =


02{ 11 } bDisplayIntFirst; }
03private: 14 friend class Display; void DisplayItem(Storage
04 int m_nValue; 15 }; &cStorage)
05 double m_dValue; class Display{ {
06 public: private: if (m_bDisplayIntFirst)
07 Storage(int nValue, bool m_bDisplayIntFirst; std::cout <<
double dValue) public: cStorage.m_nValue << " "
08 { Display(bool << cStorage.m_dValue <<
09 m_nValue = nValue; bDisplayIntFirst) std::endl;
else // display double << cStorage.m_nValue <<
first std::endl;
std::cout << }
cStorage.m_dValue << " " };

class secret testclass.printout(); class FourWheeler :


public Vehicle
}
{ friend class not; {
public:
private : Virtual Function void Make()
int a; {
class Vehicle //This cout<<"Virtual Member
};
denotes the base class of C++ function of Derived class
virtual function FourWheeler
class not { Accessed"<<endl;
{ public: public: }
not (int x) // constructor virtual void };
Make() //This denotes the C++ void main()
{ secret.a = x;}
virtual function {
void printout() { Vehicle *a, *b;
{ cout << secret.a cout <<"Member function a = new Vehicle();
of Base Class Vehicle a->Make();
<< endl; }
Accessed"<<endl; b = new FourWheeler();
}; } b->Make();
}; }
main()
{ not testclass(4); Virtual

base classes (C++ only)


Suppose you have two derived classes B and C that have a common base class A, and you also have another class Dthat
inherits from B and C. You can declare the base class A as virtual to ensure that B and C share the same subobject of A.
In the following example, an object of class D has two distinct subobjects of class L, one through class B1 and another
through class B2. You can use the keyword virtual in front of the base class specifiers in the base lists of
classes B1 and B2 to indicate that only one subobject of type L, shared by class B1 and class B2, exists.
For example:

class L { /* ... */ }; // indirect base class


class B1 : virtual public L { /* ... */ };
class B2 : virtual public L { /* ... */ };
class D : public B1, public B2 { /* ... */ }; // valid
Using the keyword virtual in this example ensures that an object of class D inherits only one subobject of class L.
A derived class can have both virtual and nonvirtual base classes. For example:
class V { /* ... */ };
class B1 : virtual public V { /* ... */ };
class B2 : virtual public V { /* ... */ };
class B3 : public V { /* ... */ };
class X : public B1, public B2, public B3 { /* ... */
};
In the above example, class X has two subobjects of class V, one that is shared by classes B1 and B2 and one through
class B3.
1 // 18 37 // instant 0]= ally
Fig. public class iate &both; invoke
24.13: : Multipl Derive // print
fig24_ 19 // e dTwo ERRO 60
13.cpp overrid definiti object R-- for (
2 // e print on 53 ambig int i =
Attem functio 38 Base uous 0; i <
pting n class *array[ 56 3; i+
to 20 Multipl 3 ]; // array[ +)
polym void e: create 1]= 61
orphic print() public array &one; array[ i
ally const Derive of 57 ] ->
call a 21 { dOne, base- array[ print();
functio 22 public class 2]= 62
n that cout Derive pointer &two; 63
is << dTwo s 58 return
3 // "Deriv 39 { 54 59 // 0;
multipl edOne 40 55 polym
y \n"; public array[ orphic
inherit 23 } :
ed // end 41 //
from functio qualify
two n print which
base 24 }; / versio
classe / end n of
s. class functio
4 Derive n print
#inclu dOne 42
de 25 void
<iostre 26 // print()
am.h> class const
5 Derive 43 {
7 8 // dTwo 44
class definiti Derive
Base on dTwo::
definiti 27 print();
on class 45 }
9 Derive // end
class dTwo : functio
Base public n print
10 { Base 46 }; /
11 28 { / end
public 29 class
: public Multipl
12 : e
virtual 30 // 47
void overrid 48 int
print() e print main()
const functio 49 {
= 0; // n 50
pure 31 Multipl
virtual void e both;
13 }; / print() //
/ end const instant
class 32 { iate
Base 33 Multipl
14 cout e
15 // << object
class "Deriv 51
Derive edTwo Derive
dOne \n"; dOne
definiti 34 } one; //
on // end instant
16 functio iate
class n print Derive
Derive 35 }; / dOne
dOne : / end object
public class 52
Base Derive Derive
17 { dTwo dTwo
36 two; //

You might also like