© 2012 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu
Quick Links
Easy Studies
« NS2  Projects »

Home Lab Exercise Object Oriented Programming Lab Exercise ProgramsCPP Program To Access Private and Protected Members Outside Classes Using Friend Function▼


CPP Program To Access Private and Protected Members Outside Classes Using Friend Function

Alogarithm steps:

  Step 1: create the class with private and protected numbers

  Step 2: Declare the function with keyword friend. This type function has the argument as function declared class type.

  Step 3: Define the friend function outside the class.

  Step 4: Use private and protected members in that friend function.

  Step 5: Call the friend function from main. 

CPP Program To Access Private and Protected Members Outside Classes Using Friend Function

#include #include class exforsys { private: int a,b; public: void test() { a=100; b=200; } friend int compute(exforsys e1) //Friend Function Declaration with keyword friend and with the object of class exforsys to which it is friend passed to it }; int compute(exforsys e1) { //Friend Function Definition which has access to private data return int(e1.a+e1.b-5); } main() { exforsys e; e.test(); //Calling of Friend Function with object as argument. cout<<"The result is:"<

SAMPLE INPUT AND OUTPUT:

The result is : 295

 
SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑
bottom