• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            分享知識

            與大家一起分享知識

            C++博客 首頁 新隨筆 聯系 聚合 管理
              19 Posts :: 3 Stories :: 45 Comments :: 0 Trackbacks

            希望大家能夠一起討論,把矩陣這個類給完善。

            #include? <iostream>
            using namespace std;

            class? Matrix
            {
            private:
            ????int? rows,columns;
            ????int **pMatrix;
            public:?
            ????Matrix(int rows = 3,int columns = 2);
            ????Matrix(const Matrix &M);
            ????~Matrix();
            ????Matrix& operator=(const Matrix& M);
            ????int? GetRows() const;
            ????int? GetColumns() const;
            ????void? SetValue();
            ????friend Matrix operator*(const Matrix& a,const Matrix& b);
            ????friend ostream& operator<<(ostream& os,const Matrix& M);
            };

            int? Matrix::GetRows() const? { return? rows;}
            int? Matrix::GetColumns() const { return? columns;}

            // 構造函數
            Matrix::Matrix(int x,int y)
            {
            ????rows = x;
            ????columns = y;
            ????//有的時候為了考慮創建對象的效率,在使用的時候分配存儲空間,而不在構造函數中分配
            ????pMatrix = new int* [x];
            ????for (int i = 0 ; i < x; i++ )
            ????{
            ????????pMatrix[i] = new int[y];
            ????????for(int j = 0;j < y;j++)?? //初始化每個值為0
            ????????????pMatrix[i][j] = 0;
            ????}
            }
            // 析構函數
            Matrix::~Matrix()
            {
            ????for (int? i = 0 ;i < rows;i ++ )
            ????????delete[] pMatrix[i];
            ????delete[] pMatrix;
            }

            // 賦值函數
            Matrix& Matrix::operator=(const Matrix& M)
            {
            ?if(this != &M)
            ?{
            ??for (int? ii = 0 ;ii < rows;ii++ )
            ???if(pMatrix[ii])
            ????delete[] pMatrix[ii];
            ??if(pMatrix)
            ???delete[] pMatrix;
            ??? rows = M.rows;
            ??? columns = M.columns;
            ??//分配存儲空間
            ??pMatrix = new int* [rows];
            ??for (int k = 0 ; k < rows; k++ )
            ???pMatrix[k] = new int[columns];

            ??for ( int? i = 0 ; i < rows; i ++ )
            ???for ( int? j = 0 ; j < columns; j ++ )
            ????pMatrix[i][j] = M.pMatrix[i][j];
            ?}
            ?return *this;
            }
            void? Matrix::SetValue()
            {
            ?int? i,j,value;
            ?for ( i = 0 ; i < rows; i ++ )
            ?{
            ??for ( j = 0 ; j < columns; j ++ )
            ??{
            ???cout << " 第 " << i << " 行 " ;
            ???cout << " 第 " << j << " 列: " ;
            ???cin >> value;
            ???cout << endl;
            ???pMatrix[i][j] = value;
            ??}
            ?}
            }
            // 拷貝構造函數
            Matrix::Matrix(const Matrix&? M)
            {?
            ?rows = M.rows;
            ?columns = M.columns;
            ?//分配存儲空間
            ?pMatrix = new int* [rows];
            ?for (int k = 0 ; k < rows; k++ )
            ??pMatrix[k] = new int[columns];

            ?for ( int? i = 0 ; i < rows; i ++ )
            ??for ( int? j = 0 ; j < columns; j ++ )
            ???pMatrix[i][j] = M.pMatrix[i][j];
            }

            Matrix operator*(const Matrix& a,const Matrix& b)
            {
            ?if? (a.columns == b.rows)
            ?{
            ??Matrix c(a.rows,b.columns);
            ??for ( int? i = 0 ;i < a.rows;i ++ )
            ??{
            ???for ( int? j = 0 ;j < b.columns;j ++ )?
            ???{??
            ????for ( int? columnIndex= 0 ;columnIndex < a.columns;columnIndex++ )
            ?????c.pMatrix[i][j] += a.pMatrix[i][columnIndex] * b.pMatrix[columnIndex][j];
            ???}
            ??}
            ??return c;
            ?}
            ?else
            ??return Matrix();
            }

            ostream& operator<<(ostream& os,const Matrix& M)
            {
            ?for (int i = 0;i < M.rows;i++ )
            ?{
            ??for (int j = 0;j < M.columns;j++ )
            ???os << M.pMatrix[i][j] << "? ";
            ??os << endl;
            ?}
            ?return (os << endl);
            }


            // 主函數
            void? main()
            {
            ?Matrix Ma(3,2),Mb(2,2);
            ?Ma.SetValue();
            ?Mb.SetValue();
            ?cout << Ma << endl;
            ?cout << Mb << endl;
            ?
            ?Matrix Mc = Ma * Mb;//拷貝構造函數
            ?cout << Mc << endl;
            ?Mc = Mb;?????//=運算符,即賦值函數
            ?cout << Mb << endl;
            }

            posted on 2006-04-12 13:56 史傳紅 閱讀(543) 評論(2)  編輯 收藏 引用 所屬分類: C/C++細節知識

            Feedback

            # re: 實現(關于《[求助] 關于拷貝構造函數,對象傳遞!!》這篇文章) 2006-04-12 16:53 任我行
            Matrix operator*(const Matrix& a,const Matrix& b)
            感覺還有問題。
            a,b的row,column 不相等沒有考慮。
              回復  更多評論
              

            # re: 實現(關于《[求助] 關于拷貝構造函數,對象傳遞!!》這篇文章) 2006-04-12 18:13 芋頭
            矩陣運算,對另一矩陣維度有要求,而這個維度是可以在編譯期確定的。這樣的話,我推薦把它寫成泛型的,方便在編譯期檢查錯誤。比如點乘,你這里判斷維度如果不正確,就構造一個默認的返回去,實際上應該報錯才好。泛型可以解決這個問題。
            template <int C, int R>
            class Matrix
            {
            ...
            };

            template <int C, int R, int R1>
            Matrix<C, R1> operator*(const Matrix<C, R>& lhs, const Matrix<R, R1>& rhs);

            大致的原型就是這樣吧,可能要加幾個typename。這樣寫,不符合要求的肯定乘不到一塊去,會提示乘操作未聲明。

            泛型還可以解決動態分配內存的問題,它可以做到在棧上分配,大小都可以在編譯期決議。當然占用棧空間比較多,寫成堆上分配也可以。  回復  更多評論
              

            香蕉99久久国产综合精品宅男自 | 嫩草影院久久国产精品| 久久精品国产亚洲AV麻豆网站| 99久久无码一区人妻a黑| 精品无码人妻久久久久久| 亚洲国产欧洲综合997久久| 久久精品这里热有精品| 香蕉久久久久久狠狠色| 久久免费线看线看| 伊人久久综合无码成人网| 伊人久久综在合线亚洲2019 | 久久婷婷国产综合精品| 欧美久久精品一级c片片| 久久久SS麻豆欧美国产日韩| 国产精品国色综合久久| 精产国品久久一二三产区区别 | 精品人妻伦九区久久AAA片69 | 2021久久国自产拍精品| 久久笫一福利免费导航| 97超级碰碰碰碰久久久久| 欧洲精品久久久av无码电影| 亚洲国产精品成人久久蜜臀 | 丁香狠狠色婷婷久久综合| 国产69精品久久久久9999APGF| 精品久久人人做人人爽综合| 91精品国产9l久久久久| 久久亚洲美女精品国产精品| 久久久精品人妻一区二区三区蜜桃| 色偷偷88欧美精品久久久 | 久久久久久亚洲Av无码精品专口| 久久久午夜精品福利内容| 欧美性猛交xxxx免费看久久久| 久久播电影网| 精品久久人人爽天天玩人人妻| 麻豆精品久久久一区二区| 91麻精品国产91久久久久 | 丰满少妇人妻久久久久久| 久久水蜜桃亚洲av无码精品麻豆| 久久精品中文无码资源站| 性欧美大战久久久久久久久| 人妻无码αv中文字幕久久|