Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Remember \"The Matrix\" movie where the machines \"grow\" humans to provide a so

ID: 3813216 • Letter: R

Question

Remember "The Matrix" movie where the machines "grow" humans to provide a source of power? Assuming that the two classes, a Matrix and a HumanCell, have been already defined, write code to implement a friend overloaded output operator for the Matrix class, which will output the Matrix instance's" name followed by the list of Human Cells plugged in. Matrix holds a handle to a dynamic rectangle array of Human Cells called "battery" The attributes "rows" and "columns'' hold the size of the rectangle matrix, in terms of Human Cells. Assume that the Cell lot is full-there is a Human plugged in every spot. Assume that the output operator for a HumanCell class has been provided, and that it outputs a Human's name and age, an needed for the report. Write a copy constructor the Matrix class. Assume that the implementations of Matrix and HumanCell classes exist. Write code sample which instantiates a Matrix "m1"of size 10 times 20 and then constructs a second Matrix as a copy of the first one

Explanation / Answer

Here I am seeing two questions which require two separate solutions in the given image, I will give solution for the first one.

Solution for Question 3

Here we will use the concept of Operator overloading to print information inside our objects in the desired format.

I have prepared this C++ program for you. You can save it as a file on a Linux machine and you can compile it using following commands in a terminal.

g++ filename.cpp

then

./a.out

It will show output promptly.

As we are told to assume certain parameters in question, so we have assumed data for HumanCell class.

Here is the code with the explanation as comments inside the code.

Code starts from line below

#include <iostream>
using namespace std;
class HumanCell{
   public:
   string name;
   int age;
   HumanCell(){}
   HumanCell(string n, int a)
   {
       name=n;
       age=a;
   }
   // Here' where operator overloading is in action, We have used operator overloading in Matrix class also below
   friend ostream &operator<<( ostream &output, const HumanCell &h ) {
       // We are printing attributes or HumanCell class using object h and printing it.
output << h.name <<"," << h.age;
return output;
}
};
class Matrix{
   public :
   string name;
   // Here we set number of rows and columns for our battery
   static const int rows=2,columns=3;
   HumanCell battery[rows][columns];
   // At this point we have our empty battery of 2x3
  
    Matrix(string n){
        name=n;
        // we are creating rows in our battery here
        // so let's fill this battery with HumanCell(s)
        battery[0][0].name="Ross Geller";
        battery[0][1].name="Monica Geller";
        battery[0][2].name="Rachel green";
        battery[1][0].name="Joey Tribiani";
        battery[1][1].name="Chandler bing";
        battery[1][2].name="Regina Phalange";
        battery[0][0].age=30;
        battery[0][1].age=31;
        battery[0][2].age=32;
        battery[1][0].age=33;
        battery[1][1].age=34;
        battery[1][2].age=35;
    }

// This is our operator overloaded function
// Here we have &output reference, we can give our required output to this reference so that will be printed on standard output
//We have to keep one constant reference to our class which is Matrix &m
    friend ostream &operator<<( ostream &output, const Matrix &m ) {
        // Here we are printing matrix name
        output << m.name <<endl;
        // here we are printing Cells of battery in a nested loop
        //Outer loop will iterate through rows and inner loop will iterate through columns, so each cell will be printed
        for(int i=0; i < 2; i++){
           for(int j=0; j < 3; j++){
               output << "Cell ["<<i<<"]["<<j<<"]"<<m.battery[i][j]<<endl;
           }
        }
return output;
}
};

// This is our main function here we initialised our matrix and we have given it's name as "Matrix 1", You can give anything you like
   int main()
    {
        Matrix m("Matrix 1");
        // Here we have printed object of Matrix class and it will print Matrix name and information about all cells inside battery of that object
       cout<<m<<endl;
        return 0;
    }

Code ends here


If you need any further assistance on this solution or explanation then please comment.