Write a program using a friend function, C/C++ Programming

Using a Friend

Using a friend function is quite easy. The following example explains a friend function to access members of two classes.

class Bclass;                                                        // Forward Declaration

class Aclass

 {

  public :

                                Aclass(int v)

                                 {

                                                Avar = v;

                                 }

 

friend int addup(Aclass &ac, Bclass &bc);

  private :

                                int Avar;

 };

class Bclass

 {

  public :

                                Bclass(int v)

                                 {

                                                Bvar = v;

                                 }

 

                                friend int addup(Aclass &ac, Bclass &bc);

  private :

int Bvar;

 

 };

int addup(Aclass &ac, Bclass &bc)

 {

                return( ac.Avar + bc.Bvar);

 }

void main()

 {

  Aclass aobj;

  Bclass bobj;

                  int total;

                                total = addup(aobj,bobj);

                 }

 

Posted Date: 6/28/2013 6:12:47 AM | Location : United States







Related Discussions:- Write a program using a friend function, Assignment Help, Ask Question on Write a program using a friend function, Get Answer, Expert's Help, Write a program using a friend function Discussions

Write discussion on Write a program using a friend function
Your posts are moderated
Related Questions
color representation 0,1,2,3,4,5,6,7...


A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

padovan series with words

Data is required to be fed into the program (input) and sent out of the program (output). Considering the draughtsman using a CAD station, the input is his commands to cause lines/

Most first graders know that nine hundred and ninety nine plus one is one thousand, but C++ doesn't! Sure, a computer can easily compute 999 + 1 and get 1000. But reading and writi

What are compound statements? - Compound statements are made up of two or more program statements that are executed together. They may be executed with a loop. - Curly brack

how made a flow chart of prime number by using for loop

#include #include using namespace std; class Book { private: string title; int iD; int year_pub; string author; public: string getTitle(); int getId(); int getYear_Pub(); st

Mathematical Statements and assignments    Within C we can directly load up the variable from within the program using the mathematical expression equates (=) e.g.   a= 'h'