全名k歌怎么一天升级12:c++编程题
来源:百度文库 编辑:高考问答 时间:2024/05/01 23:54:26
#include <iostream.h>
#include <stdlib.h>
const double PI=3.14159;
class shape
{
protected:
int x,y;
public:
virtual void show()=0;
virtual double GetArea()=0;
};
class Rectangle:public shape
{
int length,width,time;
public:
void set(int n,int a,int b,int c,int d)
{
x=a;y=b;length=c;width=d;
time=n;
}
double GetArea()
{
return (length*width);
}
void show()
{
cout<<"Rectangle "<<time<<": Area="<<GetArea()<<" center point=("<<x;
cout<<","<<y<<")"<<endl;
}
};
class Circle:public shape
{
int radius,time;
public:
void set(int n,int a,int b,int c)
{
x=a;y=b;radius=c;
time=n;
}
double GetArea()
{
return PI*(radius*radius);
}
void show()
{
cout<<"Circle "<<time<<": Area="<<GetArea()<<" center point=("<<x;
cout<<","<<y<<")"<<endl;
}
};
class Cube:public shape
{
int length,time;
public:
void set(int n,int a,int b,int c)
{
x=a;y=b;length=c;
time=n;
}
double GetArea()
{
return (length*length*6);
}
void show()
{
cout<<"Cube "<<time<<": Area="<<GetArea()<<" center point=("<<x;
cout<<","<<y<<")"<<endl;
}
};
int main()
{
int ch;
int a,b,c,d,i;
int n=1,i1=0,i2=0,i3=0;
shape *obj[100];
Rectangle s1[100];
Circle s2[100];
Cube s3[100];
while(cin>>ch) //读取第一个字符
{
if(ch==1) //Rectangle
{
cin>>a>>b>>c>>d;
s1[i1].set(n,a,b,c,d);
obj[n]=&s1[i1];
i1++;
}
else if(ch== 2) //Circle
{
cin>>a>>b>>c;
s2[i2].set(n,a,b,c);
obj[n]=&s2[i2];
i2++;
}
else if(ch== 3) //Cube
{
cin>>a>>b>>c;
s3[i2].set(n,a,b,c);
obj[n]=&s3[i2];
i3++;
}
n++;
}//while
for(i=1;i<n;i++) //输出各项参数
obj[i]->show();
return 0;
}