---
title: "Add objects of two different classes"
subject: "Object Oriented Programming"
chapter: 4
key: "semester_3/object_oriented_programming:friend-add-two-class-objects"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/friend-add-two-class-objects
site: BCT Engineering
---

# Add objects of two different classes

Object Oriented Programming · Chapter 4: Objects and Classes

- Asked in: 2070 Asar, 6 marks; 2069 Chaitra, 6 marks (asked 2 times)
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/friend-add-two-class-objects
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-4

## Question

What do you understand by friend functions and classes? Explain with example. Write a program to add members of objects of two different classes.

## Short answer

A friend function, declared with the keyword friend, can access private members of the class granting friendship. A friend class grants all its member functions access to another class's private members. To add members of objects of two classes, declare a friend function that takes objects of both classes and returns the sum of their private fields.

## Answer

#### Friend function

A **friend function** is a non-member function declared inside a class with the keyword `friend`; it can access the private and protected members of that class. It is not called through an object and has no `this` pointer.

#### Friend class

A **friend class** is a class whose member functions can all access the private and protected members of another class. It is declared in the other class as `friend class ClassName;`. Friendship is not mutual and not inherited.

```cpp
class A
{
    int secret;
    friend class B;          // all members of B can access secret
};
class B
{
public:
    void show(A &a) { cout << a.secret; }
};
```

#### Program: add members of objects of two different classes

```cpp
#include <iostream>
using namespace std;

class Second;                        // forward declaration

class First
{
    int a;
public:
    void read() { cout << "Enter value for First: "; cin >> a; }
    friend int add(First, Second);   // friend declaration
};

class Second
{
    int b;
public:
    void read() { cout << "Enter value for Second: "; cin >> b; }
    friend int add(First, Second);
};

int add(First f, Second s)           // defined outside, no scope operator
{
    return f.a + s.b;                // access private members of both
}

int main()
{
    First f;
    Second s;
    f.read();
    s.read();
    cout << "Sum = " << add(f, s) << endl;
    return 0;
}
```

#### Sample output

```text
Enter value for First: 12
Enter value for Second: 8
Sum = 20
```

The forward declaration `class Second;` is needed because `First` uses `Second` in the friend prototype before `Second` is defined.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What does friend class B; inside class A allow?
   A. A can access private members of B
   B. All member functions of B can access private members of A
   C. Only B's constructor can access A
   D. B inherits from A
   Answer: B. The class granting friendship lets all member functions of the friend class access its private members. (Key point: Friend class)

2. Which describes friendship?
   A. Mutual and inherited
   B. Mutual but not inherited
   C. Not mutual and not inherited
   D. Inherited but not mutual
   Answer: C. If B is a friend of A, A is not automatically a friend of B, and friendship is not inherited. (Key point: Friendship properties)

3. Why is class Second; declared before class First?
   A. To create an object of Second
   B. Because First uses Second in the friend prototype before it is defined
   C. To make Second a friend
   D. To allow inheritance
   Answer: B. The compiler must know Second exists when it sees add(First, Second) inside First. (Key point: Forward declaration)

4. How is the friend function add() defined outside the classes?
   A. int First::add(...)
   B. As an ordinary function without scope operator
   C. With the keyword friend again
   D. Inside main() only
   Answer: B. A friend is defined like an ordinary function, without scope operator and without friend. (Key point: Friend definition syntax)

## Related in Object Oriented Programming

- [Time class: add two objects](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Atime-class-add-objects) — Object Oriented Programming, chapter 1
- [Program using static members](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astatic-program) — Object Oriented Programming, chapter 4
- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6
- [Friend class and encapsulation](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afriend-class-encapsulation) — Object Oriented Programming, chapter 4

## Related in other subjects

- [Consultant's friend supplies material; community objects](https://www.bctengineer.com/question.md?key=semester_8%2Fengineering_professional_practice%3Aconsultant-friend-supplier-quit) — Engineering Professional Practice, chapter 7
- [8085 ALP: add two tables, sum and carry](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-add-two-tables) — Microprocessor, chapter 2
- [Add two matrices](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Amatrix-add-two) — Computer Programming, chapter 7
- [Array overflow/underflow; add two arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-overflow-underflow-add) — Computer Programming, chapter 7
