C 過載矩陣類運算子

2021-09-03 02:34:05 字數 1728 閱讀 9309

//過載 運算子 矩陣  + * = <<  >>  

#includeusing namespace std;

templateclass matrix;

templatematrix::matrix(int n, int m)

}

templatematrix::matrix(const matrix&mat)

} }

templatematrix::~matrix()

delete_p;

}

templatematrixmatrix::operator+ (matrix&mat)

else

} return matsum;

}

}

templatematrixmatrix::operator* (matrix&mat)

else

} }

return matproduct;

} }

templatematrix& matrix::operator=(const matrix&mat)

} for (int i = 0; i < mat._rows; i++)

} return (*this);

}

templateostream& operator<<(ostream&out, matrix&mat)

} return out;

}

templateistream& operator>>(istream&in, matrix&mat)

} return in;

}

templatevoid matrix::setelement()

} }

templatevoid matrix::display()

cout << endl;

} } int main()

catch (const char* msg)

try

catch (const char* msg)

return 0;

}

請輸入行數: 2

請輸入列數: 3

請輸入矩陣的元素,共6個:

1 2 3 4 5 6

1 2 3

4 5 6

請輸入行數: 2

請輸入列數: 3

請輸入矩陣的元素,共6個:

6 5 4 3 2 1

6 5 4

3 2 1

矩陣相加:

7 7 7

7 7 7

矩陣相乘需滿足第乙個矩陣的列數和第二個矩陣的行數相等!

請按任意鍵繼續. . .

請輸入行數: 2

請輸入列數: 3

請輸入矩陣的元素,共6個:

1 2 3 4 5 6

1 2 3

4 5 6

請輸入行數: 3

請輸入列數: 2

請輸入矩陣的元素,共6個:

1 2 3 4 5 6

1 23 4

5 6矩陣相加需滿足行數和列數均相等!

矩陣相乘:

22 28

49 64

請按任意鍵繼續. . .

運算子過載 類 C

運算子過載 就是對已有的運算子 c 中預定義的運算子 賦予多重的含義,是同乙個運算子作用與不同型別的資料是導致不同型別的行為 實質 函式過載 1 可以過載為普通函式也可以過載為成員函式 2 把含運算子的表示式轉換成對運算子函式的呼叫 3 把運算子的運算元轉換 成 運算子函式的引數 4 運算子被多次過...

運算子類過載

類過載格式 函式型別 operator 運算子名稱 形參表 型引數 使用引數 1 使用 號實現複數相加 不使用類過載 include using namespace std class complex complex double r,double i complex complex add com...

運算子過載 類的賦值運算子過載

下面介紹類的賦值運算子 1.c 中物件的記憶體分配方式 在c 中,物件的例項在編譯的時候,就需要為其分配記憶體大小,因此,系統都是在stack上為其分配記憶體的。這一點和c 完全不同!千 萬記住 在c 中,所有類都是reference type,要建立類的實體,必須通過new在heap上為其分配空間...