实验七

实验目的: (1)掌握运算符函数重载为成员函数的方法 (2)掌握运算符函数重载为友元函数的方法 (3)掌握不同类型数据间的转换方法

实验内容: 假设square_matrix是n阶整型方阵,请实现下列运算: (1)cin>> square_matrix (2)cout<< square_matrix (3)IntMatrix(n)生成一个n阶单位矩阵 (4)square_matrix+IntMatrix(n) (5)square_matrix_A=square_matrix_B

注意:重载+运算符为友元函数需要在函数里开辟一个临时的对象temp,但这个对象在函数调用结束后会被销毁。所以重载的=以及<<右边的操作数必须声明为const的引用,否则会报错:||=== Build: Debug in 实验七 (compiler: GNU GCC Compiler) ===|
error: invalid initialization of non-const reference of type 'IntMatrix&' from an rvalue of type 'IntMatrix'

matrix.h

#ifndef MATRIX_H_INCLUDED
#define MATRIX_H_INCLUDED
#include <iostream>//不加的话ostream会报错但istream不会
using namespace std;
class IntMatrix
{
public:
    IntMatrix(int r,int c);
    IntMatrix(int n);
    void Show();
    friend IntMatrix operator+(const IntMatrix &,const IntMatrix &); //对称性的运算符重载为友元函数
    friend ostream& operator<<(ostream &, const IntMatrix &);//=的重载和<<重载对接的参数类型一定要匹配
    friend istream& operator>>(istream &, IntMatrix &);
    IntMatrix & operator=(const IntMatrix & m);//注意是引用类型
    int getRow();
    int getCol();
    ~IntMatrix();
private:
    int **p;
    int row;
    int col;
};

#endif // MATRIX_H_INCLUDED

matrix.cpp

#include "matrix.h"
#include <iostream>
using namespace std;
IntMatrix::IntMatrix(int r,int c)
{
    int i,j;
    row=r;//别忘记把r,c也传进去
    col=c;
    p = new int *[r];
    for(i=0;i<r;i++) p[i]=new int [c];
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            p[i][j]=0;
        }
    }
}
IntMatrix::IntMatrix(int n)
{
    int i,j;
    row=n;//别忘记把r,c也传进去
    col=n;
    p = new int *[n];
    for(i=0;i<n;i++) p[i]=new int [n];
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            p[i][j]=(i==j)?1:0;
        }
    }

}
int IntMatrix::getRow()
{
    return this->row;
}
int IntMatrix::getCol()
{
    return this->col;
}
istream& operator>>(istream & input,IntMatrix & m)
{
    cout<<"The row of matrix is "<<m.row<<" and the column of the matrix is "<<m.col<<endl;
    cout<<"Plz input the correct number of row and col"<<endl;
    int i,j;
    for(i=0;i<m.row;i++)
    {
        for(j=0;j<m.col;j++)
        {
            input>>m.p[i][j];
        }
    }
}
ostream& operator<<(ostream & output,const IntMatrix & m)//如果打成>>会报错row是private的 很神奇
{
    cout<<"The row of matrix is "<<m.row<<" and the column of the matrix is "<<m.col<<endl;
    int i,j;
    for(i=0;i<m.row;i++)
    {
        for(j=0;j<m.col;j++)
        {
            output<<m.p[i][j]<<' ';
        }
        cout<<endl;
    }
}
IntMatrix operator+(const IntMatrix & A,const IntMatrix & B)
{
    IntMatrix temp(A.row,A.col);
    int i,j;
    if(A.row!=B.row || A.col!=B.col)
    {
        cout<<"Can't add!"<<endl;
    }
    else
    {
        for(i=0;i<A.row;i++)
        {
            for(j=0;j<A.col;j++)
            {
                temp.p[i][j]=A.p[i][j]+B.p[i][j];
            }
        }
    }
    return temp;
}
IntMatrix & IntMatrix::operator=(const IntMatrix & m)//注意这一行写法
{
    if(this==&m) return *this;//先判断是否相同
    IntMatrix temp(row,col);
    int i,j;
    if(row!=m.row || col!=m.col)
    {
        cout<<"Wrong!"<<endl;
    }
    else
    {
        for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
            {
                p[i][j]=m.p[i][j];
            }
        }
    }
    return *this;
}
void IntMatrix::Show()
{
    int i,j;
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            cout<<p[i][j]<<' ';
        }
        cout<<endl;
    }
}
IntMatrix::~IntMatrix()
{
    int i;
    for(i=0; i<row; i++)   delete [] p[i];
    delete [] p;
}

main.cpp

#include <iostream>
#include "matrix.h"
using namespace std;

int main()
{
    IntMatrix m(3);
    IntMatrix m1(3);
    IntMatrix m2(3,3);
    cin>>m2;
    cout<<m2+m;
    m=m1+IntMatrix(3);
    cout<<m;
    m1=m2;
    cout<<m1;
    return 0;
}
原文地址:https://www.cnblogs.com/lipoicyclic/p/12886852.html