CMatrix类
.h文件
#pragma once #ifndef CMATRIX_H #define CMATRIX_H #includeusing namespace std; class CMatrix { public: CMatrix(); CMatrix(int nRow,int nCol,double *pData=NULL); CMatrix(const CMatrix&m); CMatrix(const char*strPath); ~CMatrix(); bool Create(int nRow, int nCol, double* pData = NULL); void Set(int nRow,int nCol,double dVale); void Relaese(); friend istream & operator>>(istream&is,CMatrix &m); friend ostream& operator<<(ostream& os, const CMatrix& m); CMatrix& operator=(const CMatrix&m); CMatrix& operator+=(const CMatrix& m); double& operator[](int nIndex); double& operator()(int nRow,int nCol); bool operator== (const CMatrix & m); bool operator != (const CMatrix & m); operator double(); private: int m_nRow; int m_nCol; double *m_pData; }; CMatrix operator+(const CMatrix& m1, const CMatrix& m2); inline void CMatrix::Set(int nRow, int nCol, double dVal) { m_pData[nRow * m_nCol + nCol] = dVal; } #endif // !1
.cpp文件
#include "CMatrix.h" #include#include CMatrix::CMatrix() { m_nRow = m_nCol = 0; m_pData = 0; } CMatrix::CMatrix(int nRow, int nCol, double* pData) :m_pData(0) { Create(nRow, nCol, pData); } CMatrix::CMatrix(const CMatrix& m) : m_pData(0) { *this = m; } CMatrix::CMatrix(const char* strPath) { m_pData = 0; m_nRow = m_nCol = 0; ifstream cin(strPath); cin >> *this; } CMatrix::~CMatrix() { Relaese(); } bool CMatrix::Create(int nRow, int nCol, double* pData) { Relaese(); m_pData = new double[nRow * nCol]; m_nRow = nRow; m_nCol = nCol; if (pData) { memcpy(m_pData, pData, nRow * nCol * sizeof(double)); } return true; } void CMatrix::Relaese() { if (m_pData) { delete[]m_pData; m_pData = NULL; } m_nRow = m_nCol = 0; } istream& operator>>(istream& is, CMatrix& m) { is >> m.m_nRow >> m.m_nCol; m.Create(m.m_nRow, m.m_nCol); for (int i = 0; i < m.m_nRow * m.m_nCol; i++) { is >> m.m_pData[i]; } return is; } ostream& operator<<(ostream& os, const CMatrix& m) { os << m.m_nRow << " " << m.m_nCol << endl; double* pData = m.m_pData; for (int i = 0; i < m.m_nRow; i++) { for (int j = 0; j < m.m_nCol; j++) { os << *pData++ << " "; } os << endl; } return os; } CMatrix& CMatrix::operator=(const CMatrix& m) { if (this != &m) { Create(m.m_nRow, m.m_nCol, m.m_pData); } return*this; } CMatrix& CMatrix::operator+=(const CMatrix& m) { assert(m_nRow == m.m_nRow && m_nCol == m.m_nCol); for (int i = 0; i < m_nRow * m_nCol; i++) { m_pData[i] += m.m_pData[i]; } return *this; } CMatrix operator+(const CMatrix& m1, const CMatrix& m2) { CMatrix m3(m1); m3 += m2; return m3; } double& CMatrix::operator[](int nIndex) { assert(nIndex < m_nRow* m_nCol); return m_pData[nIndex]; } double& CMatrix::operator()(int nRow, int nCol) { assert(nRow * m_nCol * nCol + nCol < m_nRow* m_nCol); return m_pData[nRow * m_nCol + nCol]; } bool CMatrix::operator ==(const CMatrix& m) { if (!(m_nRow == m.m_nRow && m_nCol == m.m_nCol)) { return false; } for (int i = 0; i < m_nRow * m_nCol; i++) { if (m_pData[i] != m.m_pData[i]) { return false; } } return true; } bool CMatrix::operator !=(const CMatrix& m) { return!((*this) == m); } CMatrix::operator double() { double ds = 0; for (int i = 0; i < m_nRow * m_nCol; i++) { ds += m_pData[i]; } return ds; }
Main函数:
#includeusing namespace std; #include"CMatrix.h" int main() { double pData[10] = { 2,3,4,5 }; CMatrix m1, m2(2, 5, pData); cin >> m1; m2.Set(1, 3, 10); cout << m1 << m2; CMatrix ms[4] = { CMatrix(),CMatrix(2,5,pData),CMatrix(ms[1]),CMatrix("C:\1.txt") }; cout << ms[1] << ms[2]; if (ms[1] != ms[2]) { cout << "Error occur!" << endl; } ms[1] += ms[2]; ms[1][1] = 100; ms[1](1, 1) = 50; cout << ms[1]; cout << "sum of m1=" << double(ms[1]); double d = 1.2; int i = int(d); return 0; }
1.构造函数
就是在对象被 创建时利用特定的值构造对象,将对象初始化为一个特定的状态,构造函数在对象被创建时将被自动调用;
调用时无提供参数的构造函数称为默认构造函数。
2.复制构造函数
具有一般构造函数的所有特性,形参是本类对象的引用;作用是使用一个已经存在的对象,去初始化同类的新对象(复制一个完全的对象);
3.内联成员函数
声明方式有两种:隐式声明和显示声明;
隐式声明:将函数体直接放在类体内,这种方法称之为隐式声明;
显示声明:采用关键字inline,在函数返回值类型前加上inline,类定义中不加入函数体。
4.析构函数
析构函数无参、无返回值。
用来完成对象被删除前的一些清理工作;
析构函数是在对象的生存周期即将结束时刻被调用的;
调用完成之后,对象消失,相应的内存空间也被释放。
5.友元函数
友元函数是在类中用关键字friend修饰的非成员函数;
不是本类的成员函数,但是在它的函数体中可以通过对象名访问类的私有和保护成员。
6.运算符重载
“<<”与“>>”分别是流插入运算符与流提取运算符,在使用的时候需要在本文件的模块中包含头文件stream,还用过包含“using namespace std”;
运算符重载是定义一个重载运算符的函数,使指定的运算符不仅能实现原有的功能,而且也实现在函数中指定的新的功能。在使用被重载的运算符,系统会自动调用该函数,以实现相应的功能。运算符重载是通过定义函数实现的。实质就是函数的重载;
一般格式:
函数类型 operator 运算符名称 (形参表)
{对运算符的重载处理}
Eg: Complex operator + (complex &c1 , complex & c2);



