c++程序设计求解 10
一、完成程序,该程序说明如下:1、定义一个表示形状的基类CShape,它包括以下成员:1)doublePerimeter(void),用于求周长,纯虚函数,公有成员2)d...
一、 完成程序,该程序说明如下:
1、 定义一个表示形状的基类CShape,它包括以下成员:
1) double Perimeter(void),用于求周长,纯虚函数,公有成员
2) double Area(void),用于求面积,纯虚函数,公有成员
3) char *m_ObjName,表示对象名称,私有成员
4) void ShowName(void),用于显示对象名称,公有成员。若m_ObjName为空,则显示”No name”,否则显示其名称。
5) 公有成员CShape(void),作用是将m_ObjName设置为NULL
6) 公有成员CShape(char *objname),作用是将objname的内容复制到m_ObjName中
7) 无析构函数
2、 定义矩形类CRectangle、三角形类CTriangle、椭圆形类CEllipse,这三个类均以public方式派生自CShape类。根据这三个类的具体用途,添加相关的数据成员和函数成员,实现其所有成员函数和继承自基类的纯虚函数。
3、 主函数的功能:
1) 定义基类指针pBase
2) 显示一个菜单,包括矩形、三角形、椭圆三个选项,选择不同选项时,创建相应的对象,令pBase指向该对象。
3) 显示pBase所指向对象的面积和周长。
4、 说明:上述4个对象和主函数均放在同一个CPP文件中,文件名称自定义。 展开
1、 定义一个表示形状的基类CShape,它包括以下成员:
1) double Perimeter(void),用于求周长,纯虚函数,公有成员
2) double Area(void),用于求面积,纯虚函数,公有成员
3) char *m_ObjName,表示对象名称,私有成员
4) void ShowName(void),用于显示对象名称,公有成员。若m_ObjName为空,则显示”No name”,否则显示其名称。
5) 公有成员CShape(void),作用是将m_ObjName设置为NULL
6) 公有成员CShape(char *objname),作用是将objname的内容复制到m_ObjName中
7) 无析构函数
2、 定义矩形类CRectangle、三角形类CTriangle、椭圆形类CEllipse,这三个类均以public方式派生自CShape类。根据这三个类的具体用途,添加相关的数据成员和函数成员,实现其所有成员函数和继承自基类的纯虚函数。
3、 主函数的功能:
1) 定义基类指针pBase
2) 显示一个菜单,包括矩形、三角形、椭圆三个选项,选择不同选项时,创建相应的对象,令pBase指向该对象。
3) 显示pBase所指向对象的面积和周长。
4、 说明:上述4个对象和主函数均放在同一个CPP文件中,文件名称自定义。 展开
1个回答
展开全部
#include<iostream>
#include<cmath>
#include<iomanip>
#ifndef CSHAPE_H
#define CSHAPE_h
Using namespace std;
Const float PI=3.1;
class CShape //CShape抽象类定义
{
public:
virtualfloat length() //图形长度
{
return 0.0;
}
virtualfloat area()//图形面积
{
return 0.0;
}
virtualfloat valum()//图形体积
{
return 0.0;
}
virtualfloat zc()//图形周长
{
return 0.0;
}
virtualvoid draw()=0;//描绘图形
};
class CPoint:virtualpublic CShape//CPoint类定义
protected:
float x,y;
public:
CPoint(float a,float b)
{
x=a;
y=b;
}
void setpoint(float a,float b)
{
x=a;
y=b;
}
void draw()
{
cout<<"点的坐标为:"<<"("<<x<<","<<y<<")"<<endl;
}
};
class CLine:virtualpublic CShape//线类定义
{
protected:
float x,y,z,k;
public:
CLine(float a,float b,float c,float d)
{
x=a;
y=b;
z=c;
k=d;
}
void setline(float a,float b,float c,float d)
{
x=a;
y=b;
z=c;
k=d;
}
float length()
{
return(sqrt((x-z)*(z-z)+(y-k)*(y-k)));
}
void draw()
{
cout<<"两坐标分别为:"<<"("<<x<<","<<y<<")"<<",("<<z<<","<<k<<")"<<endl;
}
};
class CRectangle:virtualpublic CShape//长方形类定义
{
protected:
float length,width;
public:
CRectangle(float l,float w)
{
length=l;
width=w;
}
void setrectangle(float l,float w)
{
length=l;
width=w;
}
float area()
{
return(length*width);
}
float zc()
{
return(2*(length+width));
}
void draw()
{
cout<<"长方形的长为:"<<length<<setw(10)<<"宽为:"<<width<<endl;
}
};
class CCircle:virtualpublic CShape//圆类定义
{
protected:
float radius;
public:
CCircle(float r)
{
radius=r;
}
void setcircle(float r)
{
radius=r;
}
float area()
{
return(PI*radius*radius);
}
float zc()
{
return(2*PI*radius);
}
void draw()
{
cout<<"圆的半径为:"<<radius<<endl;
}
};
class CCylinder:virtualpublic CShape//圆柱类定义
{
protected:
float heigth,radius;
public:
CCylinder(float r,float h)
{
heigth=h;
radius=r;
}
void setcylinder(float r,float h)
{
heigth=h;
radius=r;
}
float area()
{
return(PI*radius*radius+2*PI*radius);
}
float valum()
{
return(PI*radius*radius*heigth);
}
void draw()
{
cout<<"圆柱高度为:"<<heigth<<setw(10)<<"半径为:"<<radius<<endl;
}
class CCone:virtualpublic CShape//定义一个圆锥类
{
protected:
float mx,radius;
public:
CCone(float a,float b)
{
mx=a;
radius=b;
}
void setcone(float a,float b)
{
mx=a;
radius=b;
}
float valum()
{
return(PI*radius*radius*sqrt(mx*mx-radius*radius));
}
void draw()
{
cout<<"母线长为:"<<mx<<setw(10)<<"半径长为:"<<radius<<endl;
}
};
class CTriangle:virtualpublic CShape//三角形类定义
protected:
float x,y,z;
public:
CTriangle(float a,float b,float c)
{
x=a;y=b;z=c;
}
void settriangle(float a,float b,float c)
{
x=a;y=b;z=c;
}
float zc()
{
return(x+y+z);
}
float area()
{
float t=(x+y+z)/2;
return(sqrt((t-x)*(t-y)*(t-z)));
}
void draw()
{
cout<<"三角形三边长分别为:"<<x<<","<<y<<","<<z<<endl;
}
};
class CPolygon:virtualpublic CShape//多边形类定义
{
protected:
float x;
int y;
public:
CPolygon(float a,int b)
{
x=a;
y=b;
}
void setpolygon(float a,int b)
{
x=a;
y=b;
}
float area()
{
return(sqrt(3.0)*x*x*y/4);
}
float zc()
{
return(x*y);
}
void draw()
{
cout<<"多边形的一边长为:"<<x<<setw(10)<<"边数为:"<<y<<endl;
}
};
#endif
#include"CShape.h"
#include<iostream>
#include<iomanip>
usingnamespace std;
void main()
{
char d='y';
int i(0);
cout<<"欢迎进入本系统!";
cout<<"请选择服务:"<<endl;
cout<<" 1.点的计算。"<<endl;
cout<<" 2.线的计算。"<<endl;
cout<<" 3.圆的计算。"<<endl;
cout<<" 4.圆柱的计算。"<<endl;
cout<<" 5.圆锥的计算。"<<endl;
cout<<" 6.长方形的计算。"<<endl;
cout<<" 7.多边形的计算。"<<endl;
cout<<" 8.三角形的计算。"<<endl;
cout<<" 9.退出。"<<endl;
while(d=='Y'||d=='y')
{
cout<<"请输入你的选择:"<<endl;
cin>>i;
if(i==1)
{
CShape *pshape;
CPoint point(2.0,3.0);
float a,b;
cout<<"请输入点的坐标:";
cin>>a>>b;
point.setpoint(a,b);
pshape=&point;
pshape->draw();
}
elseif(i==2)
{
CShape *pshape;
CLine line(1.0,2.0,3.0,4.0);
float a,b,c,d;
cout<<"请依次输入两端点的坐标:";
cin>>a>>b>>c>>d;
line.setline(a,b,c,d);
pshape=&line;
pshape->draw();
cout<<"线的长度为:"<<pshape->length()<<endl;
}
elseif(i==3)
{
CShape *pshape;
CCircle circle(2.0);
float a;
cout<<"请输入圆的半径:";
cin>>a;
circle.setcircle(a);
pshape=&circle;
pshape->draw();
cout<<"圆的面积为;"<<pshape->area()<<setw(10)<<"圆的周长为;"<<pshape->zc()<<endl;
}
elseif(i==4)
{
CShape *pshape;
CCylinder yz(2.0,3.0);
float a,b;
cout<<"请输入圆柱的底面半径和高:";
cin>>a>>b;
yz.setcylinder(a,b);
pshape=&yz;
pshape->draw();
cout<<"圆柱的面积为:"<<pshape->area()<<setw(10)<<"圆柱的体积为:"<<pshape->valum()<<endl;
}
elseif(i==5)
{
CShape *pshape;
CCone cone(5.0,4.0);
float a,b;
cout<<"请输入母线长和底面半径:";
cin>>a>>b;
cone.setcone(a,b);
pshape=&cone;
pshape->draw();
cout<<"圆锥的体积为:"<<pshape->valum()<<endl;
}
elseif(i==6)
{
CShape *pshape;
CRectangle rectangle(3.0,2.0);
float a,b;
cout<<"请输入长方形的长和宽:";
cin>>a>>b;
rectangle.setrectangle(a,b);
pshape=&rectangle;
pshape->draw();
cout<<"长方形的面积为:"<<pshape->area()<<setw(10)<<"长方形的周长为:"<<pshape->zc()<<endl;
}
elseif(i==7)
{
CShape *pshape;
CPolygon polygon(2.0,4);
float a;
int b;
cout<<"请输入多边形的边长和边数:";
cin>>a>>b;
polygon.setpolygon(a,b);
pshape=&polygon;
pshape->draw();
cout<<"多边形的面积为:"<<pshape->area()<<setw(10)<<"周长为:"<<pshape->zc()<<endl;
}
elseif(i==8)
{
CShape *pshape;
CTriangle triangle(3.0,4.0,5.0);
float a,b,c;
cout<<"请输入三角形三边长:";
cin>>a>>b>>c;
triangle.settriangle(a,b,c);
pshape=▵
pshape->draw();
cout<<"三角形的周长为:"<<pshape->zc()<<"面积为:"<<pshape->area()<<endl;
}
elseif(i==9)
{
return;
}
else
{
cout<<"输入错误,请重新输入!";
}
cout<<"是否继续其他操作(Y/N)?"<<endl;
cin>>d;
}
#include<cmath>
#include<iomanip>
#ifndef CSHAPE_H
#define CSHAPE_h
Using namespace std;
Const float PI=3.1;
class CShape //CShape抽象类定义
{
public:
virtualfloat length() //图形长度
{
return 0.0;
}
virtualfloat area()//图形面积
{
return 0.0;
}
virtualfloat valum()//图形体积
{
return 0.0;
}
virtualfloat zc()//图形周长
{
return 0.0;
}
virtualvoid draw()=0;//描绘图形
};
class CPoint:virtualpublic CShape//CPoint类定义
protected:
float x,y;
public:
CPoint(float a,float b)
{
x=a;
y=b;
}
void setpoint(float a,float b)
{
x=a;
y=b;
}
void draw()
{
cout<<"点的坐标为:"<<"("<<x<<","<<y<<")"<<endl;
}
};
class CLine:virtualpublic CShape//线类定义
{
protected:
float x,y,z,k;
public:
CLine(float a,float b,float c,float d)
{
x=a;
y=b;
z=c;
k=d;
}
void setline(float a,float b,float c,float d)
{
x=a;
y=b;
z=c;
k=d;
}
float length()
{
return(sqrt((x-z)*(z-z)+(y-k)*(y-k)));
}
void draw()
{
cout<<"两坐标分别为:"<<"("<<x<<","<<y<<")"<<",("<<z<<","<<k<<")"<<endl;
}
};
class CRectangle:virtualpublic CShape//长方形类定义
{
protected:
float length,width;
public:
CRectangle(float l,float w)
{
length=l;
width=w;
}
void setrectangle(float l,float w)
{
length=l;
width=w;
}
float area()
{
return(length*width);
}
float zc()
{
return(2*(length+width));
}
void draw()
{
cout<<"长方形的长为:"<<length<<setw(10)<<"宽为:"<<width<<endl;
}
};
class CCircle:virtualpublic CShape//圆类定义
{
protected:
float radius;
public:
CCircle(float r)
{
radius=r;
}
void setcircle(float r)
{
radius=r;
}
float area()
{
return(PI*radius*radius);
}
float zc()
{
return(2*PI*radius);
}
void draw()
{
cout<<"圆的半径为:"<<radius<<endl;
}
};
class CCylinder:virtualpublic CShape//圆柱类定义
{
protected:
float heigth,radius;
public:
CCylinder(float r,float h)
{
heigth=h;
radius=r;
}
void setcylinder(float r,float h)
{
heigth=h;
radius=r;
}
float area()
{
return(PI*radius*radius+2*PI*radius);
}
float valum()
{
return(PI*radius*radius*heigth);
}
void draw()
{
cout<<"圆柱高度为:"<<heigth<<setw(10)<<"半径为:"<<radius<<endl;
}
class CCone:virtualpublic CShape//定义一个圆锥类
{
protected:
float mx,radius;
public:
CCone(float a,float b)
{
mx=a;
radius=b;
}
void setcone(float a,float b)
{
mx=a;
radius=b;
}
float valum()
{
return(PI*radius*radius*sqrt(mx*mx-radius*radius));
}
void draw()
{
cout<<"母线长为:"<<mx<<setw(10)<<"半径长为:"<<radius<<endl;
}
};
class CTriangle:virtualpublic CShape//三角形类定义
protected:
float x,y,z;
public:
CTriangle(float a,float b,float c)
{
x=a;y=b;z=c;
}
void settriangle(float a,float b,float c)
{
x=a;y=b;z=c;
}
float zc()
{
return(x+y+z);
}
float area()
{
float t=(x+y+z)/2;
return(sqrt((t-x)*(t-y)*(t-z)));
}
void draw()
{
cout<<"三角形三边长分别为:"<<x<<","<<y<<","<<z<<endl;
}
};
class CPolygon:virtualpublic CShape//多边形类定义
{
protected:
float x;
int y;
public:
CPolygon(float a,int b)
{
x=a;
y=b;
}
void setpolygon(float a,int b)
{
x=a;
y=b;
}
float area()
{
return(sqrt(3.0)*x*x*y/4);
}
float zc()
{
return(x*y);
}
void draw()
{
cout<<"多边形的一边长为:"<<x<<setw(10)<<"边数为:"<<y<<endl;
}
};
#endif
#include"CShape.h"
#include<iostream>
#include<iomanip>
usingnamespace std;
void main()
{
char d='y';
int i(0);
cout<<"欢迎进入本系统!";
cout<<"请选择服务:"<<endl;
cout<<" 1.点的计算。"<<endl;
cout<<" 2.线的计算。"<<endl;
cout<<" 3.圆的计算。"<<endl;
cout<<" 4.圆柱的计算。"<<endl;
cout<<" 5.圆锥的计算。"<<endl;
cout<<" 6.长方形的计算。"<<endl;
cout<<" 7.多边形的计算。"<<endl;
cout<<" 8.三角形的计算。"<<endl;
cout<<" 9.退出。"<<endl;
while(d=='Y'||d=='y')
{
cout<<"请输入你的选择:"<<endl;
cin>>i;
if(i==1)
{
CShape *pshape;
CPoint point(2.0,3.0);
float a,b;
cout<<"请输入点的坐标:";
cin>>a>>b;
point.setpoint(a,b);
pshape=&point;
pshape->draw();
}
elseif(i==2)
{
CShape *pshape;
CLine line(1.0,2.0,3.0,4.0);
float a,b,c,d;
cout<<"请依次输入两端点的坐标:";
cin>>a>>b>>c>>d;
line.setline(a,b,c,d);
pshape=&line;
pshape->draw();
cout<<"线的长度为:"<<pshape->length()<<endl;
}
elseif(i==3)
{
CShape *pshape;
CCircle circle(2.0);
float a;
cout<<"请输入圆的半径:";
cin>>a;
circle.setcircle(a);
pshape=&circle;
pshape->draw();
cout<<"圆的面积为;"<<pshape->area()<<setw(10)<<"圆的周长为;"<<pshape->zc()<<endl;
}
elseif(i==4)
{
CShape *pshape;
CCylinder yz(2.0,3.0);
float a,b;
cout<<"请输入圆柱的底面半径和高:";
cin>>a>>b;
yz.setcylinder(a,b);
pshape=&yz;
pshape->draw();
cout<<"圆柱的面积为:"<<pshape->area()<<setw(10)<<"圆柱的体积为:"<<pshape->valum()<<endl;
}
elseif(i==5)
{
CShape *pshape;
CCone cone(5.0,4.0);
float a,b;
cout<<"请输入母线长和底面半径:";
cin>>a>>b;
cone.setcone(a,b);
pshape=&cone;
pshape->draw();
cout<<"圆锥的体积为:"<<pshape->valum()<<endl;
}
elseif(i==6)
{
CShape *pshape;
CRectangle rectangle(3.0,2.0);
float a,b;
cout<<"请输入长方形的长和宽:";
cin>>a>>b;
rectangle.setrectangle(a,b);
pshape=&rectangle;
pshape->draw();
cout<<"长方形的面积为:"<<pshape->area()<<setw(10)<<"长方形的周长为:"<<pshape->zc()<<endl;
}
elseif(i==7)
{
CShape *pshape;
CPolygon polygon(2.0,4);
float a;
int b;
cout<<"请输入多边形的边长和边数:";
cin>>a>>b;
polygon.setpolygon(a,b);
pshape=&polygon;
pshape->draw();
cout<<"多边形的面积为:"<<pshape->area()<<setw(10)<<"周长为:"<<pshape->zc()<<endl;
}
elseif(i==8)
{
CShape *pshape;
CTriangle triangle(3.0,4.0,5.0);
float a,b,c;
cout<<"请输入三角形三边长:";
cin>>a>>b>>c;
triangle.settriangle(a,b,c);
pshape=▵
pshape->draw();
cout<<"三角形的周长为:"<<pshape->zc()<<"面积为:"<<pshape->area()<<endl;
}
elseif(i==9)
{
return;
}
else
{
cout<<"输入错误,请重新输入!";
}
cout<<"是否继续其他操作(Y/N)?"<<endl;
cin>>d;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询