萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> [c++]派生類的應用

[c++]派生類的應用

   在已有的Point類的基礎上,定義一個“Circle”派生類,要求:新增一個半徑成員;能計

  算並輸出圓的周長及加圓面積

  #ifndef __Dervide_circle__circle__

  #define __Dervide_circle__circle__

  #include

  using namespace std;

  #define PI 3.1415926

  class point

  { private:

  float x,y;

  public:

  point(void)

  {x=0;y=0;}

  point(float a,float b)

  {x=a ; y=b; }

  point(const point &ob)

  {x=ob.x;y=ob.y;}

  void set_point(float a,float b);

  float read_x(void);

  float read_y(void);

  float read_x(void) const;

  float read_y(void) const;

  void move(float a,float b);

  void move(float a,float b) const;

  };

  class circle :public point

  {

  public:

  circle(float a ,float b ,float c):point(a,b)

  {r = c;}

  void set_r(float a){r = a;}

  float Get_R(){return r;}

  void circumference();

  void squire();

  private:

  float r;

  };

  void point::set_point(float a,float b)

  {x=a ; y=b; }

  float point::read_x(void)

  { return x;}

  float point::read_y(void)

  { return y;}

  void point::move (float a,float b)

  { x+=a;y+=b;}

  float point::read_x(void) const

  { return x;}

  float point::read_y(void) const

  { return y;}

  //void point::move (float a,float b) const

  // { x+=a;y+=b;}

  void circle::circumference()

  {

  float c = 2*PI*r;

  cout"周長=""" endif="" float="" pre="" s="PI*r*r;" void="">

  class="brush:java;">#include"circle.h"

  int main()

  {

  circle C(1.2,2.0,3.0);

  cout"" return="">

        :更多精彩教程請關注三聯網頁設計教程 欄目,

copyright © 萬盛學電腦網 all rights reserved