L53A08 例2:非虚成员函数 class graph i int X, y, public Graph (int xx, int yy)I X= xX;y -Grapho( j void Draw cdc &dc) dc Ext Out(x, y"This is a Graph Object "); int getXO const freturn x int getO const return y; y
21:53:08 例2:非虚成员函数 class CGraph{ int x,y; public: CGraph(int xx,int yy){ x = xx; y = yy;} ~CGraph(){ } void Draw(CDC &dc) { dc.TextOut(x,y,"This is a Graph Object"); } int getX()const{return x;} int getY()const{return y;} };
L53A08 例2:非虚成员函数(续) class Rectangle: public Graph i int lef t, top, right, bot tom; public CRectangle(int lf t, int tp, int rgt, int btm) Graph((rgt+lf t)/2,(tp+btm) /2) lef t =lf t, top = tp; right rgt; bottom btm REctangle() void Draw cdc dc) I dc Rectangle(lef t, top, right, bottom);
21:53:08 例2:非虚成员函数(续) class CRectangle:public CGraph{ int lef t,top,right,bottom; public: CRectangle(int lf t,int tp,int rgt,int btm) :CGraph((rgt+lf t)/2,(tp+btm)/2) { lef t = lf t; top = tp; right = rgt; bottom = btm; } ~CRectangle(){ } void Draw(CDC & dc) { dc.Rectangle(lef t,top,right,bottom); } };
例2:非虛成员函数(续) #include Graph h class Circle: public Graph int r 指向 Graph类的对象, public 调用 GRaph类的Draw0 Circle(int xx, int yyint rr): Graph(xx, yy)(r=rr; 1 Circle( void Draw(cdc dc (dc.EllipsegetX O/getY O-rgetXO+getO+r); 指向¢ ircle类的对象, Graph g(100, 30),*pG=&g, 调用 Graph类的Draw0 Circle ciy(100, 100, 50) CRectangle rct(100, 200, 300, 300); t f] REctangle pG>Draw(dc);/调用哪一个 Drawl?类的对象,调用 pg=&cir Graph类的 Drawl pG->Draw(adc);/调用哪一个Draw0? pG=&rct pG->Draw(dc);//调用哪一个Draw0?
21:53:08 例2:非虚成员函数(续) #include "Graph.h" class CCircle : public CGraph{ int r ; public: CCircle(int xx,int yy,int rr):CGraph(xx,yy){r=rr ;} ~CCircle(){ } void Draw(CDC & dc) {dc.Ellipse(getX()-r,getY()-r,getX()+r,getY()+r);} }; CGraph g(100,30),*pG=&g; CCircle cir(100,100,50); CRectangle rct(100,200,300,300); pG->Draw(dc); //调用哪一个Draw()? pG=&cir ; pG->Draw(dc); //调用哪一个Draw()? pG=&rct; pG->Draw(dc); //调用哪一个Draw()? 指向CGraph类的对象, 调用CGraph类的Draw() 指向CCircle类的对象, 调用CGraph类的Draw() 指 向 CRectangle 类的对象,调用 CGraph类的Draw()
L53A08 例2:虚成员函数 class graph i int X, y; public Graph (int xx, int yy)( x=xx; y yy Grapho[ y vir tual void Draw cdc &dc) ldc. ExtOut (x, y"This is a Graph 0b ject"); int getX o const return x int getY O const return y; 米
21:53:08 例2:虚成员函数 class CGraph{ int x,y; public: CGraph(int xx,int yy){ x = xx; y = yy;} ~CGraph(){ } vir tual void Draw(CDC &dc) {dc.TextOut(x,y,"This is a Graph Object");} int getX()const{return x;} int getY()const{return y;} };