Overload the operator for the class JarType, as a friend function so that the code in the “main” function would work. class JarType { public: JarType (int n) private: int numUnits: }: void main () { JarType j1(5), j2(10): if (j1 != j2) cout
Expert Answer
46
#include<iostream>
using namespace std;
class JarType {
public:
JarType(int n){
numUnits = n;
}
friend bool operator !=(JarType j1, JarType j2){
if (j1.getNumUnits() == j2.getNumUnits())
return true;
else
return false;
}
int getNumUnits(){
return numUnits;
}
private:
int numUnits;
};
int main()
{
JarType j1(5), j2(10);
if (j1 != j2)
cout << “Equal”;
else
cout << “Not Equal”;
return 0;
}
47. The output will be “An exception has occurred”