Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Friend Functions

2 posters

Go down

Friend Functions Empty Friend Functions

Post by Christopher 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.
Christopher
Christopher
Admin

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

https://unixcpp.forumotion.com

Back to top Go down

Friend Functions Empty Friend Function

Post by Vineet_More 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

Back to top Go down

Friend Functions Empty Re: Friend Functions

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

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

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

https://unixcpp.forumotion.com

Back to top Go down

Friend Functions Empty 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