• <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 史傳紅 閱讀(539) 評論(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。這樣寫,不符合要求的肯定乘不到一塊去,會提示乘操作未聲明。

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

            亚洲欧美精品一区久久中文字幕| 久久久久久夜精品精品免费啦| 精品国产一区二区三区久久| 国产成人无码久久久精品一| 国产亚洲精久久久久久无码AV| 久久久亚洲精品蜜桃臀| 精品国产乱码久久久久久呢| 久久久久亚洲AV无码网站| 久久精品国产黑森林| 中文字幕热久久久久久久| 一级做a爰片久久毛片16| 久久SE精品一区二区| 国产成人综合久久久久久| 亚洲欧美日韩久久精品第一区| 久久电影网一区| 无码人妻精品一区二区三区久久久| 香港aa三级久久三级| 青草国产精品久久久久久| 久久精品国产清自在天天线| 狠狠88综合久久久久综合网| 欧洲性大片xxxxx久久久| 色偷偷888欧美精品久久久| 国产aⅴ激情无码久久| 一级做a爰片久久毛片免费陪 | 久久国产成人精品麻豆| 日韩美女18网站久久精品| 欧美亚洲国产精品久久蜜芽| 久久人爽人人爽人人片AV| 久久久久久精品久久久久| 久久丝袜精品中文字幕| 国产激情久久久久影院老熟女免费| 久久99精品久久久久婷婷| 久久久久人妻一区精品色| 午夜人妻久久久久久久久| 亚洲国产成人精品女人久久久 | 久久久精品一区二区三区| 久久久久亚洲AV无码网站| 久久永久免费人妻精品下载| 日本强好片久久久久久AAA| 中文字幕无码av激情不卡久久| 久久毛片免费看一区二区三区|