您的位置:首页 > 编程语言 > C语言/C++

C++ 学习笔记5

2011-07-14 10:41 127 查看
 
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;

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: