江南学生公社

江南学子的天堂!

91 成员389 主题2,374 回复0 张照片

chicky的oop第八次上机报告

C
chicky
2000-12-21 15:30
#include <iostream.h>
#include <graphics.h>
#include <math.h>
#include <conio.h>
#include <stdlib.h>

class Tshape
{
protected:
double x,y;
public:
Tshape(double a,double b):x(a),y(b){}
virtual double Area()=0;
void get()
{
cout<<Area()<<endl;
}
virtual void draw()=0;
virtual void hide()=0;
};

class Tcircle:public Tshape
{
private:
double radius;
public:
Tcircle(double a,double b,double c):Tshape(a,b),radius(c){}
virtual double Area()
{
return (3.14*radius*radius);
}
virtual void draw()
{
cricle(x,y,radius);
}
virtual void hide()
{
setcolor(getbkcolor());
void draw();
setcolor(getmaxcolor());
}
};

class Trectangle:public Tshape
{
private:
double x2,y2;
public:
Trectangle(double a,double b,double c,double d):Tshape(a,b),x2(c),y2(d){}
virtual double Area()
{
return fabs((x-x2)*(y-y2));
}
virtual void draw()
{
rectangle(x,y,x2,y2);
}
virtual void hide()
{
setcolor(getbkcolor());
void draw();
setcolor(getmaxcolor());
}
};

class Ttriangle:public Tshape
{
private:
double x2,y2,x3,y3;
public:
Ttriangle(double a,double b,double c,double d,double e,double f):Tshape(a,b)
{
x2=c;
y2=d;
x3=e;
y3=f;
}
virtual double Area();
virtual void draw()
{
line(x,y,x2,y2);
line(x,y,x3,y3);
line(x2,y2,x3,y3);
}
virtual void hide()
{
setcolor(getbkcolor());
void draw();
setcolor(getmaxcolor());
}
};

double Ttriangle::Area()
{
int a,b,c,d;
a=pow((x-x2),2)+pow((y-y2),2);
b=pow((x-x3),2)+pow((y-y3),2);
c=pow((x2-x3),2)+pow((y2-y3),2);
d=pow((a+b-c),2)/(4*a*b);
c=sqrt(1-d);
d=sqrt(a*b)*c/2;
return d;
}

void main()
{
Tshape *s;
int gdriver = DETECT, gmode,errorcode;
initgraph(&gdriver, &gmode, "h:\C++\BC5\BGI");
errorcode=graphresult();
if(errorcode!=grOk)
{
cout<<"Graphics error:"<<grapherrormsg(errorcode));
cout<<"Press any key to halt:";
getch();
exit(1);
}
setcolor(getmaxcolor());
Tcircle c(200,200,100);
s=&c;
s->get();
s->draw();
getch();
s->hide();
getch();
Trectangle r(20,40,10,30);
s=&r;
s->get();
s->draw();
getch();
s->hide();
getch();
Ttriangle t(40,40,40,90,90,40);
s=&t
s->get();
s->draw();
getch();
s->hide();
getch();
closegraph();
}
小鸡无毛飞不高
1 / 1