0% found this document useful (0 votes)
11 views1 page

Addition of Two Objects of Two Different Class

The document contains a C++ program that defines two classes, ClassA and ClassB, with ClassB declared as a friend of ClassA. ClassA has a private member 'numA' initialized to 12, while ClassB has a private member 'numB' initialized to 1. The program creates an instance of ClassB and calls its 'add' method to compute and display the sum of 'numA' and 'numB'.

Uploaded by

Suman Chatterjee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views1 page

Addition of Two Objects of Two Different Class

The document contains a C++ program that defines two classes, ClassA and ClassB, with ClassB declared as a friend of ClassA. ClassA has a private member 'numA' initialized to 12, while ClassB has a private member 'numB' initialized to 1. The program creates an instance of ClassB and calls its 'add' method to compute and display the sum of 'numA' and 'numB'.

Uploaded by

Suman Chatterjee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

using namespace std;

// forward declaration
class ClassB;

class ClassA {
private:
int numA;

// friend class declaration


friend class ClassB;

public:
// constructor to initialize numA to 12
ClassA() : numA(12) {}
};

class ClassB {
private:
int numB;

public:
// constructor to initialize numB to 1
ClassB() : numB(1) {}

// member function to add numA


// from ClassA and numB from ClassB
int add() {
ClassA objectA;
return objectA.numA + numB;
}
};

int main() {
ClassB objectB;
cout << "Sum: " << objectB.add();
return 0;
}

You might also like