Friend Functions

Go down

Friend Functions

Post by Christopher on Wed Mar 03, 2010 10:22 am

Code:
#include<iostream>

class Sample
{
   int a;

public:

   Sample(int a)
   {
      this->a=a;
   }

   int getA()
   {
      return a;
   }

   friend int disp(Sample s);
};

int disp(Sample s)
{
   return s.a;
}

int main()
{
   Sample s(10);
   std::cout<<disp(s);
}

friend functions are not a part of class but can access all the members including private members of the class.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Friend Function

Post by Vineet_More on Wed Mar 03, 2010 10:42 am

Friend function is a powerful tool for the programmers but i think too much of friend fucntion should not be used as it can hamper the data security of the program.

Vineet_More

Posts : 27
Points : 31
Join date : 2010-02-26

View user profile

Back to top Go down

Re: Friend Functions

Post by Christopher on Wed Mar 03, 2010 10:46 am

if friend function is not used properly, it violates the encapsulation concept.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: Friend Functions

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum