---
title: "mdistance and edistance addition via friend"
subject: "Object Oriented Programming"
chapter: 4
key: "semester_3/object_oriented_programming:mdistance-edistance-friend"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/mdistance-edistance-friend
site: BCT Engineering
---

# mdistance and edistance addition via friend

Object Oriented Programming · Chapter 4: Objects and Classes

- Asked in: 2069 Chaitra, 6 marks
- Video: [C++ Program - friend function in c++ for adding two different distances](https://www.youtube.com/watch?v=wO9vdjwgCNQ) — MicroNG
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/mdistance-edistance-friend
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-4

## Question

Create a class mdistance to store the values in meter and centimeter and class edistance to store values in feet and inches. Perform addition of object of mdistance and object of edistance by using friend function.

## Short answer

A friend function accesses private members of both classes. It converts mdistance to cm (meter*100+cm) and edistance to cm ((feet*12+inch)*2.54), sums them, and converts back to meters and centimeters. Example: 2m 50cm + 3ft 6in =3m 56.68cm.

## Answer

A friend function is not a member of a class but can access its private members if the class declares it with the keyword `friend`. Here one function `add()` is declared as a friend of both classes, so it can read the private data of both `mdistance` (metre, centimetre) and `edistance` (feet, inches).

**Assumptions:** 1 m = 100 cm, 1 ft = 12 in, 1 in = 2.54 cm. The sum is displayed in metres and centimetres.

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

class edistance;                 // forward declaration

class mdistance {
    int meter;
    float cm;
public:
    void getdata() {
        cout << "Enter meter and centimeter: ";
        cin >> meter >> cm;
    }
    friend void add(mdistance, edistance);
};

class edistance {
    int feet;
    float inch;
public:
    void getdata() {
        cout << "Enter feet and inches: ";
        cin >> feet >> inch;
    }
    friend void add(mdistance, edistance);
};

// friend of both classes
void add(mdistance m, edistance e) {
    // convert everything to centimetres
    float total_cm = m.meter * 100 + m.cm
                   + (e.feet * 12 + e.inch) * 2.54;
    int mt = (int)(total_cm / 100);
    float c = total_cm - mt * 100;
    cout << "Sum = " << mt << " m " << c << " cm" << endl;
}

int main() {
    mdistance m;
    edistance e;
    m.getdata();
    e.getdata();
    add(m, e);
    return 0;
}
```

**Sample run:**

```text
Enter meter and centimeter: 2 50
Enter feet and inches: 3 6
Sum = 3 m 56.68 cm
```

Check: 3 ft 6 in = 42 in = 106.68 cm; 2 m 50 cm = 250 cm; total = 356.68 cm = 3 m 56.68 cm.

## Check yourself

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

1. How can add() access the private data of both mdistance and edistance?
   A. It is a member of both
   B. It is declared friend in both classes
   C. The data is public
   D. It is inherited
   Answer: B. A single function declared friend in both classes can read both private data. (Key point: Friend of both)

2. What does add() convert everything to before summing?
   A. Feet
   B. Centimetres
   C. Inches
   D. Metres only
   Answer: B. It converts to centimetres, with 1 in = 2.54 cm. (Key point: Common unit)

3. How many cm is 3 ft 6 in?
   A. 106.68
   B. 91.44
   C. 42
   D. 180
   Answer: A. 42 in x 2.54 = 106.68 cm. (Key point: Conversion)

4. 2 m 50 cm + 3 ft 6 in = ?
   A. 3 m 56.68 cm
   B. 5 m 56 cm
   C. 2 m 106.68 cm
   D. 3 m 6.68 cm
   Answer: A. 250 + 106.68 = 356.68 cm = 3 m 56.68 cm. (Key point: Sum result)

## Related in Object Oriented Programming

- [Vector class addition by object argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avector-class-addition) — Object Oriented Programming, chapter 1
- [Member function as friend of another class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amember-function-friend-of-other-class) — Object Oriented Programming, chapter 4
- [Friend class and encapsulation](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afriend-class-encapsulation) — Object Oriented Programming, chapter 4
- [Need of friend function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afriend-function-need) — Object Oriented Programming, chapter 4, asked 2×
- [LandMeasure class and sum function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Alandmeasure-class-sum) — Object Oriented Programming, chapter 4
- [Pure virtual function and abstract class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apure-virtual-abstract-class) — Object Oriented Programming, chapter 7, asked 5×

## Related in other subjects

- [Create classes from design class diagrams](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aclasses-from-design-class-diagram) — Object Oriented Analysis and Design, chapter 4, asked 2×
- [Identify conceptual classes; create domain model](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aconceptual-classes-domain-model-steps) — Object Oriented Analysis and Design, chapter 2
- [SQL: create tables for bank schema](https://www.bctengineer.com/question.md?key=semester_6%2Fdatabase_management_system%3Asql-create-tables-bank) — Database Management System, chapter 3
