Page 151 - DCAP404 _Object Oriented Programming
P. 151
Object-oriented Programming
Notes You should see the following output.
S: 11 -21 -31
-S: -11 21 31
Notes Note that how only one argument is passed to the friend function. The operator
function declared as friend is not the property of the class. Hence when we define this
friend function, we should pass the object of the class on which it operates.
Self Assessment
Fill in the blanks:
6. By overloading operators, we can control or define how an operator should operate on
……………….. with respect to a class.
7. Overloaded operators must either be a ……………….. class member function or a global
function.
8. Operators obey the precedence, grouping, and number of operands dictated by their
typical use with………………. .
9. The positive (+), negative (–) and logical not (!) operators all are unary operators, which
means they only operate on one…………………... .
7.4 Overloading Binary Operators
Binary Operators are operators, which require two operands to perform the operation. When
they are overloaded by means of member function, the function takes one argument, whereas it
takes two arguments in case of friend function. This will be better understood by means of the
following program.
The following program creates two objects of class integer and overloads the + operator to add
two object values.
#include <iostream.h>
#include <como.h>
class integer
{
private:
int val;
public:
integer();
integer(int one );
integer operator+ (integer objb);
void disp();
144 LOVELY PROFESSIONAL UNIVERSITY