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); }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

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);

}

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY