Computer Science, asked by Aayushimaurya2, 3 months ago

write c++ program to define a class distance having data member as meters display the addition of two distance using friend function

Answers

Answered by ashfanakn
0

Answer:

// Add members of two different classes using friend functions

#include <iostream>

using namespace std;

// forward declaration

class ClassB;

class ClassA {

public:

// constructor to initialize numA to 12

ClassA() : numA(12) {}

private:

int numA;

// friend function declaration

friend int add(ClassA, ClassB);

};

class ClassB {

public:

// constructor to initialize numB to 1

ClassB() : numB(1) {}

private:

int numB;

// friend function declaration

friend int add(ClassA, ClassB);

};

// access members of both classes

int add(ClassA objectA, ClassB objectB) {

return (objectA.numA + objectB.numB);

}

int main() {

ClassA objectA;

ClassB objectB;

cout << "Sum: " << add(objectA, objectB);

return 0;

}

Output

Sum: 13

In this program, ClassA and ClassB have declared add() as a friend function. Thus, this function can access private data of both classes.

On

Answered by chinnuchinnu95727
1

Answer:

continue with previous one

thank you for free points

Explanation:

and by the way you are soo beautiful in your profile

Similar questions