How function overloading is different from operator overloading. Also Write a program to overload binary &&(logical AND) operator using friend function
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;
}
Comments
Leave a comment