Question

This is for c++ Implement the following overloading operator functions: Matrix (int rowSize, int colSize); ~Matrix...

This is for c++

Implement the following overloading operator functions:

Matrix (int rowSize, int colSize);
~Matrix ();
Matrix operator + (Matrix & m);
Matrix operator += (Matrix & m);
Matrix operator += (const int &num);
Matrix operator * (Matrix & m);
Matrix operator ++();
friend Matrix operator +(const int &num, const Matrix &m);
friend istream& operator>> (istream& in, const Matrix& m);
friend ostream &operator<<(ostream &os, const Matrix &m);

Test your Matrix class withe following main function:

cout << "Matrix 1:" << endl;
Matrix matrix1 (2, 2);
cin>>matrix1;
cout<<matrix1<<endl;

cout << "Matrix 2:" << endl;
Matrix matrix2 (2, 2);
cin>>matrix2;
cout<<matrix2<<endl;

// Instantiation and setup of matrix3
cout << "Matrix 3:" << endl;
Matrix matrix3 (2, 2);
cin>>matrix3;
cout<<matrix3<<endl;
cout << "Result of matrix1 + matrix2: " << endl;
Matrix addResult (2, 2);
addResult = matrix1 + matrix2;
cout<<addResult<<endl;
cout << "Result of matrix1 * matrix3: " << endl;
Matrix product(2,2);
product = matrix1 * matrix3;
cout<<product<<endl;
cout<<"++ operator: "<<endl;
Matrix matrix4(2,2);
matrix4 = ++matrix1;
cout<<"Matrix 4:"<<endl;
cout<<matrix4<<endl;
cout<<"Matrix 1 after incremment by one:"<<endl;
cout<<matrix1<<endl;
matrix1 = 2 + matrix1;
cout<<"Matrix 1 after incremment by constant:"<<endl;
cout<<matrix1<<endl;
cout<<"Use += with a matrix"<<endl;
matrix1 += matrix2;
cout<<matrix1<<endl;
cout<<"Use += with a constant"<<endl;
matrix1 += 5;
cout<<matrix1<<endl;

Output

Matrix 1:
Enter the array:
2
3
5
1
Output:
2 3
5 1

Matrix 2:
Enter the array:
5
5
4
4
Output:
5 5
4 4

Matrix 3:
Enter the array:
3
5
1
1
Output:
3 5
1 1

Result of matrix1 + matrix2:
Output:
7 8
9 5

Result of matrix1 * matrix3:
Output:
9 13
16 26

++ operator:
Matrix 4:
Output:
3 4
6 2

Matrix 1 after incremment by one:
Output:
3 4
6 2

Matrix 1 after incremment by constant:
Output:
5 6
8 4

Use += with a matrix:
Output:
10 11
12 8

Use += with a constant:
Output:
15 16
17 13

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

//C++ program

#include<iostream>
using namespace std;

class Matrix{
   private:
       int row,col;
       int **arr;
      
   public:
       Matrix(){}
      
       Matrix (int rowSize, int colSize){
           row = rowSize;
           col = colSize;
           arr = new int*[row];
           for(int i=0;i<row;i++){
               arr[i] = new int[col];
           }
          
       }
       ~Matrix (){
          
       }
       Matrix operator + (Matrix & m){
           Matrix *newMatrix = new Matrix(row,col);
          
           for(int i=0;i<row;i++){
               for(int j=0;j<col;j++)
                   newMatrix->arr[i][j] = this->arr[i][j] + m.arr[i][j];
           }
           return *newMatrix;
       }
       Matrix operator += (Matrix & m){
           for(int i=0;i<row;i++){
               for(int j=0;j<col;j++)
                   this->arr[i][j] += m.arr[i][j];
           }
           return *this;
       }
       Matrix operator += (const int &num){
           for(int i=0;i<row;i++){
               for(int j=0;j<col;j++)
                   this->arr[i][j] += num;
           }
           return *this;
       }
       Matrix operator * (Matrix & m){
           Matrix *newMatrix = new Matrix(row,col);
          
           for (int i = 0; i < row; i++)
           {
           for (int j = 0; j < m.col; j++)
           {
           int result = 0;
           for (int k = 0; k < col; k++)
           result += this->arr[i][k] * m.arr[k][j];
       newMatrix->arr[i][j] = result;
           }
           }
           return *newMatrix;
       }
       Matrix operator ++(){
           for(int i=0;i<row;i++){
               for(int j=0;j<col;j++)
                   this->arr[i][j] ++;
           }
           return *this;
       }
       friend Matrix operator +(const int &num, const Matrix &m){
           Matrix *newMatrix = new Matrix(m.row,m.col);
          
           for(int i=0;i<m.row;i++){
               for(int j=0;j<m.col;j++)
                   newMatrix->arr[i][j] = num + m.arr[i][j];
           }
           return *newMatrix;
       }
       friend istream& operator>> (istream& in, const Matrix& m){
           for(int i=0;i<m.row;i++){
               for(int j=0;j<m.col;j++)
                   in>>m.arr[i][j];
           }
           return in;
       }
       friend ostream &operator<<(ostream &os, const Matrix &m){
           for(int i=0;i<m.row;i++){
               for(int j=0;j<m.col;j++)
                   os<<m.arr[i][j]<<" ";
               os<<"\n";
           }
           return os;
       }
};

int main(){
   cout << "Matrix 1:" << endl;
Matrix matrix1 (2, 2);
cin>>matrix1;
cout<<matrix1<<endl;

cout << "Matrix 2:" << endl;
Matrix matrix2 (2, 2);
cin>>matrix2;
cout<<matrix2<<endl;

// Instantiation and setup of matrix3
cout << "Matrix 3:" << endl;
Matrix matrix3 (2, 2);
cin>>matrix3;
cout<<matrix3<<endl;
cout << "Result of matrix1 + matrix2: " << endl;
Matrix addResult ;
addResult = matrix1 + matrix2;
cout<<addResult<<endl;
cout << "Result of matrix1 * matrix3: " << endl;
Matrix product(2,2);
product = matrix1 * matrix3;
cout<<product<<endl;
cout<<"++ operator: "<<endl;
Matrix matrix4(2,2);
matrix4 = ++matrix1;
cout<<"Matrix 4:"<<endl;
cout<<matrix4<<endl;
cout<<"Matrix 1 after incremment by one:"<<endl;
cout<<matrix1<<endl;
matrix1 = 2 + matrix1;
cout<<"Matrix 1 after incremment by constant:"<<endl;
cout<<matrix1<<endl;
cout<<"Use += with a matrix"<<endl;
matrix1 += matrix2;
cout<<matrix1<<endl;
cout<<"Use += with a constant"<<endl;
matrix1 += 5;
cout<<matrix1<<endl;
}

//sample output

Add a comment
Know the answer?
Add Answer to:
This is for c++ Implement the following overloading operator functions: Matrix (int rowSize, int colSize); ~Matrix...
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
  • Start by using the starter code provided for the Line class. It implements operator overloading so...

    Start by using the starter code provided for the Line class. It implements operator overloading so you can use cin/cout with line objects. A line will be made up of two points. Create an object to implement a “line” class which allows the programmer to store a line. This class must use the “point” class developed in Part B. The object should have two constructors, appropriate set/get functions, and overloaded I/O (cin/cout) functions. It should include functions the return the...

  • In this lab, you will need to implement the following functions in Text ADT with C++...

    In this lab, you will need to implement the following functions in Text ADT with C++ language(Not C#, Not Java please!): PS: The program I'm using is Visual Studio just to be aware of the format. And I have provided all informations already! Please finish step 1, 2, 3, 4. Code is the correct format of C++ code. a. Constructors and operator = b. Destructor c. Text operations (length, subscript, clear) 1. Implement the aforementioned operations in the Text ADT...

  • Please use C++ and add comments to make it easier to read. Do the following: 1)...

    Please use C++ and add comments to make it easier to read. Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1...

  • In the following code, it gets hung up at    cout << "Number1 * Number2 =...

    In the following code, it gets hung up at    cout << "Number1 * Number2 = " << number1 * number2 << endl; giving an error of "no math for operator<<" what am i doing wrong? Thank you #include <iostream> #include <cctype> #include <cstdlib> using namespace std; class Rational //class for rational numbers (1/2, 5/9, ect..) {    public:        Rational(int numerator, int denominator);        Rational(int numberator);        Rational(); //default        friend istream& operator >>(istream& ins,...

  • 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 <<...

  • Do the following: 1) Add a constructor with two parameters, one for the numerator, one for...

    Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1 5) Modify the operator>> function so that after it reads the denominator...

  • In this assignment you are required to complete a class for fractions. It stores the numerator...

    In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...

  • please help with the operator overloading lab (intArray) in c++ will provide what it is being...

    please help with the operator overloading lab (intArray) in c++ will provide what it is being required and the code that was given from the book. the code that was provided is below ------------------------------------------------------------------------------------------------------------------------- // iadrv.h #ifndef _IADRV_H #define _IADRV_H #include "intarray.h" int main(); void test1(); void test2(); void test3(); void test4(); void test5(); void test6(); void test7(); void test8(); void test9(); void test10(); void test11(); void test12(); void test13(); void test14(); void test15(); void test16(); void test17(); void test18();...

  • C++ Class and Operator Overloading part 1 The source file contains a C++ program that declares...

    C++ Class and Operator Overloading part 1 The source file contains a C++ program that declares and initializes an array of Circles. The program is using a for loop statement to find the largest circle in the array and display it on the output screen. However, the program is currently not working. The problem is the program uses logical operator ' > ' for comparison of circles and it is not working unless we overload such operator for the class...

  • C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class...

    C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class in separate files for the interface and implementation so that the implementation can be compiled separately from any application program. Submit three files: YourLastNameFirstNameInitialMoney.cpp - This file contains only the Money class implementation YourLastNameFirstNameInitialProj4.cpp - This file contains only the main function to use/test your money class YourLastNameFirstNameInitialMoney.h - This file contains the header file information.   Optional bonus (10%): Do some research on make...

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