0% found this document useful (0 votes)
6 views4 pages

Friend Function

Object oriented program about a friend function
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Friend Function

Object oriented program about a friend function
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Here are 10 features of friend function in C++:

1. A friend function can be a global function or a member function of another class.


2. A friend function should not be in the scope of the class of which it is a friend.
3. A friend function can be declared anywhere in the class.
4. A friend function can be called like a normal function.
5. A friend function cannot directly access the protected or private data members of the
class.
6. A friend function can use objects as arguments.
7. A friend function can access all the non-public members of a class.
8. They can be used to bridge two classes by operating objects of two different classes.
9. They increase the versatility of overloading operators.
10. They enhance encapsulation.
 A friend function can be a global function or a member function of another class.

 A friend function can access the private and protected members of the class that
grants it access.
 A friend function cannot be inherited.
 A friend function cannot be declared as virtual.
 A friend function can be overloaded.
 A friend function can be used to implement operator overloading.

class Box {
private:
int length;
int width;
public:
Box(int l, int w) : length(l), width(w) {}
friend void printLength(Box b);
};

void printLength(Box b) {
cout << b.length << endl;
}

int main() {
Box b(10, 20);
printLength(b); // Prints 10
return 0;
}

Here are the 10 features of friend class in C++:


1. A friend class can access the private and protected members of another class.
2. A friend class can be declared inside or outside the class it is a friend of.
3. A friend class can be a class or a struct.
4. A friend class can be declared with or without the keyword friend.
5. A friend class cannot be inherited from.
6. A friend class cannot be used as a base class.
7. A friend class cannot be used as a template parameter.
8. A friend class cannot be used as a member of another class.
9. A friend class cannot be used as a function parameter.
10. A friend class cannot be used as a return type from a function.
class Box {

private:

int length;

int width;

int height;

public:

friend class Friend;

Box(int l, int w, int h) {

length = l;

width = w;

height = h;

int getVolume() {

return length * width * height;

};

class Friend {

public:

int getVolume(Box box) {

return box.length * box.width * box.height;

}
};

A virtual function (also known as virtual methods) is a member function that is


declared within a base class and is re-defined (overridden) by a derived class. When
you refer to a derived class object using a pointer or a reference to the base class,
you can call a virtual function for that object and execute the derived class’s version
of the method.
 Virtual functions ensure that the correct function is called for an object,
regardless of the type of reference (or pointer) used for the function call.
 They are mainly used to achieve Runtime polymorphism.
 Functions are declared with a virtual keyword in a base class.
 The resolving of a function call is done at runtime.
Rules for Virtual Functions
The rules for the virtual functions in C++ are as follows:
1. Virtual functions cannot be static.
2. A virtual function can be a friend function of another class.
3. Virtual functions should be accessed using a pointer or reference of base
class type to achieve runtime polymorphism.
4. The prototype of virtual functions should be the same in the base as well
as the derived class.
5. They are always defined in the base class and overridden in a derived
class. It is not mandatory for the derived class to override (or re-define the
virtual function), in that case, the base class version of the function is
used.
6. A class may have a virtual destructor but it cannot have a virtual
constructor.
 C++

// C++ program to illustrate


// concept of Virtual Functions

#include <iostream>
using namespace std;

class base {
public:
virtual void print() { cout << "print base class\n"; }

void show() { cout << "show base class\n"; }


};
class derived : public base {
public:
void print() { cout << "print derived class\n"; }

void show() { cout << "show derived class\n"; }


};

int main()
{
base* bptr;
derived d;
bptr = &d;

// Virtual function, binded at runtime


bptr->print();

// Non-virtual function, binded at compile time


bptr->show();

return 0;
}

You might also like