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

Assignment Help:

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);

                 }

 


Related Discussions:- Write a program using a friend function

Write a c program to calculate the equation, Write a C program to calculate...

Write a C program to calculate the output Y for a given value of X for the following formula   Y=X 2 + 2X +3   #include stdio.h   void main()   {   char promt;   float y,x

Define constructors-extract and insert operators, For your class to work pr...

For your class to work properly, you'll need to define appropriate constructors, extract and insert operators, and of course arithmetic operators. (If you wanted to use it as a gen

Class & objects, how objects are initialized dynamically?

how objects are initialized dynamically?

Virtual destructor, What about Virtual Destructor? describe it.

What about Virtual Destructor? describe it.

Palindrome, A palindrome is a string that reads the same from both the ends...

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

Explain call by reference, Call by Reference Passing variables(paramete...

Call by Reference Passing variables(parameters) to a function in C can be done in two ways - pass by value, also called as call by value and pass by address or also known as ca

Is it legal for a member function to say delete this?, Is it legal for a me...

Is it legal for a member function to say delete this? A: As long as you're cautious, it's OK for an object to delete this. Here's how I define "cautious": You have to be a

Reverse digit function, how can i write reverse digit function like writing...

how can i write reverse digit function like writing 1234 and printing 4321

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd