• <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。這樣寫,不符合要求的肯定乘不到一塊去,會提示乘操作未聲明。

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

            国产一区二区精品久久| 午夜不卡888久久| 国内精品九九久久精品| 久久人人爽人人爽人人片av高请| 久久伊人五月丁香狠狠色| 欧美一区二区精品久久| 久久久久久久波多野结衣高潮 | 国产精品美女久久久网AV| 久久e热在这里只有国产中文精品99| 日韩亚洲国产综合久久久| 国产成人精品久久免费动漫| 久久久久久精品免费免费自慰| 天天综合久久一二三区| 久久精品一区二区三区AV| 久久久无码精品亚洲日韩蜜臀浪潮| 女人高潮久久久叫人喷水| 久久亚洲精品无码VA大香大香| 伊人久久精品无码av一区| 久久青青草原精品影院| 久久青青草视频| 久久不见久久见免费视频7| 色欲av伊人久久大香线蕉影院| 99久久精品免费看国产一区二区三区| 久久精品国产精品青草| 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | 合区精品久久久中文字幕一区| 久久精品国产久精国产思思| 免费一级做a爰片久久毛片潮| 久久国产精品国语对白| 久久久噜噜噜久久熟女AA片| 久久九九免费高清视频| 久久久久噜噜噜亚洲熟女综合| 69SEX久久精品国产麻豆| 色诱久久久久综合网ywww| 久久狠狠爱亚洲综合影院| 久久久久国色AV免费看图片| 精品免费久久久久国产一区| 久久九九有精品国产23百花影院| 久久se精品一区精品二区| 久久精品国产亚洲综合色| 9999国产精品欧美久久久久久|