/*----------------------------------------------------- Script for AdSense -----------------------------------------------------*/ /* */ /* Footer ----------------------------------------------- */ #footer { clear: both; text-align: center; color: #333333; } #footer .widget { margin:.5em; padding-top: 20px; font-size: 85%; line-height: 1.5em; text-align: left; } /** Page structure tweaks for layout editor wireframe */ body#layout #header { width: 750px; } -->

Wednesday, April 28, 2010

C++ Interview Questions: 2

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; }
} ;

2 comments:

Chirag Patel said...

Some modern C compilers allow inline C functions. Actually, C has evolved a lot nowadays to help embedded solutions more efficient.

Chirag Patel said...

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/