• <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>
            華劍緣
            一切都在這個(gè)過(guò)程中獲得,將那些目標(biāo)埋藏于心中
            posts - 19,comments - 20,trackbacks - 0
            [求助]?關(guān)于拷貝構(gòu)造函數(shù),對(duì)象傳遞!!

            #include?
            < iostream.h >
            class ?Matrix
            {
            private :
            ????
            int ?rows,columns;
            public :
            ????????
            int ? ** pMatrix;
            ???Matrix(?
            int ?rows, int ?columns);
            ???Matrix(Matrix
            & ?);
            ???
            ~ Matrix();
            int ?GetRows();
            int ?GetColumns();
            void ?SetValue();
            void ?Mul(Matrix?a,Matrix?b);
            void ?Mul(Matrix? * pa,Matrix? * pb);
            void ?Mul(Matrix? & a,Matrix? & b);

            }
            ;

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

            // 構(gòu)造函數(shù)
            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];
            }


            // 析構(gòu)函數(shù)
            Matrix:: ~ Matrix()
            {
            ????????
            for ( int ?i = 0 ;i < rows;i ++ )
            ??????????delete[]?pMatrix[i];
            ????delete[]?pMatrix;
            }


            // 賦值函數(shù)
            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(Matrix & ?M)
            {??
            ?????
            for ( int ?i = 0 ;?i < M.rows;?i ++ )
            ?????????????????
            for ( int ?j = 0 ;?j < M.columns;?j ++ )
            ???????????????????pMatrix[i][j]
            = M.pMatrix[i][j];????? /// //這里對(duì)不對(duì)?有什么更好的方式?
            }


            void ?Matrix::Mul(Matrix?a,Matrix?b)
            {
            ??Matrix?c(a.GetRows(),b.GetColumns());
            ????
            for ( int ?i = 0 ;i < a.GetRows();i ++ ) {
            ????????????????
            for ( int ?y = 0 ;y < b.GetColumns();y ++ ) {
            ??????????
            if ?(a.GetColumns() == b.GetRows())
            ????????????
            for ( int ?j = 0 ,x = 0 ;j < a.GetColumns(),x < b.GetRows?();j ++ ,x ++ )
            ??????????????????c.pMatrix[i][y]?
            += a.pMatrix[i][j] * b.pMatrix[x][y];???? /// //這里對(duì)不對(duì)?有什么更好的方式?

            ???????????
            else ? break ;
            ????????????????????????
            ????????????????}

            ????????}

            ???
            }


            // 主函數(shù)
            void ?main()
            {
            ????????Matrix?Ma(
            3 , 2 ),Mb( 2 , 2 );
            ????????Ma.SetValue();
            ????????Mb.SetValue();
            ????
            for ( int ?i;i < Ma.GetRows();i ++ )
            ????????????????
            for ( int ?j;j < Ma.GetColumns();j ++ )
            ????????????????????????cout
            << Ma.pMatrix[i][j];?? // 為什么編譯運(yùn)行后不能輸出呢??

            ????????Matrix?Mc(
            3 , 2 );??? /// 覺(jué)得這樣不妥,還有什么跟好的方法么
            ????????Mc.Mul(Ma,Mb);???? /// 這樣也不對(duì),怎么讓兩個(gè)Matrix對(duì)象相乘呢,有什么更好的方式么?
            }


            感謝大家熱心指教。
            一下是整理后的。
            /////////////////////////////////////////////////
            /////////////////////////////////////////////////

            ///////////////////Matrix.Class////////////////////////

            #include?<iostream.h>
            //using?namespace?std;???????//為什么不能在VC下正常使用

            class?Matrix
            {
            private:
            ????
            int?**pMatrix;
            ????
            int?rows,columns;
            public:
            static?int?ObjectAliveNo;
            ????Matrix(
            int?rows=0,int?columns=0);
            ????Matrix(
            const?Matrix?&M);
            ????
            ~Matrix();
            Matrix
            &?operator=(const?Matrix&?M);
            int?GetRows()?const;
            int?GetColumns()?const;
            int?GetObjNo()?const;
            void?SetValue();
            void?Mul(const?Matrix?a,const?Matrix?b);
            void?Mul(const?Matrix?*pa,const?Matrix?*pb);
            void?MUl(const?Matrix?&a,const?Matrix?&b);
            friend?Matrix?
            operator~(Matrix&?a);?????????????????????????????????//重載"~"操作符實(shí)現(xiàn)矩陣轉(zhuǎn)置
            friend?Matrix?operator*(const?Matrix&?a,const?Matrix&?b);??????????//重載"~"操作符實(shí)現(xiàn)矩陣相乘
            //friend?ostream&?operator<<(const?ostream&?os,const?Matrix&?M);???//!!!
            friend?ostream&?operator<<(ostream&?os,const?Matrix&?M);
            }
            ;

            //構(gòu)造函數(shù)
            Matrix::Matrix(int?x,int?y)
            {
            ????ObjectAliveNo
            ++;
            ????rows
            =x;
            ????columns
            =y;
            ??????pMatrix
            =new?int?*[rows];????????//創(chuàng)建指針數(shù)組
            ??????for(int?i=0;?i<rows;?i++){
            ???????pMatrix[i]
            =new?int?[columns];?//真正實(shí)現(xiàn)二維數(shù)組
            ???????for(int?j=0;?j<columns;?j++)
            ?????????pMatrix[i][j]
            =0;???????????//對(duì)二維數(shù)組初始化
            ?????}

            }


            //拷貝構(gòu)造函數(shù)函數(shù)
            Matrix::Matrix(const?Matrix&?M)
            {
            ????rows
            =M.rows;
            ????columns
            =M.columns;
            ????
            //賦值前現(xiàn)分配空間!
            ????pMatrix=new?int?*[rows];
            ?????
            for(int?m=0;?m<rows;?m++)
            ???????pMatrix[m]
            =new?int?[columns];
            ???????
            ??
            for(int?i=0;i<rows;i++)
            ????
            for(int?j=0;j<columns;j++)
            ??????pMatrix[i][j]
            =M.pMatrix[i][j];
            }


            //析構(gòu)函數(shù)
            Matrix::~Matrix()
            {
            ????ObjectAliveNo
            --;
            ????
            for(int?i=0;i<rows;i++)
            ????delete[]?pMatrix[i];????????
            //注意delete的順序
            ????delete[]?pMatrix;
            }


            //
            int?Matrix::GetRows()?const?{return?rows;}????????????//
            int?Matrix::GetColumns()?const?{return?columns;}??????//
            int?Matrix::GetObjNo()?const?{return?ObjectAliveNo;}??//對(duì)象數(shù)


            //為矩陣賦值
            void?Matrix::SetValue()
            {
            ??cout
            <<"請(qǐng)對(duì)矩陣的每一項(xiàng)賦值:"<<endl;
            ????
            int?i,j,value;
            ????
            for(i=0;i<rows;i++)
            ??????
            for(j=0;j<columns;j++){
            ????????cout
            <<"第?"<<i+1<<"";
            ????????cout
            <<""<<j+1<<"列:";
            ????????cin
            >>value;
            ????????pMatrix[i][j]
            =value;
            ??????}

            }


            //重載"="操作符實(shí)現(xiàn)矩陣之間賦值
            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;
            }


            //調(diào)用函數(shù)實(shí)現(xiàn)矩陣相乘操作
            void?Matrix::Mul(const?Matrix?a,const?Matrix?b)
            {
            ????Matrix?c(a.GetRows(),b.GetColumns());
            ????
            if(a.GetColumns()==b.GetRows()){
            ???????
            int?temp=0;
            ???????
            for(int?i=0;i<a.GetRows();i++)
            ???????????
            for(int?j=0;j<b.GetColumns();j++){
            ??????????????
            for(int?k=0;k<a.GetColumns();k++)
            ??????????????temp
            =temp+a.pMatrix[i][k]*b.pMatrix[k][j];
            ??????????????c.pMatrix[i][j]
            =temp;
            ??????????????temp
            =0;
            ???????????}

            ????}

            //輸出相乘結(jié)果
            ?????for(int?i=0;i<c.GetRows();i++){
            ????cout
            <<'\n';
            ????????
            for(int?y=0;y<c.GetColumns();y++)
            ??????????????cout
            <<c.pMatrix[i][y]<<'?';
            ????????}

            }


            //重載操作符"*"實(shí)現(xiàn)矩陣相乘操作
            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();
            }


            //重載"~"操作符實(shí)現(xiàn)矩陣轉(zhuǎn)置
            Matrix?operator~(Matrix&?a)
            {
            ????Matrix?b(a.columns,a.rows);
            ????
            for(int?i=0;i<a.rows;i++)
            ?????
            for(int?j=0;j<a.columns;j++)
            ??????b.pMatrix[j][i]
            =a.pMatrix[i][j];
            ??????
            ??????
            return?b;
            }



            //對(duì)"cout"進(jìn)行重定義
            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);
            }


            //靜態(tài)成員賦初值!
            int?Matrix::ObjectAliveNo=0;

            //主函數(shù)
            int?main()
            {
            ????Matrix?Ma(
            6,3),Mb(3,5);
            ????Ma.SetValue();
            ????Mb.SetValue();
            cout
            <<'\n'<<"現(xiàn)在有"<<Ma.GetObjNo()<<"個(gè)矩陣"<<endl;?????//前后對(duì)比檢查各種函數(shù)對(duì)ObjectAliveNo的影響
            cout<<Ma<<endl;
            Matrix?Mc;
            Mc
            =Ma*Mb;
            cout
            <<Mc;
            cout
            <<'\n'<<"現(xiàn)在有"<<Ma.GetObjNo()<<"個(gè)矩陣"<<endl;?????//見(jiàn)上一條注釋?!!出現(xiàn)錯(cuò)誤:沒(méi)有正常計(jì)數(shù)

            cout
            <<"Ma的轉(zhuǎn)置:"<<endl;
            Matrix?Me;
            Me
            =~Ma;
            cout
            <<Me<<endl;

            Matrix?Md;
            Md.Mul(Ma,Mb);
            cout
            <<'\n'<<"現(xiàn)在有"<<Ma.GetObjNo()<<"個(gè)矩陣"<<endl;????//有沒(méi)有正常計(jì)數(shù)?


            return?0;
            }

            posted on 2006-04-11 13:22 華劍緣 閱讀(724) 評(píng)論(6)  編輯 收藏 引用

            FeedBack:
            # re: [求助] 關(guān)于拷貝構(gòu)造函數(shù),對(duì)象傳遞!!
            2006-04-11 15:01 | 芋頭
            1、構(gòu)造函數(shù)里沒(méi)有對(duì)pMatrix初始化為0值,將導(dǎo)致后面有些地方錯(cuò)誤。
            2、拷貝構(gòu)造函數(shù)和默認(rèn)構(gòu)造函數(shù)只會(huì)調(diào)用一個(gè),你的拷貝構(gòu)造函數(shù)中沒(méi)有初始化pMatrix以及rows和columns。
            3、Matrix::Mul中,if可以寫(xiě)在外面;由于構(gòu)造函數(shù)中沒(méi)有初始化0,c.pMatrix[i][y] +=這里肯定是錯(cuò)誤的;另外,既然a.GetColumns() == b.GetRows(),就沒(méi)有必要用j和x這2個(gè)變量了,一個(gè)就行了。
            4、main函數(shù)里面的2個(gè)for循環(huán),怎么i和j都不用初始化0的嗎?
            5、最后2行,是很不妥。可以考慮寫(xiě)成static,或?qū)懸粋€(gè)全局的operator*。

            暫時(shí)只看出來(lái)這些。至于對(duì)錯(cuò),這個(gè)最好自己調(diào)試。可以把算法轉(zhuǎn)成自己看得懂的語(yǔ)言輸出出來(lái),比如:把矩陣a的第幾行第幾列和矩陣b的第幾行第幾列相乘,加到矩陣c的第幾行第幾列。這樣的話,自己看得懂能排錯(cuò)就行了。  回復(fù)  更多評(píng)論
              
            # re: [求助] 關(guān)于拷貝構(gòu)造函數(shù),對(duì)象傳遞!!
            2006-04-11 16:05 | 任我行
            void Matrix::Mul(Matrix a,Matrix b)給誰(shuí)用呢?
              回復(fù)  更多評(píng)論
              
            # re: [求助] 關(guān)于拷貝構(gòu)造函數(shù),對(duì)象傳遞!!
            2006-04-12 13:44 | 史傳紅
            看到了樓主的這篇文章,我試著改了一下,如下:希望大家一起加入討論。

            #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;
            }   回復(fù)  更多評(píng)論
              
            # re: [求助] 關(guān)于拷貝構(gòu)造函數(shù),對(duì)象傳遞!!
            2006-04-12 20:53 | roa420
            還是有錯(cuò),編譯出現(xiàn)7個(gè)錯(cuò)誤!
            ostream& operator<<(ostream& os,const Matrix& M) 中沒(méi)有權(quán)限訪問(wèn)類(lèi)中私有的rows和columns 這兩個(gè)成員。
              回復(fù)  更多評(píng)論
              
            # re: [求助] 關(guān)于拷貝構(gòu)造函數(shù),對(duì)象傳遞!!
            2006-04-12 21:49 | 華劍緣
            不是呀,我這里編譯,運(yùn)行都沒(méi)出錯(cuò)呀  回復(fù)  更多評(píng)論
              
            # re: [求助] 關(guān)于拷貝構(gòu)造函數(shù),對(duì)象傳遞!!
            2006-04-13 12:48 | 史傳紅
            @roa420
            我在Visual C++ 6.0中編譯的時(shí)候也出現(xiàn)過(guò)問(wèn)題,我懷疑它對(duì)友元支持的不好。
            建議你換一個(gè)編譯器試試看。  回復(fù)  更多評(píng)論
              

            只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            伊人伊成久久人综合网777| 久久精品一区二区三区中文字幕| 久久久久国色AV免费观看| 久久AV高清无码| 精品久久人人爽天天玩人人妻| 人人狠狠综合久久亚洲高清| 99久久精品免费国产大片| 91精品国产高清91久久久久久| 少妇内射兰兰久久| 亚洲欧美成人综合久久久| 精品久久久无码21p发布| 久久精品国产2020| 久久久黄色大片| 久久婷婷午色综合夜啪| 亚洲人成网站999久久久综合| 久久精品国产色蜜蜜麻豆| 久久久精品久久久久久 | 久久久99精品成人片中文字幕 | 久久久久久久久久久免费精品| 国产69精品久久久久99| 国产精品成人精品久久久| 国产精品成人99久久久久| 久久精品亚洲乱码伦伦中文| 久久99精品久久久久久水蜜桃| 久久精品无码一区二区三区日韩 | 国产成人久久激情91| 久久精品九九亚洲精品天堂| 香蕉久久一区二区不卡无毒影院| 99久久精品国产综合一区| 久久综合给合综合久久| 久久久午夜精品| 久久综合给合久久狠狠狠97色69| 国内精品久久人妻互换| 国产精品99久久久久久www| 久久综合九色综合欧美就去吻 | 久久精品国产亚洲AV香蕉| 午夜人妻久久久久久久久| 久久福利青草精品资源站免费| 国产福利电影一区二区三区,免费久久久久久久精 | 麻豆AV一区二区三区久久| 99久久无色码中文字幕|