Answer to Question #327170 in C++ for Raushan Kumar

Question #327170

How function overloading is different from operator overloading. Also Write a program to overload binary &&(logical AND) operator using friend function


1
Expert's answer
2022-04-11T07:50:17-0400

An operator is a function with a special name and an unchangeable number of arguments. More not all operators may be overloading. In other aspects, the overloading of a function is similar to the overloading of an operator.

#include <iostream>
using namespace std;


class Integer {
public:
    Integer(int v=0) : value(v) {}
    int getValue() const { 
        return value;
    }
    friend bool operator&&(Integer, Integer);


private:
    int value;
};


// Greate Common Divisior
int gcd(int m, int n) {
    if (n == 0) {
        return m;
    }
    return gcd(n, m%n);
}


// Return true if x and y are mutually prime
bool operator&&(Integer x, Integer y) {
    return gcd(x.value, y.value) == 1;
}


int main() {
    int x, y;


    cout << "Enter two integers: ";
    cin >> x >> y;
    Integer X(x), Y(y);


    if (X&&Y) {
        cout << "The number are mutually prime";
    }
    else {
        cout << "The number are not mutually prime";
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS