What is Friend function? Can derived class inherit friend function too?
It is possible for a nonmember function to have access to the private members of a class by declaring it as a friend of the class.
For example, here frd( ) is declared to be a friend of the class cl:
class cl {
.
.
.
public:
friend void frd();
.
.
.
};
One reason that friend functions are allowed in C++ is to accommodate situations in which, for the sake of efficiency.
In below example The same_color( ) function is defined as
// Return true if line and box have same color.
int same_color(line l, box b)
{
if(l.color==b.color) return 1;
return 0;
}
As you can see, the same_color( ) function needs access to the private parts of both line and box to perform its task efficiently. Being a friend of each class grants it this access privilege. Further, notice that because same_color( ) is not a member, no scope resolution operator or class name is used in its definition.
#include <iostream.h>
#include <conio.h>
class line;
class box {
int color; // color of box
int upx, upy; // upper left corner
int lowx, lowy; // lower right corner
public:
friend int same_color(line l, box b);
void set_color(int c);
void define_box(int x1, int y1, int x2, int y2);
void show_box();
} ;
class line {
int color;
int startx, starty;
int len;
public:
friend int same_color(line l, box b);
void set_color(int c);
void define_line(int x, int y, int l);
void show_line();
} ;
// Return true if line and box have same color.
int same_color(line l, box b)
{
if(l.color==b.color) return 1;
return 0;
}
void box::set_color(int c)
{
color = c;
}
void line::set_color(int c)
{
color = c;
}
void box::define_box(int x1, int y1, int x2, int y2)
{
upx = x1;
upy = y1;
lowx = x2;
lowy = y2;
}
void box::show_box()
{
int i;
textcolor(color);
gotoxy(upx, upy);
for(i=upx; i<=lowx; i++) cprintf("-");
gotoxy(upx, lowy-1);
for(i=upx; i<=lowx; i++) cprintf("-");
gotoxy(upx, upy);
for(i=upy; i<=lowy; i++) {
cprintf("");
gotoxy(upx, i);
}
gotoxy(lowx, upy);
for(i=upy; i<=lowy; i++) {
cprintf("");
gotoxy(lowx, i);
}
}
void line::define_line(int x, int y, int l)
{
startx = x;
starty = y;
len = l;
}
void line::show_line()
{
int i;
textcolor(color);
gotoxy(startx, starty);
for(i=0; i<len; i++) cprintf("-");
}
int main()
{
box b;
line l;
b.define_box(10, 10, 15, 15);
b.set_color(3);
b.show_box();
l.define_line(2, 2, 10);
l.set_color(2);
l.show_line();
if(!same_color(l, b)) cout << "Not the same.\n";
cout << "\nPress a key.";
getch();
// now, make line and box the same color
l.define_line(2, 2, 10);
l.set_color(3);
l.show_line();
if(same_color(l, b)) cout << "Are the same color.\n";
return 0;
}
Note: There are two important restrictions that apply to friend functions.
1) A derived class does not inherit friend functions.
2) friend functions may not have a storage-class specifier.
That is, they may not be declared as static or extern.
What is Inline function? How do you decide to make function inline?
one very important C++ feature not found in C is the inline function. An inline function is a function whose code is expanded inline at the point at which it is called instead of actually being called.
This is much like a parameterized function-like macro in C, but more flexible.
consider the following example code for Inline function.
#include <iostream.h>
class myclass {
int i;
public:
int get_i();
void put_i(int j);
} ;
inline int myclass::get_i()
{
return i;
}
inline void myclass::put_i(int j)
{
i = j;
}
int main()
{
myclass s;
s.put_i(10);
cout << s.get_i();
return 0;
}
If you compile this version of the program and compare it to a compiled version of the program in which inline is removed, the inline version is several bytes smaller. Also, calls to get_i( ) and put_i( ) will execute faster.
Note: It is important to understand that, technically, inline is a request, not a command, to the compiler to generate inline code. There are various situations that can prevent the compiler from complying with the request. For example, some compilers will not inline a function if it contains a loop, a switch, or a goto.
The second way to create an inline function is to define the code to a function inside a class declaration.
#include <iostream.h>
class cl {
int i;
public:
// automatic inline functions
int get_i() { return i; }
void put_i(int j) { i = j; }
} ;
4 comments:
Some modern C compilers allow inline C functions. Actually, C has evolved a lot nowadays to help embedded solutions more efficient.
you might like this category which I started long time back but stopped pursuing further due to time constraint and lack of interest in readers.
http://rutmandal.info/guj/category/computer/
http://rutmandal.info/guj/category/cpp/
Hi Hiral,
Hot! That was HOT! Glued to the par excellence - beyond comparison your proficiency and style!
I am facing a wired problem. The socket descriptor is not free (entry persists in /proc/pid/fd)
even though it is closed.
The compiler allocates the required memory space for a declared variable. By using the address of operator, the reserved address is obtained and this address may be assigned to a pointer variable. Since most of the declared variable have static memory, this way of assigning pointer value to a pointer variable is known as static memory allocation. memory is assigned during compilation time.
Code snippet is
Code:
int ctrlSock = socket (AF_INET, SOCK_STREAM, 0);
if (ctrlSock < 0)
return (-1);
struct sockaddr_in ctrlAddr;
ctrlAddr.sin_family = AF_INET;
ctrlAddr.sin_addr.s_addr = INADDR_ANY;
ctrlAddr.sin_port = htons (0);
if (bind (ctrlSock, (struct sockaddr *)&ctrlAddr, sizeof (ctrlAddr)) < 0)
{
close (ctrlSock);
return (-1);
}
close (ctrlSock);
Here, even after close, the descriptor entry exits in /proc/pid/fd
I read multiple articles and watched many videos about how to use this tool - and was still confused! Your instructions were easy to understand and made the process simple.
Shukran,
Jessica
Hi There,
This is indeed great! But I think perhaps you are generally referring C++Interview Questions which is getting unsustainable.
I am given an assignment to write code on interpolation search binary search (searching an integer in an ordered list). but I don’t have a clear idea about both of them as I missed the theory class.so can anyone suggest me from where I can get satisfying information about them?
A linker converts an object code into an executable code by linking together the necessary build in functions. The form and place of declaration where the variable is declared in a program determine the linkage of variable.
I am so grateful for your blog. Really looking forward to read more.
Kind Regards,
Jim
Post a Comment