Aim : Create two classes DM and DB which store the value of distances. DM stores distances in
meters and centimeters and DB in feet and inches. Write a program that can read values
for the class objects and add one object of DM with another object of DB. Use a friend
function to carry out the addition operation. The object that stores the results maybe a
DM object or DB object, depending on the units in which the results are required. The
display should be in the format of feet and inches or meters and centimeters depending on
the object on display.
Theory Friend Functions :
In Object-Oriented Programming (OOP), friend functions allow one class to access private and protected members of another class. This program demonstrates how to add distances stored in different unit systems (meters & centimeters vs. feet & inches) using a friend function.
Using Friend Function:
Since DM and DB are separate classes, a friend function is used to perform the addition of two objects.
The function accesses private members of both classes.
Conversion Between Units:
1 inch = 2.54 cm
1 foot = 30.48 cm
The friend function converts one unit to another before addition.
User Choice for Output Format:
The user selects whether to display the sum in meters & centimeters or feet & inches.
Syntax:
friend class class_name;
Program :
Conclusion : Hence we have performed our
practical successfully