南邮程序设计类课程教辅平台C++第3版第六章作业

南邮程序设计类课程教辅平台C++第3版第六章作业,第1张

因为太懒所以写迟了。




1. (14分) 6-5-3动态多态性

题目描述
定义一个抽象类shape,用于代表几何图形,设置计算几何图形体积的外部接口;由shape类派生出圆柱类cylinder、球sphere;圆柱体类型有私有数据成员半径r,高h;球类有私有数据成员半径r;结合抽象类的指针或引用,实现就算几何图形体积。


纯虚函数定义:double volume()=0;
(6)main()已给出,请直接复制使用

int main() {
shape *p;
double r,h;
cout<<“input r & h:”< cin>>r>>h;
cylinder cy(r,h);
sphere sp®;
p=&cy;
cout< volume()< p=&sp;
cout< volume()< return 0;
}
输入描述

输出描述

样例输入1:
input r & h:
2 5
样例输出1:
62.83
33.5093

#include
using namespace std;
const double pi=3.1415;
class shape{
	public:
		virtual double volume()=0;
};
class cylinder:public shape{
	private:
		double r;
		double h;
	public:
		cylinder(double r, double h){
			this->r=r;
			this->h=h;
		}
		virtual double volume(){
			return pi*r*r*h;
		}
};
class sphere:public shape{
	private:
		double r;
	public:
		sphere(double r){
			this->r=r;
		}
		virtual double volume(){
			return 4.0/3*pi*r*r*r;
		}
};
int main() {
   shape *p;
   double  r,h;
   cout<<"input r & h:"<<endl;
   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. (16分) 6-5-2矩阵类运算符重载

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


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



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

#include
#include
using namespace std;
(6)main()已给出,请直接复制使用
int main()
{
int row_a,col_a,row_b,col_b;
cout<<“请输入am矩阵的行数和列数:”< cin>>row_a>>col_a;
Matrix am(row_a,col_a);
cout<<“请输入bm矩阵的行数和列数:”< cin>>row_b>>col_b;
Matrix bm(row_b,col_b),cm;
cout<<“am:”< am.disp();
cout<<“bm:”< bm.disp();
cm=am+bm;
cout<<“cm=am+bm:”< cm.disp();
am=bm;
cout<<“am=bm:”< am.disp();
return 0;
}
(7)类的成员函数disp的代码已给出,请直接复制使用:
void Matrix::disp()
{
for(int i=0;i {
cout<<‘\t’;
for(int j=0;j cout<<(m+icol+j)<<‘\t’;
cout< }
}
输入描述
见输入样例

输出描述
见输出样例

样例输入1:
请输入am矩阵的行数和列数:
3 3<回车>
请输入该矩阵元素:
1 2 3 4 5 6 7 8 9<回车>
请输入bm矩阵的行数和列数:
3 3<回车>
请输入该矩阵元素:
9 8 7 6 5 4 3 2 1<回车>
样例输出1:
am:
1 2 3
4 5 6
7 8 9
bm:
9 8 7
6 5 4
3 2 1
cm=am+bm:
10 10 10
10 10 10
10 10 10
am=bm:
9 8 7
6 5 4
3 2 1
样例输入2:
请输入am矩阵的行数和列数:
3 4<回车>
请输入该矩阵元素:
1 2 3 4 5 6 7 8 9 10 11 12<回车>
请输入bm矩阵的行数和列数:
3 3<回车>
请输入该矩阵元素:
9 8 7 6 5 4 3 2 1<回车>
样例输出2:
am:
1 2 3 4
5 6 7 8
9 10 11 12
bm:
9 8 7
6 5 4
3 2 1
program terminated!

#include
using namespace std;
class Matrix{
	private:
		int *m;
		int row;
		int col;
	public:
		Matrix(int r,int c){
			row=r;
			col=c;
			m=new int[row*col];
			cout<<"请输入该矩阵元素:"<<endl;
			for(int i=0;i<row;i++){
				for(int j=0;j<col;j++)
					cin>>m[i*col+j]; 
			}
		}
		Matrix(){
			row=0;
			col=0;
			m=NULL;
		}
		~Matrix(){
			delete []m;
		}	
		friend Matrix &operator+(Matrix &a,Matrix &b){
			if(a.col!=b.col||a.row!=b.row){
				cout<<"program terminated!"<<endl;
				exit(0);
			}
			else
				for(int i=0;i<a.row;i++){
					for(int j=0;j<a.col;j++)
						(*(a.m+i*a.col+j))+=(*(b.m+i*b.col+j));
				}
			return a;
		}
		Matrix &operator=(Matrix &a){
			delete []m;
			m=new int[a.row*a.col];
			row=a.row;
			col=a.col;
			for(int i=0;i<a.row;i++){
					for(int j=0;j<a.col;j++)
						*(m+i*col+j)=*(a.m+i*a.col+j);
				}
			return *this;
		}
		void disp(){
			for(int i=0;i<row;i++)
		{
			cout<<'\t';
			for(int j=0;j<col;j++)
				cout<<*(m+i*col+j)<<'\t';
			cout<<endl;
		}
		}
};

int main()
{
	int row_a,col_a,row_b,col_b;
	cout<<"请输入am矩阵的行数和列数:"<<endl;
	cin>>row_a>>col_a;
	Matrix am(row_a,col_a);
	cout<<"请输入bm矩阵的行数和列数:"<<endl;
	cin>>row_b>>col_b;
	Matrix bm(row_b,col_b),cm;
	cout<<"am:"<<endl;
	am.disp();
	cout<<"bm:"<<endl;
	bm.disp();
	cm=am+bm;
	cout<<"cm=am+bm:"<<endl;
	cm.disp();
	am=bm;
	cout<<"am=bm:"<<endl;
	am.disp();
	return 0;
}

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

原文地址: https://outofmemory.cn/langs/564698.html

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

发表评论

登录后才能评论

评论列表(0条)

保存