#include using namespace std; class Box { double width; public: friend void printWidth( Box box ); //declare friends void setWidth( double wid ); }; // Member function definition void Box::setWidth( double wid ) { width = wid; } // Note: printWidth() is not a member function of any class. // it is written directly without Box:: void printWidth( Box box ) { /* Because printWidth() is a friend of Box, it can directly access any member of this class */ cout << "Width of box : " << box.width <
#include <iostream>
using namespace std;
class Box {
double width;
public:
friend void printWidth( Box box ); //declare friends
void setWidth( double wid );
};
// Member function definition
void Box::setWidth( double wid ) {
width = wid;
}
// Note: printWidth() is not a member function of any class.
// it is written directly without Box::
void printWidth( Box box ) {
/* Because printWidth() is a friend of Box, it can
directly access any member of this class */
cout << "Width of box : " << box.width <<endl;
}
// Main function for the program
int main() {
Box box;
// set box width without member function
box.setWidth(10.0);
// Use friend function to print the wdith.
// call directly that can access private variables of box.
printWidth( box );
return 0;
}
-By using above code To make points and length parameters of triangles and ellipses to be private. Make a counter for how many triangle-objects that are created. The counter is a static variable.
- Display the areas and the counting values.
- Use a friend function and a "const" class.

Step by step
Solved in 2 steps









