Your programming assignment is to: Create an application that declares a class,
ID: 3829525 • Letter: Y
Question
Your programming assignment is to:
Create an application that declares a class, and in it two friend functions.
Declare and implement one friend function in such way that, you shouldn't be able to modify, but only read the class attributes.
Declare and implement the other friend function in such way that, you should be able to both read and modify the class attributes.
In the main() function, create an object of the class. Call your two friend functions using that object as an argument to demonstrate the use of the friend functions.
Add plenty of narrative comments.
Your program must be compilable and executable.
Explanation / Answer
implement og friend function:-
class Boxs {
double width;
public:
double length;
friend void printWidth( Boxs box );
void setWidth( double wid );
};
#include <iostream>
using namespace std;
class Boxs {
double width;
public:
friend void pWidth( Boxs box );
void setWidth( double wid );
};
void Box::setWidth( double wid ) {
width = wid;
}
void pWidth( Boxs box ) {
cout << "Width of box : " << box.width <<endl;
}
int main( ) {
Boxs box;
box.setWidth(78.0);
pWidth( box );
return 0;
}
output:-Width of box : 10
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.