南京邮电大学MOOC高级程序语言设计(C++)第六章编程题答案

南京邮电大学MOOC高级程序语言设计(C++)第六章编程题答案,第1张

南京邮电大学MOOC高级程序语言设计(C++)第六章编程题答案
1多态性编程(10分)
题目内容:

按以下要求完成编程:

(1)定义一个抽象类shape,用于代表几何图形,设置计算几何图形体积的外部接口;

(2)由shape类派生出圆柱类cylinder、球sphere;圆柱体类型有私有数据成员半径r,高h;球类有私有数据成员半径r;

(3)结合抽象类的指针或引用,实现就算几何图形体积。


(4)纯虚函数定义:virtual double volume() = 0;

(5)圆周率取3.1415。


const double PI = 3.1415;

(6)主函数已给出,请直接复制使用

int main() {

   shape *p;

   double  r,h;

   cin>>r>>h;

   cylinder cy(r,h);

   sphere sp(r);

   p = &cy;

   cout << p->volume() << endl;    		

   p=&sp;

   cout << p->volume() << endl; 

   return 0;

}

参考程序


#include 
 
using namespace std;
const double PI = 3.1415;
class shape
{
	public:
		virtual double volume()=0;
};
class cylinder: public shape
{
	private:
		double r,h;
	public:
		cylinder(double radius, double high):r(radius),h(high)
		{
		}
		double volume()
		{
			return PI*r*r*h;
		}
	};
class sphere:public shape
{
	private:
		double r;
	public:
		sphere(double radius):r(radius)
		{
		}
		double volume()
		{
			return 4*PI*r*r*r/3;
		}
};
int main() {
 shape *p;
 double  r,h;
 cin>>r>>h;
 cylinder cy(r,h);
 sphere sp(r);
 p = &cy;
 cout << p->volume() << endl;    		
 p=&sp;
 cout << p->volume() << endl; 
 return 0;
 }

2运算符重载编程(10分)
题目内容:

设计一个矩阵类,要求矩阵类中重载运算符加(+)和赋值(=),主函数定义类对象并调用重载的运算符。


(1)本题考虑可加(即加与被加矩阵的行列数必须分别相等)和可赋值(等号左值和右值的行列数必须分别相等)情况,如出现不符合条件的运算情况输出“ program terminated! ”,并退出程序结束运行。



(2)要求分别输入矩阵 A 和 B 的行列数,各矩阵元素,然后计算 C =A+B ; A = B ; 并进行输出
(3)定义相应的构造函数和析构函数
(4)类中的成员变量应当有三个:int row,col;分别表示矩阵的行数和列数,另外还需要定义一个一级指针或二级指针m,用来申请动态空间,存放row*col个整数
(5)程序最前面的文件包含请用下面代码:

#include 
#include 
using namespace std;

(6)main()已给出,请直接复制使用

int main()
{
	int row_a,col_a,row_b,col_b;
	cin>>row_a>>col_a;
	Matrix A(row_a,col_a);
	cin>>row_b>>col_b;
	Matrix B(row_b,col_b),C;	
	C = A + B;

	C.disp();
	A = B;
	A.disp();
	return 0;
}

参考程序

#include
using namespace std;
class Matrix
{
	private:
		int row,col;
		int *p;
	public:
		Matrix(int r,int c):row(r),col(c)
		{
			p = new int[c*r];
			for (int i=0;i<row;i++)
			{
				for (int j=0;j<col;j++)
					cin>>p[i*col+j];
			}
		}
		Matrix()
		{
			p = new int[9];
		}
		void disp()
		{
			for(int i=0;i<row;i++)
			{
				for(int j=0;j<col;j++)
					cout<<p[i*col+j]<<'\t';
				cout<<endl;
			}
		}
		Matrix operator+(Matrix &O)
		{
			if (col!=O.col || row!=O.row)
			{
				cout<<"program terminated!"<<endl;
				exit(0);
			}
			Matrix temp;
			temp.col=col;
			temp.row=row;
			for(int i=0;i<row;i++)
				for(int j=0;j<col;j++)
					temp.p[i*col+j] = p[i*col+j] + O.p[i*col+j];
			return temp;
		}
		Matrix operator=(Matrix D)
		{
			col=D.col;
			row=D.row;
			for (int i = 0; i <row; i++)
			{	
				for (int j = 0; j <col; j++)
					p[i*col+j] = D.p[i*col+j];
			}
			return *this;
		}
 
};
int main()
{
	int row_a,col_a,row_b,col_b;
	cin>>row_a>>col_a;
	Matrix A(row_a,col_a);
	cin>>row_b>>col_b;
	Matrix B(row_b,col_b),C;	
	C = A + B;
	C.disp();
	cout<<endl;
	A = B;
	A.disp();
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/584769.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存