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

Entrepreneurship Need and significances of entrepreneurship development It is said that an economic is an effect for which entrepreneurship is the cause. Entrepreneurship has development has therefore become a matter of great concretion in all countries. We can see the development of graph to America is developed day by day and become a most powerful country in the world due to development of entrepreneurship. almost all related entrepreneurship exists in America in different sector like industry, web developments, scientific research and so on. In other countries entrepreneurship development is a serious problem . all countries want to developed there entrepreneurship but the real problem is how to developed entrepreneurship. We have to create a program regrading entrepreneurship program(edp) offer the solution of this problem. Business main possess certain competitions which result in superior performances the question that arises is weather these characteristics are inb...

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); ...

Computer generation

* The development of computer has been very rapid in sixty year.the development has been disting in five stages. Each stage is called computer generation. *first generation 1946 to 1955 *second generation 1955 yo 1965 *third generation 1965 to 1975 * fourth generation 1975 to 1990 * fifth generation 1990 to yet