Wednesday, March 28, 2018

Write a C++ program to illustrate binary operator as a non-member function.


AIM: Write a C++ program to illustrate binary operator as a non-member  function.
THEORY:
A binary operator is an operator that operates on two operands and manipulates them to return a result. Operators are represented by special characters or by keywords and provide an easy way to compare numerical values or character strings.

Binary operators are presented in the form:

Operand1 Operator Operand2
SOURCE CODE:
#include<iostream>
using namespace std;
class FBOP{
            int x,y;
            public:
            FBOP(){
            }
            FBOP(int a,int b)        {
                        x=a;
                        y=b;
            }         
            void show()     {
                        cout<<"\n x= "<<x<<"\n "<<"\n y= "<<y;
            }
            friend FBOP operator+(FBOP ob1,FBOP ob2);
};
FBOP operator+(FBOP ob1,FBOP ob2){
            FBOP temp;
            temp.x=ob1.x+ob2.x;
            temp.y=ob1.y+ob2.y;
            return temp;
}
int main(){
            FBOP ob1(1,2),ob2(10,20),ob3;
            ob3=ob1+ob2;
            cout<<"\n Values of ob1:";
            ob1.show();
            cout<<"\n Values of ob2:";
            ob2.show();
            cout<<"\n Values of ob3";
            ob3.show();
}

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home