C++ simple friend function. Create a C++ class called cube that calculates the volume of a cube. Use appropriate accessors (getters) and mutators (setters), constructors, etc. Your program must also include a friend function that will sum the volume of two cube class instances. Output the individual volumes of each of the two cube classes and then the total volume from the friend function. Example of friend funtion #include using namespace std; class Box { double width; public: friend void printWidth( Box box ); void setWidth( double wid ); }; // Member function definition void Box::setWidth( double wid ) { width = wid; } // Note: printWidth() is not a member function of any class. 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 < utilizing namespace sexually transmitted disease; class block { public: twofold side; twofold volume() { return(side*side*side); } cube(double side1) { cout << "\nA constructor is designated" << endl; side=side1; } block() { cout << "\nA default constructor is called " << endl; } ~block() { cout << "\nDestructing " << side << endl; } }; int fundamental() { block c1(2.34); block c2; cout << "\nThe side of the block is: " << c1.side << endl; cout << "\nThe volume of the main 3D square is : " << c1.volume() << endl; cout << "\nEnter the length of the subsequent block : " ; cin >> c2.side; cout << "\nThe volume of second block is : " << c2.volume() << endl; return(0); }
C++ simple friend function.
Create a C++ class called cube that calculates the volume of a cube. Use appropriate accessors (getters) and mutators (setters), constructors, etc. Your program must also include a friend function that will sum the volume of two cube class instances.
Output the individual volumes of each of the two cube classes and then the total volume from the friend function.
Example of friend funtion
#include <iostream>
using namespace std;
class Box {
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};
// Member function definition
void Box::setWidth( double wid ) {
width = wid;
}
// Note: printWidth() is not a member function of any class.
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.
printWidth( box );
return 0;
}
I do not want a incomplete non friend code such as:
#include<iostream>
utilizing namespace sexually transmitted disease;
class block
{
public:
twofold side;
twofold volume()
{
return(side*side*side);
}
cube(double side1)
{
cout << "\nA constructor is designated" << endl;
side=side1;
}
block()
{
cout << "\nA default constructor is called " << endl;
}
~block()
{
cout << "\nDestructing " << side << endl;
}
};
int fundamental()
{
block c1(2.34);
block c2;
cout << "\nThe side of the block is: " << c1.side << endl;
cout << "\nThe volume of the main 3D square is : " << c1.volume() << endl;
cout << "\nEnter the length of the subsequent block : " ;
cin >> c2.side;
cout << "\nThe volume of second block is : " << c2.volume() << endl;
return(0);
}
Step by step
Solved in 2 steps with 1 images