C++面向对象 第二次实验报告

  1、重载下面点类Point的运算符“<<”和“>>”,并测试该运算符。

#include  <iostream>

using namespace std;

class Point

{

  double x , y ;

public:

Point(){};

//重载运算符">>"

    friend istream & operator >> (istream &in, Point &p) ;

//重载运算符"<<"

   friend ostream & operator << (ostream &out, Point &p) ;  

} ;

#include  <iostream>
using namespace std;
class Point
{
	double x, y;
public:
	Point() {};
	//重载运算符">>"
	friend istream& operator >> (istream& in, Point& p);
	//重载运算符"<<" 
	friend ostream& operator << (ostream& out, Point& p);
};
istream& operator >> (istream& in, Point& p)
{
	cout << "please cin the x and y" << endl;
	in >> p.x >> p.y;
	return in;
}
ostream& operator << (ostream& out, Point& p)
{
	out << "x=" << p.x << " " << "y=" << p.y << endl;
	return out;
}
int main()
{
	Point a;
	Point b;
	cin >> a;
	cin >> b;
	cout << "a: " << a << endl;
	cout << "b: " << b << endl;
}

  2、假设有两个均为3行3列的矩阵ml和m2,编写程序实现矩阵类Matrix,并重载运算符“+”、“>>”和“<<”,使之能用于矩阵m1和m2相加、输入和输出。

#include <iostream>
using namespace std;
class Matrix
{
	friend istream& operator >> (istream& in, Matrix& p);
	friend ostream& operator << (ostream& out, Matrix& p);
	friend Matrix operator+(Matrix& a, Matrix& b);
private:
	int m_row;//行
	int m_col;//列
	double* m_mat;//一维数组转化为二维数组 a[i*m_col+j]相当于a[i][j]
public:
	Matrix()
	{
		int row;
		int col;
		cout << "please cin row and col" << endl;
		cin >> row >> col;
		m_row = row;
		m_col = col;
		m_mat = new double[m_row * m_col];
	}
	Matrix(int row, int col)
	{
		this->m_row = row;
		this->m_col = col;
		this->m_mat = new double[m_row * m_col];
	}
	Matrix(const Matrix& p)
	{
		this->m_row = p.m_row;
		this->m_col = p.m_col;
		this->m_mat = new double[this->m_row * this->m_col];
		for (int i = 0; i <this->m_row; i++)
		{
			for (int j = 0; j < this->m_col; j++)
			{
				this->m_mat[i * this->m_col + j] = p.m_mat[i * p.m_col + j];
			}
		}
	}
	~Matrix()
	{
		if (m_mat != nullptr)
		{
			delete[]m_mat;
			m_mat = nullptr;
		}
		m_row = 0;
		m_col = 0;
	}
	/*Matrix operator=(Matrix p)
	{
		this->m_row = p.m_row;
		this->m_col = p.m_col;
		this->m_mat = new double[m_row * m_col];
		for (int i = 0; i < this->m_row; i++)
		{
			for (int j = 0; j < this->m_col; j++)
			{
				this->m_mat[i * this->m_col + j] = p.m_mat[i * p.m_col + j];
			}
		}
		return *this;
	}*/
};
istream& operator >> (istream& in, Matrix& p)
{
	for (int i = 0; i < p.m_row; i++)
	{
		for (int j = 0; j < p.m_col; j++)
		{
			in >> p.m_mat[i * p.m_col + j];
		}
	}
	return in;
}
ostream& operator << (ostream& out, Matrix& p)
{
	for (int i = 0; i < p.m_row; i++)
	{
		for (int j = 0; j < p.m_col; j++)
		{
			out << p.m_mat[i * p.m_col + j] << " ";
		}
		cout << endl;
	}
	return out;
}
Matrix operator+(Matrix& a, Matrix& b)
{
	if (a.m_row == b.m_row && a.m_col == b.m_col)
	{
		Matrix temp(a.m_row, a.m_col);
		for (int i = 0; i < temp.m_row; i++)
		{
			for (int j = 0; j < temp.m_col; j++)
			{
				temp.m_mat[i * temp.m_col + j] = a.m_mat[i * a.m_col + j] + b.m_mat[i * b.m_col + j];
			}
		}
		return temp;
	}
	else
	{
		cout << "两矩阵不可相加" << endl;
		exit(-1);
	}
}
int main()
{
	Matrix m1;
	Matrix m2;
	cout << "please cin m1" << endl;
	cin >> m1;
	cout << "please cin m2" << endl;
	cin >> m2;
	cout << "m1" << endl << m1 << endl;
	cout << "m2" << endl << m2 << endl;
	Matrix m3 = m1 + m2;
	/*Matrix m3;
	m3 = m1 + m2;*/
	cout << "m1+m2" << endl << m3 << endl;
}

日后谈:第二题如果重载赋值构造函数,必须要用值传递,不能用引用,不知道为啥。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值