Question & Answer: Overload the operator for the class JarType, as a friend function so that the code in the "main"…..

46. overload the operator for the class Jartype, as a friend function so that the code i the main function would work. class JarType public: JarType (int n) private: int numUnits void main JarType j1 (5), 32(10) cout Jars not equal.n; << are cout ss Jars are equal n 47. What is the output of the following code if the system is unable to allocate the requested amount of memory #include iostream» int main() try t char p - new char (1000]; if (p 0 throw M catch ( char cout << Out of memory; return 1 ) catch ( char f cout << Not enough memory; return 3; ) catch exception) f cout << Memory is insufficient; return 2: catch 《 ) { cout << An exception has occurred. return 4; } return 0; Page 8

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”

Still stressed from student homework?
Get quality assistance from academic writers!