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

[C++] I have a question about class inheritance. In my following code the class

ID: 3885540 • Letter: #

Question

[C++]

I have a question about class inheritance. In my following code the class "RREF" inherits from the class "Matrix", however, when I try to access information inside the "Matrix" class through the "RREF" class in my main function, it displays an error message, saying that it wasn't declared in this scope. I will mark the section where it happens. How can I fix this so it properly inherits it?

#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

class Matrix{
  
public :

double mat[3][4] = {{5, -6, -7, 7},
{3, -2, 5, -17},
{2, 4, -3, 29}};


};
/*-------------------------------------------------------
CLASS NAME: RREF (Row Reduced Echelon Form)

DESCRIPTION: This class inherits from the Matrix class and holds the functions to do the RREF calculations.
-------------------------------------------------------*/
class RREF: public Matrix{
public:
  

  
/*-------------------------------------------------------
FUNCTON NAME: printmat
PARAMETERS: double mat[][4]
RETURN TYPE:
DESCRIPTION: This function will print out the Matrix
-------------------------------------------------------*/
void printmat(double mat[][4]){
int p=3;
int q=4;

for (int i=0; i<p; i++) {
for (int j=0; j<q; j++) {
cout << setw(7) << setprecision(4) << mat[i][j] <<" ";
}
cout << endl;
}
cout << endl;
}
/*-------------------------------------------------------
FUNCTON NAME: RowRed
PARAMETERS: double mat[][4]
RETURN TYPE:
DESCRIPTION: This function will do the row reduced echelon form calculation
-------------------------------------------------------*/   
void RowRed(double mat[][4]){
const int nrows = 3; // number of rows
const int ncols = 4; // number of columns

int lead = 0;

while (lead < nrows) {
  double d, m;

for (int r = 0; r < nrows; r++) { // for each row ...
/* calculate divisor and multiplier */
d = mat[lead][lead];
m = mat[r][lead] / mat[lead][lead];

for (int c = 0; c < ncols; c++) { // for each column ...
if (r == lead)
mat[r][c] /= d; // make pivot = 1
else
mat[r][c] -= mat[lead][c] * m; // make other = 0
}
}

lead++;
printmat(mat);
}
}
};

int main() {

bool choice;
char input;
  
RREF ob;


//Displaying a menu, so that the user can choose what he wants to do
choice = false;

while(!choice) {
cout << " " << endl;
cout <<"** Choose from the following **"<< endl;
cout << " " << endl;
cout << "r - RREF" << endl;
cout << "q - Quit" << endl;
cout << " " << endl;
cout << "Note: Choosing 'i' or 'd' will only apply to Matrix A" << endl;
cout << "Note: Currently, RREF is not related to Matrix A or B, because the array dimensions don't fit. Instead it will do the calculation on a preset Matrix" << endl;
cout << " " << endl;
cout << "Enter your choice: ";
cin >> input;
cout << endl;
//A switch to handle the user inputs
switch(input){
  
  
case 'r': case 'R': ob.printmat(mat); // Here's where the error happens it says that "mat" wasn't declared in the scope, but I thought I inherited that information.
ob.RowRed(mat);
break;
  
case 'q': case 'Q': exit(0);
  
  
  
default:cout<<" ERROR: Please enter valid input! ";
}

}

return 0;
}

Please, how do I fix this so it inherits it properly. I really need to understand this concept.

Explanation / Answer

Explaination:

The error was coming becauese you are accessing the class variable mat outside the class directly without using the object.

if we want to access the class variable outside the class we need to use the object in below case we have to use like ob.mat .

//Please see the modified code

#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

class Matrix{

public :

double mat[3][4] = {{5, -6, -7, 7},
                      {3, -2, 5, -17},
                      {2, 4, -3, 29}};


};
/*-------------------------------------------------------
CLASS NAME: RREF (Row Reduced Echelon Form)

DESCRIPTION: This class inherits from the Matrix class and holds the functions to do the RREF calculations.
-------------------------------------------------------*/
class RREF: public Matrix{
public:



/*-------------------------------------------------------
FUNCTON NAME: printmat
PARAMETERS: double mat[][4]
RETURN TYPE:
DESCRIPTION: This function will print out the Matrix
-------------------------------------------------------*/
void printmat(double mat[][4]){
    int p=3;
    int q=4;

      for (int i=0; i<p; i++) {
        for (int j=0; j<q; j++) {
          cout << setw(7) << setprecision(4) << mat[i][j] <<" ";
        }
        cout << endl;
      }
    cout << endl;
}
/*-------------------------------------------------------
FUNCTON NAME: RowRed
PARAMETERS: double mat[][4]
RETURN TYPE:
DESCRIPTION: This function will do the row reduced echelon form calculation
-------------------------------------------------------*/
void RowRed(double mat[][4]){
    const int nrows = 3; // number of rows
    const int ncols = 4; // number of columns

    int lead = 0;

      while (lead < nrows) {
        double d, m;

          for (int r = 0; r < nrows; r++) { // for each row ...
          /* calculate divisor and multiplier */
            d = mat[lead][lead];
            m = mat[r][lead] / mat[lead][lead];

              for (int c = 0; c < ncols; c++) { // for each column ...
                if (r == lead)
                  mat[r][c] /= d; // make pivot = 1
                else
                  mat[r][c] -= mat[lead][c] * m; // make other = 0
              }
          }

lead++;
printmat(mat);
      }
}
};

int main() {

bool choice;
char input;

RREF ob;


//Displaying a menu, so that the user can choose what he wants to do
choice = false;

while(!choice) {
cout << " " << endl;
cout <<"** Choose from the following **"<< endl;
cout << " " << endl;
cout << "r - RREF" << endl;
cout << "q - Quit" << endl;
cout << " " << endl;
cout << "Note: Choosing 'i' or 'd' will only apply to Matrix A" << endl;
cout << "Note: Currently, RREF is not related to Matrix A or B, because the array dimensions don't fit. Instead it will do the calculation on a preset Matrix" << endl;
cout << " " << endl;
cout << "Enter your choice: ";
cin >> input;
cout << endl;
//A switch to handle the user inputs
    switch(input){

    
      case 'r': case 'R': ob.printmat(ob.mat); // Here's where the error happens it says that "mat" wasn't declared in the scope, but I thought I inherited that information.
                                  ob.RowRed(ob.mat);
        break;

      case 'q': case 'Q': exit(0);
    
    

      default:cout<<" ERROR: Please enter valid input! ";
    }

}

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote