Friend Functions:
Due to information hiding feature
in C++ the date from the class can be accessed only through member functions of
the class. it saves the data from any
possible misuse, accidental or otherwise.
But some times this feature take information hiding too far. There are situations where a rigid and
controlled access leads to inconvenience and hardships.
Then in such a situation friend
function is non-member function which has an access to a class’s private
members.
Example 1
Class Bclass; //
forward declaration
Class Aclass
{
private:
int Avar;
public:
Aclass (intv)
{
Avar = v;
};
friend int addup(Aclass & ac,
Bclass & bc);
};
class Bclass
{
private:
int Bvar;
public:
Bclass (int v)
{
Bvar =v;
};
friend int addup(Aclass & ac,
Bclass & bc);
};
int addup(Aclass & ac, Bclass
& bc) //
friend definition
{
return(ac. Avar+bc.Bvar);
}
void main(void)
{
Aclass a = 15;
Bclass b =16;
int total;
total = addup(a,b);
}
This function can directly access
private members of the classes. To
access private members, name of the member has to be prefixed with the name of
the object along with dot operator.
First line of the program is a forward declaration which tells the
compiler that a class with the name Bclass has been defined later in the
program.
A function does not belong to any
particular class it is not a member function hence to invoke it does not
require any object name, class name with scope resolution operator(::). The objects on which a friend function has to
operate upon have to be passed to it as arguments.
Controversial Friends:
Only when a function accesses
private (or protected) members of two or more classes directly, it has to be
declared as a friend function otherwise public members of the class can be
accessed directly by any function. There
is a lot of controversy regarding use of friend functions. On the one hand, friend functions increase
flexibility, on the other hand, they are against the principles of the
OOPS. A friend has to be declared as
such in a class definition. In the absence
of source code, it is not possible to do so.
If the source code is available, then existing working classes should
not be modified. For friend functions a
class should be designed as such, right from the beginning.
Friend Classes:
An entire class can be made a
friend, in which case all member functions of the friend class can be access
private members of the other class.
class cl
{
private:
int Apriv;
public:
-. ……….. // constructors and other functions
friend class bl;
};
class Bclass
{
private:
int Bpriv;
public:
void fn(cl ac)
{
Bpriv = ac.Apriv;
}
};
void main (void)
{
cl.aobj;
Bl.bobj;
Bobj.fn1(aobj);
}
This program declares class Bl to
be a friend of cl. It means that all
members functions of Bl have been granted direct access to all members of cl.
Inline Functions:
In C++ functions save memory
space, but take extra time. There are
lot of instructions associated with every function call. Which slows down the execution speed.
To save on this time, a small
function can be made inline. Compiler
replaces the function call to inline function by its code. This is the actual code of function is
written in calling function and the function itself is removed. The source code will have function calls, but
object code will contain the function code itself-nothing is left for linker to
resolve. Such functions are called
inline functions. Inline functions are
best suited for small functions. The
program may run faster and take less space, but source listing is longer and
complex. Readability and clarity, as
offered by use of inline functions, gets lost.
Example.
inline int square (int I)
{
return (i*i);
}
void main(void)
{
printf(“5*5 =%d\n”,square(5));
}
The function definition must be
written before any calls are made to that function. However the use of inline keyword is just a
request to the compiler. Sometimes,
compiler may not grant the request and compile the function as a normal function. It is at the discretion of the compiler
whether to generate code as inline or not.
It may decide the function is too long to be inline. Function
using loops can not be declared and used as inline functions.
Practical Session 6
Example 1(Friend Class)
#include<iostream.h>
#include<conio.h>
class two; // Forward Declaration
class one{
private:
int data1;
public:
void setdata(int init){
data1=init;
}
friend int add_both(one a,two b);
};
class two{
private:
int data2;
public:
void setdata(int init){
data2=init;
}
friend int add_both(one a,two b);
};
int add_both(one a,two b){
return a.data1+b.data2;
}
void main( ){
one a;
two b;
a.setdata(90);
b.setdata(900);
cout<<"\n Sum of the
one and two "
<<add_both(a,b);
getch( );
}
Example 2
#include<iostream.h>
#include<conio.h>
class boy{
private:
int income1;
int income2;
public:
void setdata(int in1,int in2){
income1=in1;
income2=in2;
}
friend class girl;
};
class girl{
int income;
public:
int girlfunc(boy b1){
return b1.income1 + b1.income2;
}
void setdata(int in){
income=in;
}
void show( ){
boy b1;
b1.setdata(1000,2000);
cout<<"\n Boy's Income
is :
"<<b1.income1;
cout<<"\n Girls Income
is :
"<<income;
}
};
void main( ){
clrscr( );
boy b1;
girl g1;
b1.setdata(5000,3000);
g1.setdata(900);
cout<<"\n Boys Total
income :
" <<g1.girlfunc(b1);
g1.show( );
getch( );
}
Example 3(Inline Function)
#include<iostream.h>
#include<string.h>
#include<conio.h>
class myname{
private:
char first[15];
char middle[15];
char last[15];
public:
myname( ){
first[0] = middle[0] =
last[0]='\0';
}
myname(char *firstname);
myname(char *firstname,char *middlename);
myname(char *firstname,char
*middlename,char *lastname);
void show( char *msg);
};
inline myname::myname(char
*firstname){
strcpy(first,firstname);
last[0] ='\0';
}
myname::myname(char
*firstname,char *middlename){
strcpy(first,firstname);
strcpy(middle,middlename);
}
myname::myname(char
*firstname,char *middlename,char *lastname){
strcpy(first,firstname);
strcpy(middle,middlename);
strcpy(last,lastname);
}
void myname::show( char *msg){
cout<<msg;
cout<<"\n First Name
: "<<first<<endl;
if(middle[0]){
cout<<"\n Middle Name
: "<<middle<<endl;
}
if(last[0]){
cout<<"\n Last Name
: "<<last<<endl;
}
}
void main( ){
clrscr( );
myname n1,n2,n3;
n1=myname("Sandhya");
n2=myname("Sandhya","Vinod");
n3=myname("Sandhya","Vinod","Chatarji");
cout<<" \t";
n1.show("First
Person");
cout<<endl;
cout<<" \t";
n2.show("Second
Person");
cout<<endl;
cout<<" \t";
n3.show("Last Person");
getch( );
}
Example 4
#include<iostream.h>
#include<conio.h>
const int MAX_LEN =25;
class person{
private:
char name[MAX_LEN];
char sex[5];
int age;
public:
void readdata( ){
cout<<"\n\n Enter
Name : ";
cin>>name;
cout<<"\n Enter
Gender : ";
cin>>sex;
cout<<"\n Enter
Age : ";
}
void display( ){
cout<<"\n Name:
"<<name;
cout<<"\t Gender:
"<<sex;
cout<<"\t Age:
"<<age;
}
};
class student:public person{
private:
int rollno;
char branch[20];
public:
void readdata( ){
person::readdata( );
cout<<"\n Roll
Number : ";
cin>>rollno;
cout<<"\n Study
Branch : ";
cin>>branch;
}
void display( ){
person::display( );
cout<<"\t Roll no:
"<<rollno;
cout<<"\t Branch:
"<<branch;
}
};
class exam:public student{
protected:
int m1,m2;
public:
void readdata( ){
student::readdata( );
cout<<"\n\n Scored
marks in subject 1: ";
cin>>m1;
cout<<"\n Scored marks
in subject 2: ";
cin>>m2;
}
void display( ){
student::display( );
cout<<"\n Marks1:
"<<m1;
cout<<"\t Marks2:
"<<m2;
cout<<"\t Total Marks:
"<<total( );
}
int total( ){
return m1+m2;
}
};
void main( ){
clrscr( );
exam annual;
cout<<"\n Enter data
for student. . . . ." ;
annual.readdata( );
cout<<"\n \t\t\t\t
Student Details ";
cout<<"\n
\t\t--------------------------------------------";
annual.display( );
getch( );
}
Example 5(Inline Function)
#include<iostream.h>
#include<conio.h>
inline int sqr(int num)
{
return num*num;
}
void main( ){
clrscr( );
float n;
cout<<"\n Enter a
Number : ";
cin>>n;
cout<<"\n Its Squre
No : "<<sqr(n)<<endl;
cout<<"\n Square of
10 :
"<<sqr(10)<<endl;
getch( );
}
Example 6(Inline Function)
#include<iostream.h>
#include<conio.h>
inline void errormessage(char
*s){
cout<<"\a"<<"\n"<<s;
exit(1);
}
main( ){
clrscr( );
errormessage("You called
?");
getch( );
}
Example 7
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(void){
char *weirdo
="ABRACADABARA";
int len =strlen(weirdo);
for(int sub =0; sub<=len;
++sub)
cout.write(weirdo,sub)<<"\n";
getch( );
}
Example 8(Accessing Friend Class)
#include<iostream.h>
#include<conio.h>
class complex {
private:
float
real;
float
imag;
public:
complex(
){
real=imag=0.0;
}
void
getdata( );
void
outdata(char *msg);
friend
complex operator -(complex c1)
{
complex
c;
c.real
= -c1.real;
c.imag
= -c1.imag;
return(c);
}
void
readdata( );
};
void
complex :: readdata( ){
cout<<"\n\n
Real Part : ";
cin>>real;
cout<<"\n
Imaginary Part : ";
cin>>imag;
}
void
complex::outdata(char *msg){
cout<<"\n
Message from the Server ";
cout<<msg<<endl;
cout<<"
( "<<real;
cout<<"
, "<<imag<<" )";
}
void main( ){
clrscr( );
complex c1,c2;
cout<<"\n Enter
Complex c1 ";
c1.readdata( );
c2= -c1;
c1.outdata("Complex c1 :
");
c2.outdata("Complex c2 = - Complex c1: ");
getch( );
}
Example 9(Friend Function)
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class wife;
class husband{
private:
char name[20];
int age,sal;
public:
husband(int a,int s){
age=a;
sal=s;
};
friend int addup(husband
&ac,wife &bc);
friend int diff(husband
&ac,wife &bc);
};
class wife{
private:
char name[20];
int age,sal;
public:
wife(int a,int s){
age=a;
sal=s;
};
friend int addup(husband
&ac,wife &bc);
friend int diff(husband
&ac,wife &bc);
};
int addup(husband &ac, wife
&bc){
return(ac.sal+bc.sal);
}
int diff(husband &ac,wife
&bc){
return(ac.age-bc.age);
}
void main(void){
husband asal(30,5000);
wife bsal(27,4000);
clrscr( );
int total;
total=addup(asal,bsal);
printf("\n Total salary of
Husband and Wife %d ",total);
husband aage(30,5000);
wife bage(27,3000);
int diff1;
diff1=diff(aage,bage);
printf("\n Difference of Age
between Husband and Wife %d ",diff1);
getch( );
No comments:
Post a Comment