Add some functions to the simple graphics system: perimeter(), area() and volume() to calculate its perimeter, area and volume (only for ThreeDimentionalShape) respectively; a function draw() to draw a shape with different colors, thickness and styles.
建立一個簡單圖形系統,可提供如下功能:
?perimeter():計算圖形的周長;
?area():計算圖形面積;
?volume():計算圖形體積(只針對三維圖形);
?draw():繪製圖形。
注意:
1.多態的使用;
2.繪製圖形時可只輸出特徵資訊,無須繪製具體的圖形。
//本程式在VCSP6下調試通過
#include <iostream>
#include <math.h>
using namespace std;
const double PI=3.1415926;
class Shape //定義Shape抽象類別
{
public:
virtual float perimeter( )=0;
virtual float Volume( )=0; //定義體積函數
virtual float Area( )= 0; //定義面積函數
virtual void Disp( )= 0; //定義輸出函數
virtual void Draw( )=0;
};
class Rectangle: public Shape //定義矩形衍生類別
{
public:
void Setvalue(int m, int n)
{
x = m;
y = n;
}
float Area( ) //虛函數在矩形類中的定義版本
{
sr = x*y;
return 1;
}
float perimeter()
{
pr=2*(x+y);
return 1;
}
float Volume()
{
return 1;
}
void Disp( )
{
cout <<"矩形面積="<< sr << endl;
cout<<"矩形周長="<<pr<<endl;
}
void Draw()
{
cout<<"draw a rectangle."<<endl;
}
private:
double sr;
double x,y;
double pr;
};
class Circle: public Shape //定義圓的衍生類別
{
public:
void Setvalue(int m)
{
x = m;
}
float perimeter()
{
pc=2*PI*x;
return 1;
}
float Volume()
{
return 1;
}
float Area( ) //虛函數在圓類中的定義版本
{
sc = PI*x*x;
return 1;
}
void Disp( )
{
cout <<"圓的面積="<< sc << endl;
cout<<"圓的周長="<<pc<<endl;
}
void Draw()
{
cout<<"draw a circle."<<endl;
}
private:
double sc;
double x;
double pc;
};
class sphere:public Shape //定義球的衍生類別
{
public:
void Setvalue(int m)
{
x=m;
}
float Volume()
{
vs=4/3*PI*x*x*x;
return 1;
}
float perimeter()
{
return 1;
}
float Area()
{
as=4*PI*x*x;
return 1;
}
void Disp()
{
cout<<"球的體積="<<vs<<endl;
cout<<"球的表面積="<<as<<endl;
}
void Draw()
{
cout<<"draw a sphere."<<endl;
}
private:
double vs;
double x;
double as;
};
class cuboid:public Shape //定義長方體的類
{
public:
void Setvalue(int l,int w,int h)
{
x=l;
y=w;
z=h;
}
float Volume()
{
vc=x*y*z;
return 1;
}
float Area()
{
ac=2*(x*y+x*z+y*z);
return 1;
}
void Disp()
{
cout<<"長方體的體積="<<vc<<endl;
cout<<"長方體的表面積="<<ac<<endl;
}
float perimeter()
{
return 1;
}
void Draw()
{
cout<<"draw a cuboid."<<endl;
}
private:
double x,y,z;
double vc,ac;
};
int main( )
{
Rectangle r; //定義矩形類對象
Circle c; //定義圓類對象
sphere s; //定義球對象
cuboid cu; //定義長方體對象
int x, y,z;
int choice; //用來接收所選擇的選項號
do{ //迴圈選擇選項
cout <<"1.求矩形面積和周長"<<endl; //顯示選擇菜單
cout <<"2.求圓的面積和周長"<<endl;;
cout<<"3.求球的體積和表面積"<<endl;;
cout<<"4.求長方體的體積和表面積"<<endl;;
cout <<"0.退出"<<endl;;
cout <<"請選擇功能(0-4):";
cin >> choice; //輸入選項號
if(choice == 1)
{ cout<<"請輸入矩形的長和寬:";
cin>>x>>y;
r.Setvalue(x,y);
r.Area();
r.perimeter();
r.Disp();
r.Draw();
}
if(choice==2)
{ cout<<"請輸入圓的半徑:";
cin>>x;
c.Setvalue(x);
c.Area();
c.perimeter();
c.Disp();
c.Draw();
}
if(choice==3)
{
cout<<"請輸進球的半徑: ";
cin>>x;
s.Setvalue(x);
s.Volume();
s.Area();
s.Disp();
s.Draw();
}
if(choice==4)
{
cout<<"請輸入長方體的長,寬,高(按順序):";
cin>>x>>y>>z;
cu.Setvalue(x,y,z);
cu.Volume();
cu.Area();
cu.Disp();
cu.Draw();
}
cout <<endl;
}while(choice!= 0);
return 0;
}