grin3 2015-11-14 06:27 采纳率: 0%
浏览 1738

新手求助 定义一个name 如何输入图形类的名字输出相应的类的内容

设计三个图形类:Circle(圆)、Rectangle(矩形)、Triangle(三角形);
1、Cirlce类基本信息:圆心坐标、半径;
Rectangle类基本信息:长、宽;
Triangle类基本信息:三个顶点坐标;
其中:成员变量为private属性,成员函数为public属性;
2、每个图形类有计算图形的面积GetArea(),显示图形的基本信息函数Show();

现在我的代码不能实现 输入名字circle 就只需要输入circle类成员 ,然后只显示circle类的信息;
还有 三角形类里 判断能不能构成三角形的条件对吗。。。

#include
#include
using namespace std;
const double PI=3.141592653;

class Circle{
public:
Circle(double x,double y,double r);
double GetArea();
void Show();
double Set();
private:
double x,y;
double radius;
};
Circle::Circle(double x,double y,double r){
x=x;
y=y;
radius=r;
}
double Circle::GetArea(){
return PI*radius*radius;
}
double Circle::Set(){
cin>>x;
cin>>y;
cin>>radius;
}

class Circle{
public:
Circle(double x,double y,double r);
double GetArea();
void Show();
double Set();
private:
double x,y;
double radius;
};
Circle::Circle(double x,double y,double r){
x=x;
y=y;
radius=r;
}
double Circle::GetArea(){
void Circle::Show(){
if(radius>0)
cout<<"Area "<<GetArea()<<endl;
else
cout<<"输入错误!"<<endl;
}

class Rectangle{
public:
Rectangle(double a,double b);
double GetArea();
void Show();
double Set();
private:
double l,w;
};
Rectangle::Rectangle(double a,double b){
l=a;
w=b;

}
double Rectangle::GetArea(){
return l*w;
}
double Rectangle::Set(){
cin>>l;
cin>>w;
}
void Rectangle::Show(){
if(l>0&&w>0)
cout<<"RectangleArea=:"<<GetArea()<<endl;
else
cout<<"输入错误!"<<endl;
}

class Triangle{
public:
Triangle(double x1,double x2,double x3,double y1,double y2,double y3){
x1=x1;
x2=x2;
x3=x3;
y1=y1;
y2=y2;
y3=y3;}
double GetArea();
void Show();
double Set();
private:
double x1,x2,x3,y1,y2,y3;
};
double Triangle::GetArea(){
return x1*y2+x2*y3+x3*y1-x1*y3-x2*y1-x3*y2;
}
double Triangle::Set(){
cin>>x1>>y1>>x2>>y2>>x3>>y3;
}

void Triangle::Show(){
double a,b,c;
a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
b=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
c=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
if(((a+b)>c)&&((a-b)b)&&((a-c)a)&&((b-c)<a))
cout<<"TriangleArea="<<GetArea()<<endl;
else
cout<<"Wrong Input!"<<endl;
}

int main() {
char name;
cout<<"Input name"< cin>>name;
rewind(stdin);
Circle a(0,0,0);
a.Set();
a.Show();
cin>>name;
rewind(stdin);
Rectangle b(0,0);
b.Set();
b.Show();
cin>>name;
rewind(stdin);
Triangle c(0,0,0,0,0,0);
c.Set();
c.Show();
}

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-11-14 13:21
    关注

    没看到你输入的逻辑,当然不行,cin >> a.name 就可以。当然name因为是char只能有一个字符
    三角形类里 判断能不能构成三角形的条件,可以任何一边必须小于另两边之和,才能构成三角形。

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件