Unit 2
Friend Function, Friend Class,
Container Class, Dynamic Object
Friend Function
• Data hiding is a fundamental concept of
object-oriented programming. It restricts the
access of private members from outside of the
class.
• Similarly, protected members can only be
accessed by derived classes and are
inaccessible from outside.
For Example
class MyClass
{
private:
int member1;
};
int main()
{
MyClass obj;
obj.member1 = 5;
// Error! Cannot access private members from here.
}
• However, there is a feature in C++
called friend functions that break this rule and
allow us to access member functions from
outside the class.
Friend Function
• A friend function can access the private and protected data of a
class. We declare a friend function using the friend keyword inside
the body of the class.
• Syntax:
class className
{ ... .. ...
friend returnType functionName(arguments);
... .. ...
}
Working of friend Function
#include <iostream>
using namespace std;
class Distance
{ private:
int meter;
// friend function
friend int addFive(Distance);
public:
Distance() {
meter=0; }
};
// friend function definition
int addFive(Distance d)
{ //accessing private members from the friend function
d.meter += 5;
return d.meter; }
int main() {
Distance D;
cout << "Distance: " << addFive(D);
return 0;
}
Output
• Distance: 5
• Here, addFive() is a friend function that can
access both private and public data members.
• Though this example gives us an idea about
the concept of a friend function.
• A more meaningful use would be operating on
objects of two different classes. That's when
the friend function can be very helpful.
Example: Add Members of Two Different Classes
• // Add members of two different classes using friend functions
#include <iostream>
using namespace std;
class ClassB;
class ClassA
{
private:
int numA;
// friend function declaration
friend int add(ClassA, ClassB);
public:
ClassA()
{
numA=12;
}
};
class ClassB
{
private:
int numB;
// friend function declaration
friend int add(ClassA, ClassB);
public:
ClassB()
{
numB = 1;
}
};
// access members of both classes
int add(ClassA objectA, ClassB objectB)
{
return (objectA.numA + objectB.numB);
}
int main()
{
ClassA objectA;
ClassB objectB;
cout << "Sum: " << add(objectA, objectB);
return 0;
}
• Output: Sum: 13
• In this program, ClassA and ClassB have
declared add() as a friend function. Thus, this
function can access private data of both classes.
• One thing to notice here is the friend function
inside ClassA is using the ClassB. However, we
haven't defined ClassB at this point.
• For this to work, we need a forward declaration
of ClassB in our program.
• // forward declaration
class ClassB;
Friend Class
class ClassB;
class ClassA
{
// ClassB is a friend class of ClassA
friend class ClassB;
... .. ...
}
class ClassB
{
... .. ...
}
Friend Class
• When a class is declared a friend class, all the
member functions of the friend class become friend
functions.
• Since ClassB is a friend class, we can access all
members of ClassA from inside ClassB.
• However, we cannot access members of ClassB from
inside ClassA. It is because friend relation in C++ is
only granted, not taken.
C++ program to demonstrate the working
of friend class
#include <iostream>
using namespace std;
// forward declaration
class ClassB;
class ClassA
{
private: int numA;
// friend class declaration
friend class ClassB;
public:
// constructor to initialize numA to 12
ClassA()
{
numA = 12;
}
};
class ClassB
{
private: int numB;
public:
// constructor to initialize numB to 1
ClassB()
{ numB = 1;
}
// member function to add numA
// from ClassA and numB from ClassB
int add()
{
ClassA objectA;
return objectA.numA + numB;
}
};
int main()
{
ClassB objectB;
cout << "Sum: " << objectB.add();
return 0;
}
• Output: Sum: 13
• Here, ClassB is a friend class of ClassA.
So, ClassB has access to the members of classA.
• In ClassB, we have created a function add() that
returns the sum of numA and numB.
• Since ClassB is a friend class, we can create
objects of ClassA inside of ClassB.
Container Class
• A container class as the name suggests is used to contain different values,
objects, and variables, etc. in the memory or the external storage.
• A container class supports other classes present in the programs and it
functions to hide the objects/variables used in the memory.
• It stores many items and all of these items are easily accessible by other
members of the program.
• All container classes access the elements of the container efficiently
through the iterators.
• This class is known to hold some similar and mixed objects in the
memory.
• A container can be of a homogeneous or heterogeneous type.
• If the container holds mixed objects then it is heterogeneous, while in the
case of similar items it is known as homogeneous container class.
Container Class
• A class is said to be a container when it is used to hold values
that are utilized by other variables in the same program.
• A GUI class library has a group of container classes.
• The pointers containers provide the containers that hold the
objects that are allocated through safe heap selection. This
usage of container class has the purpose to make OOP very
easy in the C++ language. This is done when a standard set of
the class is established.
• The type of relationship between classes is known as
containership.
• The class that contains this kind of relationship is the
container class. Similarly, the object is known as a container
object.
C++ Standard Container Classes
• The standard classes are described as follow:
• Std::map: This is used to handle an array or a
sparse matrix.
• Std::vector: Just like an array, the container
classes have additional features like inserting
and removing elements, memory management
automatically, and throw exceptions.
• Std::string: It is an array of characters.
Syntax of Containership
// Class that is to be contained
class One
{
};
// Container class
class Two {
// creating object of One
One O;
};
Example
Example 2
Dynamic Object
• In C++, the objects can be created at run-time.
• C++ supports two operators new and delete to
perform memory allocation and de-allocation.
• These types of objects are called dynamic objects.
• The new operator is used to create objects
dynamically and the delete operator is used to
delete objects dynamically.
• The dynamic objects can be created with the help
of pointers.
Syntax
• ClassName *ptr_obj; // pointer to object
• ptr_obj = new ClassName
// Dynamic object creation
• delete ptr_obj; // Delete object dynamically
C++ program to implement the dynamic
objects
#include<iostream>
using namespace std;
// Class definition
class Test
{
// Data members
int a, b;
public:
// Constructor to initialize
// data members of class
Test()
{
cout << "Constructor is called" <<
endl;
a = 1;
b = 2;
};
// Destructor
~Test()
{
cout << "Destructor is called" <<
endl;
}
// Function to print values
// of data members
void show()
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
};
int main()
{
// pointer to object
Test *ptr;
// dynamic object creation
ptr = new Test;
// Accessing member through
// pointer to object
ptr->show();
// Destroying object dynamically
delete ptr;
return 0;
}
Output
Constructor is called
a=1
b=2
Destructor is called