Friends: Friend Function Sample
Friends: Friend Function Sample
A
function which is not a member or an external class can never access such private data. But
there may be some cases, where a programmer will need need access to the private data from
non-member functions and external classes. C++ offers some exceptions in such cases.
A class can allow non-member functions and other classes to access its own private data, by
making them as friends.
• Once a non-member function is declared as a friend, it can access the private data of the
class
• similarly when a class is declared as a friend, the friend class can have access to the
private data of the class which made this a friend
#include <iostream.h>
//Declaration of the function to be made as friend
int AddToFriend(int x);
class CPP_Tutorial
{
int private_data;
friend int AddToFriend(int x);
public:
CPP_Tutorial()
{
private_data = 5;
}
};
int AddToFriend(int x)
{
CPP_Tutorial var1;
return var1.private_data + x;
}
int main()
{
cout << "Added Result for this C++ tutorial: "<< AddToFriend(4)<<endl;
}
friend class:
Declaration of a friend class is also similar. Only thing is a class definition is slightly different.
Friend class:
This is a good way out given by C++ to avoid restrictions on private variables. But this should
be used with caution though. If all the functions and classes are declared as friends, then the
concept of encapsulation and data security will go for a toss.
That is why the concept of friend functions and classes should be used with proper judgment.
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 D that 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:
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.
Creating or deriving a new class using another class as a base is called inheritance in C++. The
new class created is called a Derived class and the old class used as a base is called a Base
class in C++ inheritance terminology.
The derived class will inherit all the features of the base class in C++ inheritance. The derived
class can also add its own features, data etc., It can also override some of the features
(functions) of the base class, if the function is declared as virtual in base class.
. When a class is inherited all the functions and data member are inherited, although not all of
them will be accessible by the member functions of the derived class. But there are some
exceptions to it too.
There are some points to be remembered about C++ inheritance. The protected and public
variables or members of the base class are all accessible in the derived class. But a private
member variable not accessible by a derived class.
It is a well known fact that the private and protected members are not accessible outside the
class. But a derived class is given access to protected members of the base class.
Let us see a piece of sample code for C++ inheritance. The sample code considers a class
named vehicle with two properties to it, namely color and the number of wheels. A vehicle is a
generic term and it can later be extended to any moving vehicles like car, bike, bus etc.,
class Car: public vehicle //Sample derived class for C++ inheritance tutorial
{
protected:
char type_of_fuel;
public:
Car();
};
The derived class Car will have access to the protected members of the base class. It can also
use the functions start, stop and run provided the functionalities remain the same.
In case the derived class needs some different functionalities for the same functions start, stop
and run, then the base class should implement the concept of virtual functions.