Friend Function

 #include <iostream>
 #include <conio.h>
    using namespace std;
    class example
    {
    private:
            int a,b;
    public:
           void Input();
           friend int sum(example akses); /*Friend Function Declaration with keyword friend and with the object of class exforsys to which it is friend passed to it*/
    };
void example :: Input()
{
     cout<<"Masukan nilai 1 : ";
     cin>>a;
     cout<<"Mauskan nilai 2 : ";
     cin>>b;
}
    int sum(example akses)
    {
            //Friend Function Definition which has access to private data
            return(akses.a+akses.b);
    }
   
    int main()
    {
            example akses;
            akses.Input();
            cout << "The result is : " << sum(akses);//Calling of Friend Function with object as argument.
           
            system("pause");
            return 0;
     }

No comments:

Post a Comment

Tutorial Lengkap Hosting Website Gratis Dengan Gambar

Hallo Guys.. sudah sangat lama sekali sejak terakhir kali saya melakukan posting di blogger ini... kalau saya tidak salah terakhir saya ...