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...
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...
A memory is just like a human brain. It is used to store data and instructions. Computer memory is the storage space in the computer, where data is to be processed and instructions required for processing are stored. The memory is divided into large number of small parts called cells. Each location or cell has a unique address, which varies from zero to memory size minus one. For example, if the computer has 64k words, then this memory unit has 64 * 1024 = 65536 memory locations. The address of these locations varies from 0 to 65535. Memory is primarily of three types − Cache Memory Primary Memory/Main Memory Secondary Memory Cache Memory Cache memory is a very high speed semiconductor memory which can speed up the CPU. It acts as a buffer between the CPU and the main memory. It is used to hold those parts of data and program which are most frequently used by the CPU. The parts of data and programs are transferred from the disk to cache memory by the operating system, from...