C++语言程序设计实验报告——实验五

一、实验目的及要求

掌握运算符重载的方法;
掌握使用虚函数的继承实现动态多态性,掌握纯虚函数及抽象类的使用。

二、实验环境

硬件要求:计算机一台。
软件要求:Windows操作系统,Dev-C++或VC++ 6.0编译环境

三、实验内容

实验题目(1)

有两个矩阵A和B,均为3行3列。重载运算符“+”和“”,使之分别能用于矩阵相加和相乘。C = A+B, C = AB。

class Matrix
{
public:
	Matrix();
	Matrix(int matrix[3][3]);
	Matrix operator + (Matrix &m);
	Matrix operator * (Matrix &m);
	void display()const;
private:
	int m_matrix[3][3];
};
int main(void)
{
	int a[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
	int b[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

	Matrix m1(a);
	Matrix m2(b);
	
	Matrix m3 = m1+m2;
	Matrix m4 = m1*m2;
	
	m3.display();
	cout << "-------------------" << endl;
	m4.display();
	
	return 0;
}

多态是面上对象程序设计比较重要的一块知识点,多态能方便程序的编写,扩展性强,针对这个题目来说,博主认为拿来练手非常好,难度并不高,挺能适应初学者的。话不多说,上代码:

#include<iostream>
using namespace std;
class Matrix
{
public:
	Matrix();
	Matrix(int matrix[3][3]);
	Matrix operator + (const Matrix& m)const;
	Matrix operator * (const Matrix& m)const;
	void display()const;
private:
	int m_matrix[3][3];
};
Matrix::Matrix() {
	for (int i=0;i<3;i++) {
		for (int j=0;j<3;j++) {
			m_matrix[i][j] = 1;
		}
	}
}
Matrix::Matrix(int matrix[3][3]){
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			m_matrix[i][j] = matrix[i][j];
		}
	}
}
Matrix Matrix ::operator +(const Matrix& m)const{
	Matrix sum;
	for (int i=0;i<3;i++) {
		for (int j=0;j<3;j++) {
			sum.m_matrix[i][j] = m_matrix[i][j] + m.m_matrix[i][j]-1;
		}
	}
	return sum;
}
Matrix Matrix ::operator *(const Matrix& m)const {
	Matrix product;
	for (int i=0;i<3;i++) {
		for (int j=0;j<3;j++) {
			product.m_matrix[i][j] = m_matrix[i][j] * m.m_matrix[i][j];
		}
	}
	return product;
}
void Matrix :: display() const {
	for (int i=0;i<3;i++) {
		for (int j=0;j<3;j++) {
			cout << m_matrix[i][j] << "	";
		}
		cout << endl;
	}
}
int main(void)
{
	int a[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	int b[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	Matrix m1(a);
	Matrix m2(b);
	Matrix m3 = m1 + m2;
	Matrix m4 = m1 * m2;
	cout << "A+B" << endl;
	m3.display();
	cout << "-------------------" << endl;
	cout << "A×B" << endl;
	m4.display();
	return 0;
}

运行结果如图所示:
题目一运行结果


实验题目(2)

定义抽象类Geometry,含有计算几何图形周长的纯虚函数double Circumfer();含有计算面积的纯虚函数double Area();在此基础上公有的派生出矩形类Rectangle,和圆类Circle,并且分别给出计算周长函数Circumf()和计算面积函数Area()的定义。最后通过函数fun()分别输出面积和周长。可以在主函数中使用以下语句来测试类。

int main()
{
	Rectangle x1(2,3);
     Rectangle *p1;//(需补充)使指针指向对象x1
     cout<<"Rectangle:"<<endl;
     fun(p1);

     Circle y1(5);
     Circle *p2;
     …
     cout<<"Circle:"<<endl;
     fun(p2);
	
	return 0;
}

博主认为,这道题目的难度系数也不是很高,熟练一点的一般都能够很快编写完成并且得到正确结果。
代码如下:

#include<iostream>
#include<string>
#include<cstring>
#include<cstdlib>
using namespace std;
const double PI = 3.1415926;
class Geometry {
public:
	virtual double Circumfer() = 0;
	virtual double Area() = 0;
};
class Rectangle :public Geometry {
public:
	Rectangle(double xx,double yy) {
		this->x = xx;
		this->y = yy;
	}
	Rectangle(){}
	~Rectangle(){}
	virtual double Circumfer() {
		return 2 * (x + y);
	}
	virtual double Area() {
		return x * y;
	}
	void fun(Rectangle* p);
private:
	double x, y;			//长、宽
};
class Circle :public Geometry {
public:
	Circle(double rr) {
		this->r = rr;
	}
	Circle(){}
	~Circle(){}
	virtual double Circumfer() {
		return 2 * PI * r;
	}
	virtual double Area() {
		return PI * r * r;
	}
	void fun(Circle* p);
private:
	double r;
};
void fun(Geometry *p){
		cout << "周长为:" << p->Circumfer() << endl;
		cout << "面积为:" << p->Area() << endl;
	}
int main()
{
	Rectangle x1(2, 3);
	Rectangle* p1;
	p1 = &x1;
	cout << "Rectangle:" << endl;
	fun(p1);
	Circle y1(5);
	Circle* p2;
	p2 = &y1;
	cout << "Circle:" << endl;
	fun(p2);
	return 0;
}

运行结果如图所示:
题目2运行结果

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刚入坑的软件猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值