Create a class Resource. The class should have: Two private variables status and
ID: 3700845 • Letter: C
Question
Create a class Resource. The class should have:
Two private variables status and writeTo representing integer value either 0 or 1.
One default constructor that initializes the status and writeTo to zero.
One single parameterized constructor to initialize the writeTo variable.
Two constant accessor functions per class that return the values of status and writeTo separately
Two mutator functions per class that set the values of status and writeTo separately.
One output (member) function that outputs the resource status. The output function should be able to print on the screen or an output file.
void output(ostream &out_stream);
A friend function check_status that accesses the status variables of both classes. If status in each resource object is set to “1”, display “resource available” else display “resource unavailable”.
bool check_status(Resource &res1, Resource &res2)
// Define status checking code here.
// return true if res1 status and res2 status are both 1
// else return false.
Explanation / Answer
#include <iostream>
using namespace std;
class Resource
{
private:
int status,writeTo;
public:
Resource()
{
status = 0;
writeTo = 0;
}
Resource(int writeTo)
{
this->writeTo = writeTo;
}
int getStatus()const
{
return status;
}
int getWriteTo()const
{
return writeTo;
}
void setStatus(int status)
{
this->status = status;
}
void setWriteTo(int writeTo)
{
this->writeTo = writeTo;
}
void output(ostream &out_stream)
{
out_stream<<"Status : "<<status<<" Write To : "<<writeTo;
}
friend bool check_status(Resource &res1, Resource &res2);
};
bool check_status(Resource &res1, Resource &res2)
{
// Define status checking code here.
// return true if res1 status and res2 status are both 1
if(res1.status == 1 && res2.status == 1)
return true;
// else return false.
else return false;
}
int main() {
Resource r1(0);
Resource r2(1);
r1.output(cout); // cout is used to display on screen
if(check_status(r1,r2))
cout<<" Status of resource r1 and r2 are same ";
else
cout<<" Status of resource r1 and r2 are not same ";
return 0;
}
output:
Status : 0 Write To : 0
Status of resource r1 and r2 are not same
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.