Friend Function
Friend Function
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;
}
private:
int length;
int width;
int height;
public:
length = l;
width = w;
height = h;
int getVolume() {
};
class Friend {
public:
}
};
#include <iostream>
using namespace std;
class base {
public:
virtual void print() { cout << "print base class\n"; }
int main()
{
base* bptr;
derived d;
bptr = &d;
return 0;
}