Friend Funcion and Virtual Function
Friend Funcion and Virtual Function
• When virtual functions are used, a program that appears to be calling a function
of one class may in reality be calling a function of a different class.
• Following example shows what happens when a base class and derived classes all
have functions with the same name, and you access these functions using
pointers but without using virtual functions.
• #include <iostream> • class Derv2 : public Base //derived class 2
• using namespace std; • {
• class Base //base class • public:
• { • void show()
• public: • { cout << "Derv2\n";
• void show() //normal function • }
• };
• {
• int main()
• cout << "Base\n";
• {
• }
• Derv1 dv1;
• }; • Derv2 dv2;
• class Derv1 : public Base //derived class 1 • Base* ptr; //pointer to base class
• { • ptr = &dv1; //put address of dv1 in pointer
• public: • ptr->show(); //execute show()
• void show() • ptr = &dv2;
• { cout << "Derv1\n"; • ptr->show();
• } • return 0;
• }; • }
• #include <iostream> • class Derv2 : public Base
• using namespace std; • {
• class Base • public:
• { • void show() {
• public: • cout << "Derv2\n";
• virtual void show() • }
• { • };
• cout << "Base\n"; • main()
• } • {
• Derv1 dv1;
• };
• Derv2 dv2;
• class Derv1 : public Base
• Base* ptr;
• {
• ptr = &dv1;
• public:
• ptr->show();
• void show() {
• ptr = &dv2;
• cout << "Derv1\n";
• ptr->show();
• }
• }
Friend Function
• Friend Function in C++ is the creative function that breaks the data hiding in an
object-oriented programming language. The data hiding prevents the access of
private members externally from the class.
• The protected members are inaccessible from the outside and can only be
accessed by the derived classes.
•}
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.
• class className {
• ... .. ...
• friend returnType functionName(arguments);
• ... .. ...
• }
Friend Function
• #include <iostream> • int add(Distance d)
• using namespace std; • {
• class Distance { • //accessing private members from the friend function
• private: • d.meter = d.meter + 5;
• int meter; • return d.meter;
• friend int add(Distance);
• }
• public:
• main()
• Distance()
• {
• {
• meter = 10 ; • Distance D;
• } • cout << "Distance: " << add(D);
• }; • }
• #include <iostream> • class ClassB {
• using namespace std; • private:
• class ClassB; • int numB;
• class ClassA { • friend int add(ClassA, ClassB);
• private: • public:
• int numA; • ClassB()
• friend int add(ClassA, ClassB); • {
• public: • numB = 1 ;
• ClassA() • } int add(ClassA objectA, ClassB objectB)
• { • }; {
return (objectA.numA + objectB.numB);
• numA = 2 ; }
main() {
• } ClassA objectA;
• }; ClassB objectB;
cout << "Sum: " << add(objectA, objectB);
}
• #include <iostream> • public:
• using namespace std; • beta() {
• class beta; //needed for frifunc declaration • data = 7 ; }
• class alpha { • friend int frifunc(alpha, beta);
• private: • };
• int data; • int frifunc(alpha a, beta b) //function definition
• public: • {
• alpha() { • return( a.data + b.data );
• data = 3 ; } • }
• friend int frifunc(alpha, beta); • main() {
• }; • alpha aa;
• class beta { • beta bb;
• private: • cout << frifunc(aa, bb) << endl;
• int data; • }