Write a C++ program creates two distance class find sum and difference using friend function

Write a C++ program creates two distance class find sum and difference using friend function

Assignment
23

Create two classes dist1 (meters, centimeters) and dist2
(feet, inches). Accept two distances from the user, one in meters and
centimeters and the other in feet and inches. Find the sum and difference of
the two distances. Display the result in both (meters and centimeters) as well
as feet and inches (use friend function).

Program Code for run:

#include
<iostream>

using
namespace std;

class
Dist2;

class
Dist1

{

        float meters, centimeters;

    public:

        void accept()

        {

            cout << “* * * * * * * *
* * * * * * * * * * * * * * * * * * * * *”;

            cout << endl;

            cout << ” \t \t Accept
Dist 1 ( meters, centimeters ) \n”;

            cout << “* * * * * * * *
* * * * * * * * * * * * * * * * * * * * *”;

            cout << endl;

            cout << ” \t Accept
Meter ” << endl << “\t”;

            cin >> meters;

            cout << ” \t Accept
Centimeters ” << endl << “\t”;

            cin >> centimeters;

            cout << “* * * * * * * *
* * * * * * * * * * * * * * * * * * * * *”;

            cout << endl;

        }

       friend void sum(Dist1 d1, Dist2 d2);

       friend void difference(Dist1 d1, Dist2
d2);

};

class
Dist2

{

        float feet, inches;

    public:

        void accept() 

        {

            cout << “* * * * * * * *
* * * * * * * * * * * * * * * * * * * * *”;

            cout << endl;

            cout << ” \t \t Accept
Dist 2 ( feet, inches ) \n”;

            cout << “* * * * * * * *
* * * * * * * * * * * * * * * * * * * * *”;

            cout << endl;

            cout << ” \t Accept feet
” << endl << “\t”;

            cin >> feet;

            cout << ” \t Accept
inches ” << endl << “\t”;

            cin >> inches;

            cout << “* * * * * * * *
* * * * * * * * * * * * * * * * * * * * *”;

            cout << endl;

        }

        friend void sum(Dist1 d1, Dist2 d2);

        friend void difference(Dist1 d1, Dist2
d2);

};

void
sum(Dist1 d1, Dist2 d2)

{

    cout << “* * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *”<< endl;

    cout << ” \t \t \t Addition of
Dist1 and Dist2 ” << endl;

    cout << ” \t Dist1 (meters,
centimeters) \t Dist2 (feet, inches) ” << endl;

    cout << ” \t \t \t Addition
(feet, centimeters)” << endl;

    cout << “* * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *”<< endl;

    cout << “\t Feet = ”
<< (d1.meters * 3.28) + (d2.feet) << endl;

    cout << “\t centimeters = ”
<< (d1.centimeters) + (d2.inches * 2.54) << endl;

    cout << “* * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *”<< endl;

}

void
difference(Dist1 d1, Dist2 d2)

{

    cout << “* * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *”<< endl;

    cout << ” \t \t \t Difference of
Dist1 and Dist2 ” << endl;

    cout << ” \t Dist1 (meters,
centimeters) \t Dist2 (feet, inches) ” << endl;

    cout << ” \t \t \t Difference
(feet, centimeters)” << endl;

    cout << “* * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *”<< endl;

    if((d1.meters * 3.28) > (d2.feet))

        cout << “\t Feet = ”
<< (d1.meters * 3.28) – (d2.feet) << endl;

    else

        cout << “\t Feet = ”
<< (d2.feet) – (d1.meters * 3.28) << endl;

    if((d1.centimeters) > (d2.inches *
2.54))

        cout << “\t centimeters =
” << (d1.centimeters) – (d2.inches * 2.54) << endl;

    else

        cout << “\t centimeters =
” << (d2.inches * 2.54) – (d1.centimeters) << endl;

    cout << “* * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *”<< endl;

}

int
main()

{

            int
n;

            Dist1
d1;

            Dist2 d2;

            d1.accept();

            d2.accept();

   while(1)

   {

            cout
<< “* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*”<< endl;

            cout
<< ” \t Find the sum and difference of the two distances ”
<< endl;

            cout
<< ” \t \t Using friend function ” << endl;

            cout
<< “* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*”<< endl;

            cout
<< ” \t Addition (Sum) : 1 \t\t Difference : 2 \n”;

            cout
<< ” \t Distance Accept Again : 3 \t Exit : Other \n”;

            cout
<< ” \t\t Choose Appropriate option \n”;

            cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
” << endl;

            cout
<< ” \t “;

            cin
>> n;

            switch(n)

            {

                        case
1: sum(d1, d2);

                                      break;

                        case
2: difference(d1, d2);

                                      break;

                        case
3: d1.accept();

                                      d2.accept();

                                      break;

                        default:
cout << ” Thank You to Use this Program !”;

                                      exit(0);

            }

  }   

}

Output
of Program:

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *

                        Accept Dist 1 ( meters, centimeters )

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *

                Accept Meter

               10

                Accept Centimeters

               7

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *

                        Accept Dist 2 ( feet, inches )

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *

                Accept feet

               23

                Accept inches

               5

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                Find the sum and difference of the two
distances

                               Using friend function

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                Addition (Sum) : 1                             Difference : 2

                Distance Accept Again : 3               Exit : Other

                                Choose Appropriate option

               *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

                1

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                                      Addition of Dist1 and Dist2

                Dist1 (meters, centimeters)              Dist2 (feet, inches)

                                      Addition (feet, centimeters)

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                Feet = 55.8

                centimeters =
19.7

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                Find the sum and difference of the two
distances

                               Using friend function

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                Addition (Sum) : 1                             Difference : 2

                Distance Accept Again : 3               Exit : Other

                                Choose Appropriate option

               *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

                2

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                                       Difference of Dist1 and Dist2

                Dist1 (meters, centimeters)              Dist2 (feet, inches)

                                       Difference (feet, centimeters)

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                Feet = 9.8

                centimeters =
5.7

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                Find the sum and difference of the two distances

                               Using friend function

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                Addition (Sum) : 1                             Difference : 2

                Distance Accept Again : 3               Exit : Other

                                Choose Appropriate option

               *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

                3

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *

                               Accept Dist 1 ( meters, centimeters )

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *

                Accept Meter

               7

                Accept Centimeters

               6

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *

                               Accept Dist 2 ( feet, inches )

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *

                Accept feet

               8

                Accept inches

               8

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                Find the sum and difference of the two
distances

                               Using friend function

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                Addition (Sum) : 1                             Difference : 2

                Distance Accept Again : 3               Exit : Other

                                Choose Appropriate option

               *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

                1

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                                       Addition of Dist1 and Dist2

                Dist1 (meters, centimeters)              Dist2 (feet, inches)

                                      Addition (feet, centimeters)

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                Feet = 30.96

                centimeters =
26.32

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                Find the sum and difference of the two
distances

                               Using friend function

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                Addition (Sum) : 1                             Difference : 2

                Distance Accept Again : 3               Exit : Other

                                Choose Appropriate option

               *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

                2

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                                        Difference of Dist1 and Dist2

                Dist1 (meters, centimeters)              Dist2 (feet, inches)

                                        Difference (feet, centimeters)

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                Feet = 14.96

                centimeters =
14.32

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                Find the sum and difference of the two
distances

                               Using friend function

               * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *

                Addition (Sum) : 1                             Difference
: 2

                Distance Accept Again : 3               Exit : Other

                                Choose Appropriate option

               *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

                4

               Thank
You to Use this Program !

Source link

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top