0% found this document useful (0 votes)
48 views6 pages

Friends: Friend Function Sample

C++ allows classes to declare other functions and classes as "friends" so they can access the class's private members. A friend function declared outside the class can access the class's private data. Similarly, a friend class can access the private data of the class that declared it as a friend. This provides an exception to the usual encapsulation rules for private members. The concept of friends should be used judiciously to avoid compromising encapsulation.

Uploaded by

Hemank Sabharwal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views6 pages

Friends: Friend Function Sample

C++ allows classes to declare other functions and classes as "friends" so they can access the class's private members. A friend function declared outside the class can access the class's private data. Similarly, a friend class can access the private data of the class that declared it as a friend. This provides an exception to the usual encapsulation rules for private members. The concept of friends should be used judiciously to avoid compromising encapsulation.

Uploaded by

Hemank Sabharwal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Any data which is declared private inside a class is not accessible from outside the class.

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

Friend function sample:

#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;
}

The output of the above C++ Tutorial sample will be


Added Result for this C++ tutorial: 9

friend class:

Declaration of a friend class is also similar. Only thing is a class definition is slightly different.

Friend class:

#include < iostream.h >


class CPP_Tutorial
{
int private_data;
friend class friendclass;
public:
CPP_Tutorial()
{
private_data = 5;
}
};
class friendclass
{
public:
int subtractfrom(int x)
{
CPP_Tutorial var2;
return var2.private_data - x;
}
};
int main()
{
friendclass var3;
cout << "Added Result for this C++ tutorial: "<< var3.subtractfrom(2)<
}

The output of the above C++ Tutorial sample will be


Subtracted Result for this C++ tutorial: 3

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:

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.
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.

C++ inheritance is very similar to a parent-child relationship

. 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.

Some of the exceptions to be noted in C++ inheritance are as follows.

• The constructor and destructor of a base class are not inherited


• the assignment operator is not inherited
• the friend functions and friend classes

of the base class are also not inherited.

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 vehicle //Sample base class for c++ inheritance tutorial


{
protected:
char colorname[20];
int number_of_wheels;
public:
vehicle();
~vehicle();
void start();
void stop();
void run();
};

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.

You might also like