Skip to main content

Friend function in c++

                                                             
   FRIEND FUNCTION

We know that private member cannot be access from out side the class.That is a non member function cannot have an access to the private data of a class,but there may be some situation where we would like two classes to share a particular function.In such situation c++ allows the common function to be made friendly with both the classes,that allows to have access to the private data of these classes.Such a function need not be a function of any of these classes.
To declear a friend function the function decided by  the keyword friend.the definition does not used the keyword friend or the scope resulation operator(::) .A function can be decleare as a friend in any number of classes.All the a friend function of class is a but it has full access to the private members of class.

Charaterstatices of friends function :-

   ·       A friend function is not the scope of class to his it has been decleare as friend.
   ·      Since it is not in the scope of the class, it cannot be called using the object of class it can be called like a normal function without the help of any function
   ·      It cannot access data members directly and has to used an object and dot membership operator with each member name.
   ·      Friend function either in the public or private part of th class without effecting its meaning.
   ·      Usually it has the object as arguments.

Friend function with single class

#include<iostream.h>
#include<conio.h>
Class test
{
Int a,b;
Public:
Void getdata()
{
Cout<<”enter two values”;
Cin>>a>>b;
}
Friend float mean(test t);
};
Float mean(test t)
{
Float x;
X=(t.a+t.b)/2;
Return x;
}
Void main()
{
Test t;
t.getdata();
cout<<”\n mean value=”<<mean(t);
getch();
}

Friend function double class :-

#include<iostream.h>
#include<conio.h>
Class test2 ;
Class test1
{
Int x;
Public:
Void getdata()
{
Cout<<”enter any value”;
Cin>>x;
}
Friend void sum( test1, test2);
};
Class test2
{
Int y;
Public:
Void getdata()
{
Cout<<”\n enter any value”;
Cin>>y;
}
Friend void sum( test1, test2);
};
Void sum(test1 t1, test2 t2)
{
Int total;
total=t1.x + t2.y;
cout<<”\n sum=”<,total;
}
Void main()
{
Test t1;
Test2 t2;
T1.getdata();
T2.getdata();
Sum(t1,t2);
Getch();
}



Comments

Popular posts from this blog

Array in c

                                                              ARRAY An array is a fixed size scequence collection of element of the same data type.It is simply a grouping of like type data.In its simplest form,an array be used to represent a list of number or a list of names.An array is a collection of similar data type elements.some examples where the concept of an array can be used. List of temperatures records every hour in a day,or a month, or a year. List of employee in an organization. List of products and their post sold by a store. Test score of a class of student. List of consumers and their telephones number and etc. Since an array provide a convent structure for represent data, it is classified as one of the data structure in C.
Entrepreneurship opportunities: - There are many ways in which a person might exercise his or her entrepreneurial skills. The three main types of businessman opportunities include franchises, developing new operations within an existing organization, and forming a completely new one. Businessman opportunities can mean anything from working on small projects or the development of massive new enterprises. It is possible to find businessman opportunities within an already established organization. People who do so are sometimes called entrepreneurs, or inside businessmen. An entrepreneur takes initiative to identify and develop projects that help an organization meet its objectives or grow in new directions. Corporate social businessman opportunities relate specifically to leading projects that enhance a company's performance in terms of social development and responsibility. Many companies today encourage different kinds of entrepreneurship across all of their departm

Pointer in c language

                                                          Pointer A pointer is a drivided class.It is built from one of the fundamental data type available in c.Pointer contain memory addresses as their values.since these memory addresses are the locations in the computer memory where program instructions and data are stored.Pointer can be used to access and manuplates data store in the memory. Pointer decleartion :- A=[10] Where a is variable name. 10 is the value of variable. Adderss of variable(suppose) 65524. *p – pointer symbol Pointer program :- #include<stdio.h> #include<conio.h> Void main() { Int a ; Int *p ; Printf(“enter a no”); Scanf(“%d”,&a); P=&a; Printf(“\n value of variable %d=”,a); Printf(“\n value of variable %d=”,*p); Printf(“\n addres of variable %u=”,&a); Printf(“\n address of variable %u=”,p); Getch() ; }