Overload binary operators in C ++

Binary operators in C ++ take two parameters. You use binary operators quite often, such as addition operator (+), subtraction operator (-) and division operator (/).

The following example explains how the plus (+) operator can be overloaded in C ++. Similarly, you can overload the minus operator (-) and the division operator (/).

 #include using namespace std ; class Box { double chieudai ; // Chieu dai cua mot box double chieurong ; // Chieu rong cua mot box double chieucao ; // Chieu cao cua mot box public : double tinhTheTich ( void ) { return chieudai * chieurong * chieucao ; } void setChieuDai ( double dai ) { chieudai = dai ; } void setChieuRong ( double rong ) { chieurong = rong ; } void setChieuCao ( double cao ) { chieucao = cao ; } // Nap chong toan tu + de cong hai doi tuong Box. Box operator +( const Box & b ) { Box box ; box . chieudai = this -> chieudai + b . chieudai ; box . chieurong = this -> chieurong + b . chieurong ; box . chieucao = this -> chieucao + b . chieucao ; return box ; } }; // ham main cua chuong trinh int main ( ) { Box Box1 ; // Khai bao Box1 la cua kieu Box Box Box2 ; // Khai bao Box2 la cua kieu Box Box Box3 ; // Khai bao Box3 la cua kieu Box double thetich = 0.0 ; // Luu giu the tich cua mot box tai day // thong tin chi tiet cua box 1 Box1 . setChieuDai ( 3.0 ); Box1 . setChieuRong ( 4.0 ); Box1 . setChieuCao ( 5.0 ); // thong tin chi tiet cua box 2 Box2 . setChieuDai ( 6.0 ); Box2 . setChieuRong ( 7.0 ); Box2 . setChieuCao ( 8.0 ); // the tich cua box 1 thetich = Box1 . tinhTheTich (); cout << "The tich cua Box1 : " << thetich << endl ; // the tich cua box 2 thetich = Box2 . tinhTheTich (); cout << "The tich cua Box2 : " << thetich << endl ; // Cong hai doi tuong: Box3 = Box1 + Box2 ; // the tich cua box 3 thetich = Box3 . tinhTheTich (); cout << "The tich cua Box3 : " << thetich << endl ; return 0 ; } 

Compiling and running the above C ++ program will produce the following results:

Overload binary operators in C ++ Picture 1

According to Tutorialspoint

Previous article: Overloading a single-operator operator in C ++

Next lesson: Overload relational operator in C ++

4.5 ★ | 2 Vote

May be interested

  • Operator in C ++Operator in C ++
    an operator is an icon, which tells the compiler to perform specific mathematical and logical operations. c ++ provides many available operators.
  • ! = and! == What is the difference in JavaScript?! = and! == What is the difference in JavaScript?
    javascript includes operators like in other languages. an operator performs some operations on one or more operands (data values) and produces a result. today's article will help readers learn about 2! = and! == operators in javascript.
  • How to Convert from Binary to DecimalHow to Convert from Binary to Decimal
    the binary system is the internal language of electronic computers. if you are a serious computer programmer, you should understand how to convert from binary to decimal. this wikihow will show you how to do this. === using positional...
  • Binary Search algorithm (Binary Search)Binary Search algorithm (Binary Search)
    binany search is a fast search algorithm with runtime complexity of Ο (log n). the algorithm of binary search works based on the principle of division and rule (divide and conquer). in order for this algorithm to work correctly, the data set should be in sorted form.
  • C Shell operatorsC Shell operators
    this tutorial lists all the operators available in c shell. here, most operators are similar to those we have in the c programming language.
  • How to fix CPU overload error 99%, 100% simple, effective!How to fix CPU overload error 99%, 100% simple, effective!
    how to fix cpu overload error 99%, 100%? this article will help you solve the problem of computer overload quickly and effectively!
  • Binary Search Tree (Binary Search Tree)Binary Search Tree (Binary Search Tree)
    a binary search tree (bst search tree) is a tree in which all nodes have the following characteristics.
  • Basic Shell operatorsBasic Shell operators
    there are many operators supported by each shell. our tutorial is based on the default shell (bourne) so we are discussing bourne shell operators in this chapter.
  • The secret is hidden inside the line of binary code on the 50-pound sheet printed with Alan TuringThe secret is hidden inside the line of binary code on the 50-pound sheet printed with Alan Turing
    there will not be too much to say if there is no mysterious binary code printed in the middle of the bill.
  • Korn Shell OperatorKorn Shell Operator
    this tutorial lists all the operators available in the korn shell. here most of these operators are similar to what we have in the c program language.