**一.**定义一个抽象类shape,用于代表几何图形,设置计算几何图形体积的外部接口;由shape类派生出圆柱类cylinder、球sphere;圆柱体类型有私有数据成员半径r,高h;球类有私有数据成员半径r;结合抽象类的指针或引用,实现就算几何图形体积。纯虚函数定义:double volume()=0;main()已给出,请直接复制使用
int main()
{
shape *p;
double r,h;
cout<<“input r & h:”<<endl;
cin>>r>>h;
cylinder cy(r,h);
sphere sp®;
p=&cy;
cout< volume()<<endl;
p=&sp;
cout< volume()<<endl;
return 0;
}
输入描述
无
输出描述
无
样例输入1:
input r & h:
2 5
样例输出1:
62.83
33.5093
#include<iostream>
#include<string.h>
#include <iomanip>
using namespace std;
#pragma warning(disable:4996)
class shape
{
protected:
double radius;
public:
shape(double r)
{
radius = r;
}
shape()
{
radius = 0;
}
virtual double area() = 0; //求表面积
virtual double volume() = 0; //求体积
virtual void print() = 0; //输出相关信息
};
class sphere:public shape //球体
{
private:
int radius;
public:
sphere(int r)
{
radius = r;
}
virtual double area() //求表面积
{
return 4 * 3.1415 * radius * radius;
}
virtual double volume() //求体积
{
return (3.1415 * (4.0 / 3) * radius * radius * radius);
}
virtual void print() //输出相关信息
{
cout << "球体表面积为:" << area() << " 球体体积为:" << volume() << endl;
}
};
class cylinder :public shape //圆柱体
{
private:
double height;
double radius;
public:
cylinder(double r, double h)
{
radius = r;
height = h;
}
virtual double area() //求表面积
{
return 2 * 3.1415 * radius *(radius + height);
}
virtual double volume() //求体积
{
return 3.1415 * height * radius * radius;
}
virtual void print() //输出相关信息
{
cout << "圆柱体表面积为:" << area() << " 圆柱体体积为:" << volume() << endl;
}
};
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 << setiosflags(ios::fixed) << setprecision(2);
cout << p->volume() << endl;
p = &sp;
cout << setiosflags(ios::fixed) << setprecision(4);
cout << p->volume() << endl;
getchar();
getchar();
return 0;
}
**二.**设计一个矩阵类,要求矩阵类中重载运算符加(+)和赋值(=),主函数定义类对象并调用重载的运算符。
提示:
(1) 本题考虑可加(即加与被加矩阵的行列数必须分别相等)和可赋值(等号左值的行列数变为右值的行列数)情况,其他情况输出“ program terminated! ”
(2) 要求分别输入矩阵 am 和 bm 的行列数,各矩阵元素,分别计算 cm=am+bm;am=bm; 并进行输出
(3) 定义相应的构造函数和析构函数
(4) 类中的成员变量应当有三个:int row,col;分别表示矩阵的行数和列数,另外还需要定义一个一级指针或二级指针用来申请动态空间,存放rowcol个整数
(5) 程序最前面的文件包含请用下面代码:
#include
#include <stdlib.h>
using namespace std;
(6)main()已给出,请直接复制使用
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;
}
(7)类的成员函数disp的代码已给出,请直接复制使用:
void Matrix::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;
}
}
样例输入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<iostream>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
using namespace std;
#pragma warning(disable:4996)
class Matrix
{
int *m, row, col;
public:
Matrix(int r, int c)
{
row = r;
col = c;
m = new int[row*col];
cout << "请输入该矩阵元素:";
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cin >> m[i*col + j];
}
}
}
Matrix()
{
row = col = 0;
m = NULL;
}
~Matrix()
{
delete m;
}
Matrix & operator=(Matrix &mat)
{
delete m;
m = new int[mat.row*mat.col];
row = mat.row;
col = mat.col;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
*(m + col * i + j) = *(mat.m + i * mat.col + j);
}
}
return *this;
}
void disp();
friend Matrix &operator+(Matrix &, Matrix &);
};
void Matrix::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;
}
}
Matrix &operator+(Matrix &mat1, Matrix &mat2)
{
if (mat1.col != mat2.col || mat1.row != mat2.row)
{
cout << "program terminated!" << endl;
exit(0);
}
else
{
for (int i = 0; i < mat1.row; i++)
{
for (int j = 0; j < mat1.col; j++)
{
(*(mat1.m + i * mat1.col + j)) += (*(mat2.m + i * mat2.col + j));
}
}
}
return mat1;
}
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();
while (1)
{
getchar();
}
return 0;
}
#include <iostream>
#include <stdlib.h>
using namespace std;
class Matrix
{
protected:
int row;
int col;
int *m;
public:
Matrix(int r, int c);
Matrix(){row=col=0;m=NULL;}
friend Matrix &operator+(Matrix &M1,Matrix &M2);
Matrix &operator=(Matrix &M);
void disp();
~Matrix();
};
Matrix::Matrix(int r, int c)
{
row=r;
col=c;
int n=row*col;
m=new int[n];
cout<<"请输入该矩阵元素:"<<endl;
for(int i=0;i<n;i++)
cin>>m[i];
}
Matrix &operator+(Matrix &M1,Matrix &M2)
{
if(M1.row!=M2.row||M1.col!=M2.col)
{cout<<"program terminated!"<<endl;
exit(0);
}
if(M1.row==M2.row&&M1.col==M2.col)
{
for(int i=0;i<M1.row*M1.col;i++)
M1.m[i]=M1.m[i]+M2.m[i];
return M1;
}
}
Matrix& Matrix::operator=(Matrix &M)
{
delete m;
m=new int[M.col*M.row];
row=M.row;
col=M.col;
if(row==M.row&&col==M.col)
{
delete m;
m=new int[M.col*M.row];
for(int i=0;i<row*col;i++)
m[i]=M.m[i];
return *this;
}
}
void Matrix::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;
}
}
Matrix::~Matrix()
{
delete []m;
}
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;
}