Trusted by millions of Kenyans
Study resources on Kenyaplex

Get ready-made curriculum aligned revision materials

Exam papers, notes, holiday assignments and topical questions – all aligned to the Kenyan curriculum.

Imagine a situation in which two classes, called pr1 and pr2, shown below, share one printer.Further imagine that other parts of your program need to...

Imagine a situation in which two classes, called pr1 and pr2, shown below, share one printer.Further imagine that other parts of your program need to when the printer is in use by an object of either of these two classes. Create a function in C++ called inuse() that returns true when the printer is being used by either and false otherwise. Make this function a friend of both pr1 and pr2.
class pri1 {
int printing; //...
public:
pr1 () {printing = 0}
void set_print (int status) {printing = status;}
// ...
};
class pr2 {
int printing;
// ...
public:
pr2 () {printing = 0;}
void set_prt status) {printing = status; }
// ...
};

Answers


Davis

#include
using namespace std;
class pr2; //forward declaration
class pr1 {
int printing; ...
public:
pr1() {printing = 0; }
void set_print(int status) {printing = status; }
// ...
friend int inuse (pr1 o1, pr2 o2);
};
clr2 {
int printing;
// ...
pr2 () {printing = 0; }
void set_print (int status) { printing = status ; }
// ...
friend int inuse(pr1 o1, pr2 o2;
// Return true if printer is in use.
int inuse(pr1 o1, pr2 o2)
{
if(o1.printing || o2.printing) return 1;
else return 0;
}
int main ()
{
pr
pr2 p2;
if (! inuse(p1, p2)) cout << "Printer idle\n";
cout << "Setting p1 to printing ...\n";
p1.set_print(1);
if (inuse(p1, p2)) cout << "Now, prier in use. \n";
cout << "Turn off p1...\n";
p1.set_print (0);
if(! inuse(p1, p2)) cout << "Printer idle\n";
cout << "Turn on p2...\n";
p2.set_pri);
if (inuse(p1, p2)) cout << "Now, printer in use.\";
return 0;

Githiari answered the question on May 29, 2018 at 17:45

Answer Attachments

Exams With Marking Schemes

Related Questions