Write a program in C++ to overload the binary operator “+” (addition)

Write a program in C++ to overload the binary operator “+” (addition)

Assignment
26

Write a program in C++ to overload the binary operator “+”
(addition).

Program Code for run:                       

#include
<iostream>

#include
<iomanip>

using
namespace std;

class
Box

{

            int
l, w, h;

public:

            Box
(int l = 0, int w = 0, int h = 0)

            {

                        this->l
= l;

                        this->w = w;

                        this->h
= h;

            }

            void
operator+( Box b)

            {

                        Box
b1;

                        b1.l
= this->l + b.l;

                        b1.w
= this->w + b.w;

                        b1.h
= this->h + b.h;

   

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

              cout << endl;

              cout << ” \t   Addition of Box ” << endl;

              cout << “      Using + Operator overload ” <<
endl;

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

              cout <<endl;

              cout << ” \t \t      B  
O   X ” << endl;

              cout << ” – – – – – – – – – –
– – – – – – – – – – – – – – – – – – – – “;

              cout << endl;

              cout << ” \t “<<
setw(6) << ” Length: “;

              cout << ” \t “<<
setw(6) << ” Width: “;

              cout << ” \t “<<
setw(6) << ” Height: ” << endl;

 

              cout << “  T1″;

              cout << ” \t “<<
setw(6) << this->l ;

              cout << ” \t “<<
setw(6) << this->w ;

              cout << ” \t “<<
setw(6) << this->h << endl;

 

              cout << “  + 
” << endl;

 

              cout << “  T2″;

              cout << ” \t “<<
setw(6) << b.l ;

              cout << ” \t “<<
setw(6) << b.w ;

              cout << ” \t “<<
setw(6) << b.h << endl;

 

              cout << ” – – – – – – – – – –
– – – – – – – – – – – – – – – – – – – – “;

              cout << endl;

 

              cout << “Result”;

              cout << ” \t ” <<
setw(6) << b1.l ;

              cout << ” \t “<<
setw(6) << b1.w ;

              cout << ” \t “<<
setw(6) << b1.h << endl;

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

              cout << endl;

           }

};

int
main ()

{

  Box b;

  int l, w, h;

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

  cout << ” \t \t Accept Box ”
<< endl;

  cout << “    Box Parameters: Length, Width and
Height” << endl;

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

  cout << ” \t Accept Box Parameters
for Box 1 ” << endl;

  cout << “    Box Parameters: Length, Width and
Height” << endl;

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

  cout << ” \t Accept Length for Box
B1 ” << endl << ” \t “;

  cin >> l;

  cout << ” \t Accept Width for Box
B1 ” << endl << ” \t “;

  cin >> w;

  cout << ” \t Accept Height for Box
B1 ” << endl << ” \t “;

  cin >> h;

  Box b1 (l, w, h);

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

  cout << ” \t Accept Box Parameters
for Box 2 ” << endl;

  cout << ”    Box Parameters: Length, Width and
Height” << endl;

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

  cout << ” \t Accept Length for Box
B2 ” << endl << ” \t “;

  cin >> l;

  cout << ” \t Accept Width for Box
B2 ” << endl << ” \t “;

  cin >> w;

  cout << ” \t Accept Height for Box
B2 ” << endl << ” \t “;

  cin >> h;

  Box b2 (l, w, h);

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

  b1 + b2;

  return 0;

}

Output
of Program:

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

                         Accept Box

    Box Parameters: Length, Width and Height

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

             Accept Box Parameters for Box 1

    Box Parameters: Length, Width and Height

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

             Accept Length for Box B1

             1

             Accept Width for Box B1

             2

             Accept Height for Box B1

             3

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

             Accept Box Parameters for Box 2

    Box Parameters: Length, Width and Height

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

             Accept Length for Box B2

             3

             Accept Width for Box B2

             2

             Accept Height for Box B2

             1

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

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

               Addition of Box

      Using + Operator overload

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

                              B  
O   X

 – – – – – – – – – – – – – – – – – – – – – – –
– – – – – – –

              Length: 
Width:  Height:

      T1           1            2
           3

       + 

      T2           3            2
           1

 – – – – – – – – – – – – – – – – – – – – – – –
– – – – – – –

Result           4            4
           4

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

Source link

Leave a Comment

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

Scroll to Top