Untitled

mail@pastecode.io avatarunknown
plain_text
a month ago
722 B
1
Indexable
Never
// Add members of tus different classes using friend functions

#include<iostream>

using namespace std;
//forward declaration
class ClassB;
class ClassA
{ 
public:
// constructor to inicial.
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(22)
{}
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;