Question

Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor...

Finish the class Matrix.h:

class Matrix
   
{
public:
    Matrix();                                           //default constructor
    ~Matrix();                                          //destructor
    Matrix(const Matrix &);                             //copy constror
    Matrix(int row, int col);                            
    Matrix operator+(const Matrix &)const;              //overload operator“+”
    Matrix& operator=(const Matrix &);                  //overload operator“=”
    Matrix transpose()const;                            //matrix transposition
    void display()const;                                //display the data   
private:
    int row;                                            //row
    int col;                                            //column
    int** mat;                                          //to save the matrix
};

//main.cpp

#include <iostream>
#include"Matrix.h"
using namespace std;
int main() {
int row, col;
cout << "input the row and the col for Matrix a, b" << endl;
cin >> row >> col;
Matrix a(row, col), b(row, col), c(a), d;
cout << endl << "Matrix a:" << endl;
a.display();
cout << endl << "Matrix b:" << endl;
b.display();
c = a + b;
cout << endl << "Matrix c = Matrix a + Matrix b :" << endl;
c.display();
cout << endl << "Matrix a transpose to Matrix d:" << endl;
d = a.transpose();
d.display();
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Matrix.h

#include<iostream>
using namespace std;
class Matrix
{
    private:
        int row;
        int col;
        int **mat;
    public:
       Matrix();
       ~Matrix();
       Matrix(const Matrix &);
       Matrix(int row,int col);
       Matrix operator + (const Matrix &)const;
       Matrix& operator = (const Matrix &);
           Matrix Transpose() const;
           void display() const;
           void input() const;
};
//default constructor it assign 10 to row and column
Matrix :: Matrix()
{
   row=10;
   col=10;
   int i;
   //allocate memory for row
       mat=new int*[10];
   //alocate memory for columns
   for(int i=0;i<10;i++)
   {
       mat[i]=new int[10];
   }
}
//Destructor
Matrix :: ~Matrix()
{
    delete mat;
}
//parameterized constructors
Matrix :: Matrix (const Matrix &m)
{
    int i,j;
    //assign row and column
    this->row = m.row;
    this->col = m.col;
    //allocate memory for row
mat=new int*[row];
    //alocate memory for columns
   for( i=0;i<row;i++)
   {
       mat[i]=new int[col];
   }
}
  
//parameterized constructor
Matrix :: Matrix(int r,int c)
{
   row=r;
   col=c;
       //allocate memory for row
   mat=new int*[row];
int i;
    //alocate memory for columns
   for(int i=0;i<row;i++)
   {
       mat[i]=new int[col];
   }
}
//input method
void Matrix :: input()const
{
    int i,j;
    cout<<endl<<"Enter"<<row*col<<" elements";
    for(i=0;i<row;i++)
    for(j=0;j<col;j++)
    cin>>mat[i][j];
}
//display method
void Matrix :: display()const
{
    int i,j;
    cout<<endl<<"Matrix elements : \n";
    for(i=0;i<row;i++)
    {
       for(j=0;j<col;j++)
    cout<<" "<<mat[i][j];
    cout<<endl;
       }
   
}
//operator overloading add two matrices
Matrix Matrix :: operator + (const Matrix &m)const
{
    int i,j;
    Matrix obj;
        obj.row=m.row;
        obj.col=m.col;
           
    for(i=0;i<m.row;i++)
    for(j=0;j<m.col;j++)
    obj.mat[i][j] = mat[i][j] + m.mat[i][j];
       
    return obj;
}
//assign a matrix to other
Matrix& Matrix :: operator = (const Matrix &m)
{
       int i,j;
        row=m.row;
        col=m.col;
       
    for(i=0;i<row;i++)
    for(j=0;j<col;j++)
    mat[i][j] = m.mat[i][j];
   
   return *this;
}
//transpose a matrix
Matrix Matrix :: Transpose() const
{
       int i,j;
    Matrix obj;
    obj.row=row;
    obj.col=col;
   
    for(i=0;i<row;i++)
    for(j=0;j<col;j++)
    obj.mat[i][j]=mat[j][i];
    return obj;
}


main.cpp

#include<iostream>
#include "matrix.h"
using namespace std;
   
    int main() {
int row, col;
cout << "input the row and the col for Matrix a, b" << endl;
cin >> row >> col;
Matrix a(row, col), b(row, col),c(a), d;
cout << endl << "Matrix a:" << endl;
a.input();
a.display();

cout << endl << "Matrix b:" << endl;
b.input();
b.display();

c = a+b;
cout << endl << "Matrix c = Matrix a + Matrix b :" << endl;
c.display();
cout << endl << "Matrix a transpose to Matrix d:" << endl;
d = a.Transpose();
d.display();
return 0;
}

OUTPUT

İ3 CAUsers\ROZYnKRISHNADesktop\pointerr.exe input the ro and the col for Matrix a b Matrix a: Ente 4 elements1 Matrix element

Add a comment
Know the answer?
Add Answer to:
Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &);...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

  • LW: Class Constructors Objectives Write a default constructor for a class. Note that this const...

    LW: Class Constructors Objectives Write a default constructor for a class. Note that this constructor is used to build the object anytime an argument is not provided when it is defined. Write a parameterized constructor that takes arguments that are used to initialize the class’s member variables Labwork Download the code associated with this lab: ColorConstructor.zip Main.cpp Color.h Color.cpp Compile and run the code. The first line of output of the program should display nonsensical integers for the values of...

  • Write a MyString class that stores a (null-terminated) char* and a length and implements all of...

    Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...

  • need help..... sudoku.h class sudoku { public: sudoku(); //default constructor //Postcondition: grid is initialized to 0...

    need help..... sudoku.h class sudoku { public: sudoku(); //default constructor //Postcondition: grid is initialized to 0 sudoku(int g[][9]); //constructor //Postcondition: grid = g void initializeSudokuGrid(); //Function to prompt the user to specify the numbers of the //partially filled grid. //Postcondition: grid is initialized to the numbers // specified by the user. void initializeSudokuGrid(int g[][9]); //Function to initialize grid to g //Postcondition: grid = g; void printSudokuGrid(); //Function to print the sudoku grid. bool solveSudoku(); //Funtion to solve the sudoku problem....

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

  • C++ CODE: The matrix class consists of two files: matrix.h and matrix.cpp. The class has the...

    C++ CODE: The matrix class consists of two files: matrix.h and matrix.cpp. The class has the following definition: matrix -size:int -mat:int** ---------------- +matrix(file:string) +~matrix() +operator +(add:matrix*):matrix & +operator-(sub:matrix*):matrix & +friend operator<<(out:ostream &, t:const matrix&):ostream & +displayRow(r:int):void The class variables are as follows: • size: the number of rows and columns in a square matrix • mat: the integer matrix that contains the numeric matrix itself. The methods have the following behaviour: 2 • matrix(file:string): The default constructor. It receives the...

  • C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object...

    C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object from another of the same type. II. Copy an object to pass it as argument to a function. III. Copy an object to return it from a function. Read the following program and answer questions (20 points) #include <iostream> using namespace std; class Line public: int getLength( void); Line( int len // simple constructor Line( const Line &obj) 1/ copy constructor Line (); //...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

  • 13.21 Lab: Rational class This question has been asked here before, but every answer I have...

    13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...

  • C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all...

    C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Submit your completed source (.cpp) file. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT