Skip to main content

C++ Friend Function and Operator Overloading

Friend Function in C++

Introduction


C++ language uses object oriented approach which provides great deal of security by binding data and functions together. There are concepts in OOPS (data hiding and encapsulation ) that ensure that a non-member function should not be able to access an object’s private or protected data.

If you are not the member of a class, then u cannot access its private data. It adds security to the C++ programs, but can also add inconvenience. There are situations when it is required to allow functions outside the class to access and manipulate the private members of the class. This can be achieved by friend functions.

Use and Real-life example

[sws_blue_box box_size="100"] Suppose there are two classes, manager and teacher, and there is a need to use a income_tax() function on these two classes. Here C++ has the feature of friend function where it allows a common function to be made friend with both the classes, thereby allowing the function to access the private data of these classes and more importantly such function need not be a member of any of these classes. [/sws_blue_box] The concept of friend function is similar to a real life situation when you given your room keys to your friend and that friend only has access to the room and other people can access your room only through that friend.
Syntax and Working

A friend function is a non-member function that has been granted an access to a class’s private and protected members.

A non-member function can access the private and protected members of a class if it is declared a friend of that class. That is done by including a declaration of this external function within the class, and preceding it with the keyword friend. For instance to make a function tax() as a friend to class teacher , the class definition must look like as follows:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
class teacher
{
           private :
           …..................
           …..................
           public :
           ….................
           …..................

  friend void tax(void);
            ….................
            ….................
};
When declaring a function as friend function, the keyword friend appears only in the prototype declaration of the function inside the class definition, but the actual function declaration and definition does not contain any friend keyword and these occur outside the class definition.
Characteristics of Friend Functions

There are certain properties that friend functions should have :
Friend Function cab be invoked without the help of any object like a normal function.
The meaning and functionality of friend function is not affected if it is declared either in the public and private part of the class.
It is not in the scope of the class to which it has been declared as friend.
It cannot be called using the object of that class as it is not in the scope of the class.
Friend Functions can also be used in operator loading.
The member names cannot be accessed by the friend function directly, it has to use an object name and dot operator.
The functionality of friend function is not mutual. This means that if A  is declared friend to B, this does not give B right to access the private members of class A.

An Example:-

#include<iostream>


using namespace std;


class comp
{ public:

int real;
int img;
public:
comp()
{
real=0;
img=0;


}
comp(int r,int i)
{
real=r;
img=i;


}
comp(const comp &c)
{
real=c.real;
img=c.img;

}
comp operator=(comp c);
friend ostream& operator<<(ostream &,comp &c);
friend istream& operator>>(istream &,comp &c);
comp operator+(comp c);
comp operator,(comp c);




};


comp comp::operator,(comp c)
{ comp temp;
temp.real=c.real;
temp.img=c.img;
return(temp);



}


comp comp::operator+(comp c)
{
comp temp;
temp.real=real+c.real;
temp.img=img+c.img;
return(temp);


}

ostream& operator<<(ostream &dout,comp &c)
{
dout<< c.real<<endl;
dout<< c.img<<endl;


}
comp comp::operator=(comp c)
{
real=c.real;
img=c.img;
return(*this);



}
istream& operator>>(istream &din,comp &c)
{



din>>c.real;
din>>c.img;


}



int main()
{
comp c1,c2(2,3),c3,c4;

cout<<c1;

c3=c1;
cout<<c3<<endl;
cout<<c2;
c3=(c1,c2,c3);
cout<<c3;

return 0;
}



Comments

Popular posts from this blog

CDAC CCAT Rank - Which Centre you Should go for...

Subscribe us for our YouTube channel and any kind of help Click here to ask questions regarding CDAC 1. C-DAC (Head Quarters) Pune    CDAC's Admission Booklet- Process of Admission to Post Graduate Diploma Courses of C-DAC                         click below to know about the CCAT's This batch allotment                click above to know about the CCAT's This batch allotment Rank 1-300 c-dac HQ has been the best from the start. 2.  Sunbeam Pune Rank 300-500 I got very positive feedback from my friends who are in c-dac banglure main campus,since there are many companies you may get more opportunities. 3.  C-DAC Knowledge Park Rank 400-700 It as very good faculty .Almost all the students get placed here every year. 4. C-DAC Hyderabad Rank 200-1000 (It depends on the course which you select) C-DAC hyderabad is very good for the course PG-DESD. PG-DESD course is in hyderbad is better than pune HQ as per the past feedback.It as v

CDAC COURSES AND PLACEMENTS, WHICH IS BETTER FOR YOU

Subscribe us for our YouTube channel and any kind of help   Click here to ask questions regarding CDAC Before going through this post, I would like to draw your attention towards the importance of this post. This page not only explains my experience in CDAC but also aims at answering the queries of you all who are going or looking to have a course from CDAC. Kindly post your queries at the bottom of this page and we will get back to you within 24 hours. Kindly do not post your queries as an Anonymous user and do not forget to subscribe via email so as to keep track of your query.                         click below to know about the CCAT's This batch allotment                 click below to know about the CCAT's This batch allotment ______________________________________________ NOW a day lot of the graduates and post graduates are wondering most of the times on which course they should go for. What are the pros & cons of joining a particular ce

Placement Statistics - 2011 & 2012

Subscribe us for our YouTube channel and any kind of help                          Post your queries below and we will get back to you in no more than 24 hours.  Click here to ask questions regarding CDAC                         click below to know about the CCAT's This batch allotment                     click above to know about the CCAT's This batch allotment CDAC's Admission Booklet- Process of Admission to Post Graduate Diploma Courses of C-DAC Click here to see placement statistics About C-CAT, Exam Pattern and Books No. of Seats Across Various Training Centres Important Dates - 2014 Tags: CDAC, CDAC scope in future, CDAC placements, CDAC training, CDAC recruitment, CDAC training centres, DSSD, DESD, DAC, PGDSSD, PGDESD, PGDAC, DABC, PGDABC, VLSI, PGDITISS, PGDIVESD, PGDESD, PGDWiMC, placement statistics