1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
#include<iostream>
#include<iomanip>
#include<string.h>
using namespace std;
class Matrix{
private:
int rows; //For rows
int cols; //For columns
int **ptr; //pointer to pointer to point in the array
public:
//Declaring Constructors and destructors
Matrix(Matrix &);
Matrix(int, int);
~Matrix();
//Declaring other functions
friend istream& operator>>(istream &, Matrix &);
friend ostream& operator<<(ostream &,Matrix &);
Matrix operator +(Matrix &);
Matrix operator -(Matrix &);
Matrix operator *(Matrix &);
Matrix operator =(Matrix &);
void transpose(Matrix &);
//End of class definition
};
//Defining Copy constructor
Matrix::Matrix(Matrix &m){
rows = m.rows;
cols = m.cols;
ptr = new int* [rows];
for(int i= 0;i<rows;i++){
ptr[i]=new int [cols];
for(int i =0 ;i<rows;i++){
for(int j=0;j<cols;j++){
ptr[i][j] = m.ptr[i][j];
}
}
}
}
//Defining parametrised constructor
Matrix::Matrix(int m, int n){
rows = m;
cols = n;
ptr = new int* [m];
for(int i =0;i<m;i++){
ptr[i] = new int[n];
}
}
//Defining oveloaded operator to take matrix as an input
istream & operator >>(istream &din , Matrix & m){
for(int i =0;i<m.rows;i++){
for(int j =0;j<m.cols;j++){
din >> m.ptr[i][j];
}
}
return din;
}
//Defining overoaded operator to display matrix
ostream & operator << (ostream &dout,Matrix &m ){
for(int i =0;i<m.rows;i++){
for(int j = 0; j<m.cols;j++){
dout<<setw(5)<< m.ptr[i][j];
}
dout<<endl;
}
return dout;
}
//Defining overloaded + opearator to add two matrices
Matrix Matrix::operator +(Matrix &m){
Matrix temp( rows, cols);
for(int i =0;i<rows;i++){
for(int j =0;j<cols;j++){
temp.ptr[i][j]= ptr[i][j] + m.ptr[i][j];
}
}
return temp;
}
//Definig overloaded - operator to subtract two matrices
Matrix Matrix::operator -(Matrix &m){
Matrix temp(rows,cols);
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
temp.ptr[i][j] = ptr[i][j] - m.ptr[i][j];
}
}
return temp;
}
//Defining overloaded *opeartor to perform matrix multiplication
Matrix Matrix::operator *(Matrix &m){
Matrix temp(rows,cols);
int product_sum;
for(int i =0;i<rows;i++){
for(int j=0;j<cols;j++){
product_sum =0;
for(int k=0;k<rows;k++){
product_sum += ptr[i][k] * m.ptr[k][j];
}
temp.ptr[i][j] = product_sum;
}
}
return temp;
}
//Defining overloaded = operator to assingn matrix value into another matrix
Matrix Matrix::operator =(Matrix & m){
for(int i =0;i<rows;i++){
for(int j=0;j<cols;j++){
ptr[i][j] = m.ptr[i][j];
}
}
return *this;
}
//Definig a function to perform transpose of a matrix
void Matrix::transpose(Matrix &m){
for(int i=0;i< rows;i++){
for(int j=0;j<cols;j++){
ptr[j][i] = m.ptr[i][j];
}
}
}
//Definig Destructor
Matrix::~Matrix(){
for(int i=0;i<rows;i++){
delete[] ptr[i];
}
delete[] ptr;
}
//Main Function
int main(){
int row_1 ,row_2,col_1,col_2 ,chs;
char choice , ch;
cout<<"\n\n\t\t\t\t\tCreate Matrix A";
cout<<"\nEnter the number of rows for matrix : ";
cin>>row_1;
cout<<"\nEnter the number of columns for matrix : ";
cin>>col_1;
Matrix A(row_1,col_1);
cout<<"\nEnter the elements of Matrix row wise : ";
cin >> A;
cout<<"\nEntered Matrix \n";
cout << A;
cout<<"\n\n\t\t\tCreate Matrix B";
cout<<"\nEnter the number of rows for matrix : ";
cin>>row_2;
cout<<"\nEnter the number of columns for matrix : ";
cin>>col_2;
Matrix B(row_2,col_2);
cout<<"\nEnter the elements of Matrix row wise : ";
cin >> B;
cout<<"\nEntered Matrix\n";
cout << B;
Matrix C(col_1,row_1);
C = A + B; //GETTING ERROR ON THIS LINE
// **NO Operator "=" matches these operands , operand types -- Matrix = Matrix**
return 0;
}
|