• <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>

            分享知識(shí)

            與大家一起分享知識(shí)

            C++博客 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
              19 Posts :: 3 Stories :: 45 Comments :: 0 Trackbacks

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

            #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;}

            // 構(gòu)造函數(shù)
            Matrix::Matrix(int x,int y)
            {
            ????rows = x;
            ????columns = y;
            ????//有的時(shí)候?yàn)榱丝紤]創(chuàng)建對(duì)象的效率,在使用的時(shí)候分配存儲(chǔ)空間,而不在構(gòu)造函數(shù)中分配
            ????pMatrix = new int* [x];
            ????for (int i = 0 ; i < x; i++ )
            ????{
            ????????pMatrix[i] = new int[y];
            ????????for(int j = 0;j < y;j++)?? //初始化每個(gè)值為0
            ????????????pMatrix[i][j] = 0;
            ????}
            }
            // 析構(gòu)函數(shù)
            Matrix::~Matrix()
            {
            ????for (int? i = 0 ;i < rows;i ++ )
            ????????delete[] pMatrix[i];
            ????delete[] pMatrix;
            }

            // 賦值函數(shù)
            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;
            ??//分配存儲(chǔ)空間
            ??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;
            ??}
            ?}
            }
            // 拷貝構(gòu)造函數(shù)
            Matrix::Matrix(const Matrix&? M)
            {?
            ?rows = M.rows;
            ?columns = M.columns;
            ?//分配存儲(chǔ)空間
            ?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);
            }


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

            posted on 2006-04-12 13:56 史傳紅 閱讀(548) 評(píng)論(2)  編輯 收藏 引用 所屬分類: C/C++細(xì)節(jié)知識(shí)

            Feedback

            # re: 實(shí)現(xiàn)(關(guān)于《[求助] 關(guān)于拷貝構(gòu)造函數(shù),對(duì)象傳遞!!》這篇文章) 2006-04-12 16:53 任我行
            Matrix operator*(const Matrix& a,const Matrix& b)
            感覺(jué)還有問(wèn)題。
            a,b的row,column 不相等沒(méi)有考慮。
              回復(fù)  更多評(píng)論
              

            # re: 實(shí)現(xiàn)(關(guān)于《[求助] 關(guān)于拷貝構(gòu)造函數(shù),對(duì)象傳遞!!》這篇文章) 2006-04-12 18:13 芋頭
            矩陣運(yùn)算,對(duì)另一矩陣維度有要求,而這個(gè)維度是可以在編譯期確定的。這樣的話,我推薦把它寫成泛型的,方便在編譯期檢查錯(cuò)誤。比如點(diǎn)乘,你這里判斷維度如果不正確,就構(gòu)造一個(gè)默認(rèn)的返回去,實(shí)際上應(yīng)該報(bào)錯(cuò)才好。泛型可以解決這個(gè)問(wèn)題。
            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);

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

            泛型還可以解決動(dòng)態(tài)分配內(nèi)存的問(wèn)題,它可以做到在棧上分配,大小都可以在編譯期決議。當(dāng)然占用棧空間比較多,寫成堆上分配也可以。  回復(fù)  更多評(píng)論
              

            久久久久中文字幕| 久久夜色精品国产亚洲| 国产精品成人久久久久久久| a级毛片无码兔费真人久久| 亚洲精品美女久久久久99小说| 久久精品国产日本波多野结衣| 久久婷婷成人综合色综合| 色综合色天天久久婷婷基地| 久久久久久曰本AV免费免费| 国产激情久久久久影院老熟女免费| 久久这里都是精品| 久久艹国产| 国产亚洲婷婷香蕉久久精品| 久久精品国产亚洲αv忘忧草 | 青青草国产精品久久久久| 久久夜色精品国产| 亚洲国产精品热久久| 日产精品久久久久久久性色| 一本色道久久综合| 国产午夜精品久久久久九九| AV色综合久久天堂AV色综合在| 色婷婷久久久SWAG精品| 999久久久国产精品| 99精品久久久久中文字幕| 国产成人无码精品久久久性色| 久久男人中文字幕资源站| 国内精品久久久久久久亚洲| 久久免费高清视频| 欧美777精品久久久久网| 久久久久久免费一区二区三区 | 久久精品国产福利国产琪琪 | 香蕉久久夜色精品国产尤物| 久久国产综合精品五月天| 99久久人人爽亚洲精品美女| 久久精品国产精品亚洲精品| 色综合久久中文色婷婷| 99国内精品久久久久久久 | 久久se精品一区二区| 欧美久久综合性欧美| 国产精品永久久久久久久久久| 国产激情久久久久影院|