⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 circle.cpp

📁 面向对象程序设计
💻 CPP
字号:
#include <iostream.h>
#include <graphics.h>
#include <conio.h>
class location{   //定义 类 Location
    protected:
	int x,y;  // x,y 为保护成员
    public:
	location(int initx, int inity)  //基类Location构造函数
	   { x=initx;  y=inity;  }
};
class point:public location{  //定义 类 point,为类 Location 的公有派生类
    public:
	point(int initx, int inity):location(initx, inity) //point 类构造函数
	   { }
	void show()                      //用当前颜色画点
	   { putpixel(x,y,getcolor()); }
	void hide()                      //抹去显示点
	   { putpixel(x,y,getbkcolor()); }//即用背景颜色画点
	void moveto(int newx, int newy)  //移动显示点
	   { hide();           //抹去原有点
	     x=newx;  y=newy;  //设置新座标位置
	     show();           //画新点
	   }
};
class circles:public point{   //定义 类 circle,为类 point 的公有派生类
    private:
	int radius;
    public:
	circles(int initx, int inity, int initradius):point(initx, inity)
	   { radius=initradius; }     //circle 类构造函数
	void show()                   //画一个园
	   { circle(x,y,radius); }
	void hide()                   //抹去已显示园
	   { unsigned int tempcolor;
	     tempcolor=getcolor();    //记下当前颜色
	     setcolor(getbkcolor());  //将背景颜色设置为当前色
	     circle(x,y,radius);      //用此颜色画园,相当于抹去园
	     setcolor(tempcolor);     //将当前颜色恢复到原来状态
	   }
	void expand(int expandby)     //放大显示的园
	   {
	     hide();                  //删去原有园
	     radius+=expandby;        //增大园半径
	     if (radius<0) radius=0;  //避免负值
	     show();                  //画新园
	   }
	 void moveto(int newx, int newy)  //移动显示的园
	   { hide();
	     x=newx;  y=newy;
	     show();
	   }
	 void contract(int contractby)    //缩小显示的园
	   { expand(-contractby); }
};
void main()
{
  int gdriver=DETECT, gmode;
  initgraph(&gdriver,&gmode,"d:\\borlandc\\bgi"); //初始化屏幕成图形模式
  circles mycircle(100,200,50);         //定义一个园对象
  setcolor(10);                         //设置当前色
  mycircle.show();                      //画一个园
  getch();
  for(int i=0; i<=100; i++)             //慢慢移动园
    { for(long j=0; j<=1000000; j++);
    mycircle.moveto(100+i,200);  }
  getch();
  int k=0;
  for(i=0; i<=50; i++)                   //慢慢放大园
    { for(long j=0; j<=1000000; j++);
	mycircle.expand(i-k);
	k=i; }
  getch();
  k=0;
  for(i=0; i<=75; i++)                   //慢慢缩小园
    { for(long j=0; j<=1000000; j++);
	mycircle.contract((i-k));
	k=i; }
  getch();
  closegraph();                          //关闭图形模式
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -