Get premium membership and access questions with answers, video lessons as well as revision papers.

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


Next: Given this class; class planet { int moons; double dist_from_sun; // in miles double diameter; double mass; public: //... double get_ () { return disfrom_sun; } }; Create a function called light() using c++...
Previous: Explain what is wrong with the following program and then fix it. // This program contains an error. #include #include using namespace std; class myclass { int *p; public: myclass(int i); ~myclass()...

View More Computer Science Questions and Answers | Return to Questions Index


Learn High School English on YouTube

Related Questions