Hey everyone, I am posting my code below in which I need somehelp in the = opera
ID: 3617947 • Letter: H
Question
Hey everyone, I am posting my code below in which I need somehelp in the = operator. Please help me out. Here is my code.#include <iostream> #include <cstdlib> #include <iomanip> using namespace std; const int ROWS = 6; const int COLS = 6;
class TwoD{ public: TwoD(); TwoD(int r, int c); TwoD(const TwoD& twoDObject); void setRowCols(int r, int c); void keyBoardinput(); void display(); friend TwoD operator +(const TwoD&b,const TwoD &c ); TwoD& operator =(const TwoD&b); //~TwoD(); private: int rows, columns, a[ROWS][COLS]; }; TwoD::TwoD(){ rows=ROWS; columns=COLS; }
TwoD::TwoD(int r, int c){ if ( r > ROWS || c > COLS ) { cout << "Current max is" << r << " for rows, and"; cout << "Current max is" << c << " for cols.Exiting..."; exit( 1 ); }
rows = r; columns = c; } void TwoD::setRowCols(int r , int c){ if ( r > ROWS || c > COLS ) { cout << "Current max is" << ROWS << " for rows, and"; cout << "Current max is" << COLS << " for cols.Exiting..."; exit( 1 ); } rows = r; columns = c; } void TwoD::keyBoardinput(){
for ( int i = 0; i < rows; i++ ) { for ( int j = 0; j < columns; j++ ) { cin >> a[ i ] [ j ]; } }
}
void TwoD::display(){
for ( int i = 0; i < rows; i++ ) { for ( int j = 0; j < columns; j++) cout << setw( 5 ) << a[i ][ j ] << " "; cout << endl; }
} TwoD operator +(const TwoD &b,const TwoD &c ){ TwoD x; for(int i=0;i<b.rows;i++) for(int j=0;j<c.columns;j++) x.a[i][j]=b.a[i][j]+c.a[i][j]; return x; } TwoD& TwoD::operator=(const TwoD& b){ TwoD d; for(int i=0;i<b.rows;i++) for(int j=0;i<b.columns;j++) d.a[i][j]=b.a[i][j]; return d; } int main(){ TwoD a; TwoD b; TwoD c; int theRows1, theCols1, theRows2, theCols2,ans; cout<<"Enter the row and column dimensionsof the array"<<endl; cin>>theRows1>>theCols1; a.setRowCols(theRows1,theCols1); cout<<"Enter "<<theRows1<<" rowsof "<<theCols1<<" doubles each"<<endl; a.keyBoardinput(); cout<<"Echoing the 2 dim array, matrix1"<<endl; a.display(); cout<<"Enter the row and column dimensionsof the array"<<endl; cin>>theRows2>>theCols2; b.setRowCols(theRows2,theCols2); cout<<"Enter "<<theRows2<<" rowsof "<<theCols2<<" doubles each"<<endl; b.keyBoardinput(); cout<<"Echoing the 2 dim array, matrix2"<<endl; b.display(); cout<<endl<<endl; //c=a+b; c=a; cout<<"Echoing the 2 dim array, sum ofmatrix 1 and 2 "<<endl<<"rows"<<theRows1<<" cols"<<theCols1<<endl<<endl; c.display(); system("pause"); return 0; }
I am posting my code below in which I need somehelp in the = operator. Please help me out. Here is my code.
#include <iostream> #include <cstdlib> #include <iomanip> using namespace std; const int ROWS = 6; const int COLS = 6;
class TwoD{ public: TwoD(); TwoD(int r, int c); TwoD(const TwoD& twoDObject); void setRowCols(int r, int c); void keyBoardinput(); void display(); friend TwoD operator +(const TwoD&b,const TwoD &c ); TwoD& operator =(const TwoD&b); //~TwoD(); private: int rows, columns, a[ROWS][COLS]; }; TwoD::TwoD(){ rows=ROWS; columns=COLS; }
TwoD::TwoD(int r, int c){ if ( r > ROWS || c > COLS ) { cout << "Current max is" << r << " for rows, and"; cout << "Current max is" << c << " for cols.Exiting..."; exit( 1 ); }
rows = r; columns = c; } void TwoD::setRowCols(int r , int c){ if ( r > ROWS || c > COLS ) { cout << "Current max is" << ROWS << " for rows, and"; cout << "Current max is" << COLS << " for cols.Exiting..."; exit( 1 ); } rows = r; columns = c; } void TwoD::keyBoardinput(){
for ( int i = 0; i < rows; i++ ) { for ( int j = 0; j < columns; j++ ) { cin >> a[ i ] [ j ]; } }
}
void TwoD::display(){
for ( int i = 0; i < rows; i++ ) { for ( int j = 0; j < columns; j++) cout << setw( 5 ) << a[i ][ j ] << " "; cout << endl; }
} TwoD operator +(const TwoD &b,const TwoD &c ){ TwoD x; for(int i=0;i<b.rows;i++) for(int j=0;j<c.columns;j++) x.a[i][j]=b.a[i][j]+c.a[i][j]; return x; } TwoD& TwoD::operator=(const TwoD& b){ TwoD d; for(int i=0;i<b.rows;i++) for(int j=0;i<b.columns;j++) d.a[i][j]=b.a[i][j]; return d; } int main(){ TwoD a; TwoD b; TwoD c; int theRows1, theCols1, theRows2, theCols2,ans; cout<<"Enter the row and column dimensionsof the array"<<endl; cin>>theRows1>>theCols1; a.setRowCols(theRows1,theCols1); cout<<"Enter "<<theRows1<<" rowsof "<<theCols1<<" doubles each"<<endl; a.keyBoardinput(); cout<<"Echoing the 2 dim array, matrix1"<<endl; a.display(); cout<<"Enter the row and column dimensionsof the array"<<endl; cin>>theRows2>>theCols2; b.setRowCols(theRows2,theCols2); cout<<"Enter "<<theRows2<<" rowsof "<<theCols2<<" doubles each"<<endl; b.keyBoardinput(); cout<<"Echoing the 2 dim array, matrix2"<<endl; b.display(); cout<<endl<<endl; //c=a+b; c=a; cout<<"Echoing the 2 dim array, sum ofmatrix 1 and 2 "<<endl<<"rows"<<theRows1<<" cols"<<theCols1<<endl<<endl; c.display(); system("pause"); return 0; } #include <iostream> #include <cstdlib> #include <iomanip> using namespace std; const int ROWS = 6; const int COLS = 6;
class TwoD{ public: TwoD(); TwoD(int r, int c); TwoD(const TwoD& twoDObject); void setRowCols(int r, int c); void keyBoardinput(); void display(); friend TwoD operator +(const TwoD&b,const TwoD &c ); TwoD& operator =(const TwoD&b); //~TwoD(); private: int rows, columns, a[ROWS][COLS]; }; TwoD::TwoD(){ rows=ROWS; columns=COLS; }
TwoD::TwoD(int r, int c){ if ( r > ROWS || c > COLS ) { cout << "Current max is" << r << " for rows, and"; cout << "Current max is" << c << " for cols.Exiting..."; exit( 1 ); }
rows = r; columns = c; } void TwoD::setRowCols(int r , int c){ if ( r > ROWS || c > COLS ) { cout << "Current max is" << ROWS << " for rows, and"; cout << "Current max is" << COLS << " for cols.Exiting..."; exit( 1 ); } rows = r; columns = c; } void TwoD::keyBoardinput(){
for ( int i = 0; i < rows; i++ ) { for ( int j = 0; j < columns; j++ ) { cin >> a[ i ] [ j ]; } }
}
void TwoD::display(){
for ( int i = 0; i < rows; i++ ) { for ( int j = 0; j < columns; j++) cout << setw( 5 ) << a[i ][ j ] << " "; cout << endl; }
} TwoD operator +(const TwoD &b,const TwoD &c ){ TwoD x; for(int i=0;i<b.rows;i++) for(int j=0;j<c.columns;j++) x.a[i][j]=b.a[i][j]+c.a[i][j]; return x; } TwoD& TwoD::operator=(const TwoD& b){ TwoD d; for(int i=0;i<b.rows;i++) for(int j=0;i<b.columns;j++) d.a[i][j]=b.a[i][j]; return d; } int main(){ TwoD a; TwoD b; TwoD c; int theRows1, theCols1, theRows2, theCols2,ans; cout<<"Enter the row and column dimensionsof the array"<<endl; cin>>theRows1>>theCols1; a.setRowCols(theRows1,theCols1); cout<<"Enter "<<theRows1<<" rowsof "<<theCols1<<" doubles each"<<endl; a.keyBoardinput(); cout<<"Echoing the 2 dim array, matrix1"<<endl; a.display(); cout<<"Enter the row and column dimensionsof the array"<<endl; cin>>theRows2>>theCols2; b.setRowCols(theRows2,theCols2); cout<<"Enter "<<theRows2<<" rowsof "<<theCols2<<" doubles each"<<endl; b.keyBoardinput(); cout<<"Echoing the 2 dim array, matrix2"<<endl; b.display(); cout<<endl<<endl; //c=a+b; c=a; cout<<"Echoing the 2 dim array, sum ofmatrix 1 and 2 "<<endl<<"rows"<<theRows1<<" cols"<<theCols1<<endl<<endl; c.display(); system("pause"); return 0; }
Explanation / Answer
Could you please highlight the specific line? Thanks!