首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

跪求大神指导函数指针有关问题

2013-01-05 
跪求大神指导函数指针问题#include iostream.hclass Base{private:int xint ypublic:Base()Base(int

跪求大神指导函数指针问题
跪求大神指导函数指针有关问题#include <iostream.h>
class Base
{
private:
int x;
int y;
public:
Base();
Base(int x,int y);
virtual void draw(){};
void move();
void (Base::*Pd[6])();
};



class Poit :virtual public Base
{
public:
Poit();
Poit(int x,int y);
void draw();

};



class Line :virtual public Base
{
private:
int a;
int b;
public:
Line();
Line(int a,int b,int x,int y);
void draw();

};


class Circle :virtual public Base
{
private:
int r;
public:
Circle();
Circle(int x,int y,int r);
void draw();

};

class Graphics :public Poit,public Line, public Circle
{
private:

int i;
public:
Graphics():i(0){}
void task(Poit *p);
void task(Line  *l);
void task(Circle *c);
void draw();
void fun(Base *b)
{
Pd[i++]=b->draw;
}
};
Base::Base()
{
x=0;
y=0;
}

Base::Base(int x,int y)
{
this->x=x;
this->y=y;
}
void Base::move()
{
cout<<"将画笔移动到("<<x<<","<<y<<"),";
}



Poit::Poit():Base(0,0)
{
}

Poit::Poit(int x,int y):Base(x,y)
{
}

void Poit::draw()
{
Base::move();
cout<<"画一个点"<<endl;
}

Line::Line():Base(0,0)
{
a=0;
b=0;
}

Line::Line(int a,int b,int x,int y):Base(x,y)
{
this->a=a;
this->b=b;
}

void Line::draw()
{
Base::move();
cout<<"画一条到("<<a<<","<<b<<")的直线"<<endl;
}

Circle::Circle():Base(0,0)
{
r=0;
}

Circle::Circle(int x,int y,int r):Base(x,y)
{
this->r=r;
}

void Circle::draw()
{
Base::move();
cout<<"画一个半径"<<r<<"的圆"<<endl;
}

void Graphics::draw()
{
int n;
for(n=0;n<6;n++)
{
cout<<"第"<<n+1<<"步:";
*(Pd[n])();
}
}

void Graphics::task(Poit *p)
{
fun(p);
}
void Graphics::task(Line  *l)
{
fun(l);
}
void Graphics::task(Circle *c)
{
fun(c);
}
int main()
{
Graphics g;
g.task(new Poit(3,5));
g.task(new Line(1,2,3,5));
g.task(new Circle(3,4,5));
g.task(new Line(8,6,21,15));
g.task(new Poit(6,9));
cout<<"绘图步骤:"<<endl;
g.draw();

return 0;
}


[解决办法]

#include <iostream>
 #include <string>
 using namespace std;
 
 class myerror
 {
     std::string str;
 public:
     myerror(const char* str):str(str){}
     const char* what()const
     {
         return str.c_str();


     }
 };
 class Base //
 {
 private:
     int x;
     int y;
 protected:
     enum {MAX_OBJ_NUM=6}; //能存入的最大对象数
 public:
     Base():x(0),y(0) {}
     Base(int x,int y):x(x),y(y) {}
     virtual void draw()=0; //
     void move();
     Base* Pd[MAX_OBJ_NUM];
 };
 class Poit :virtual public Base //点
 {
 public:
     Poit():Base() {}
     Poit(int x,int y):Base(x,y) {}
     void draw();
 };
 
 class Line :virtual public Base //线
 {
 private:
     int a;
     int b;
 public:
     Line():Base(),a(0),b(0) {}
     Line(int x,int y,int a,int b):Base(x,y),a(a),b(b) {}
     void draw();
 };
 
 class Circle :virtual public Base //圆
 {
 private:
     int r;
 public:
     Circle():Base(),r(0) {}
     Circle(int x,int y,int r):Base(x,y),r(r) {}
     void draw();
 };
 class Graphics :public Poit,public Line, public Circle //图集
 {
 private:
 
     int i;
 public:
     Graphics():Poit(),Line(),Circle(),i(0) {}
     void task(Base *p);
     void draw();
     void fun(Base *b)
     {
         if(i>=(int)MAX_OBJ_NUM)
         {
             // cout<<"已满,"<<endl;  return;
             throw myerror("-----FULL-----");
         }
         Pd[i++]=b;
     }
 };
 void Base::move()
 {
     cout<<"将画笔移动到("<<x<<","<<y<<"),";
 }
 void Poit::draw()
 {
     Base::move();
     cout<<"画一个点"<<endl;
 }
 void Line::draw()
 {
     Base::move();
     cout<<"画一条到("<<a<<","<<b<<")的直线"<<endl;
 }
 void Circle::draw()
 {
     Base::move();
     cout<<"画一个半径为"<<r<<"的圆"<<endl;
 }
 void Graphics::draw()
 {
     for(int n=0; n<i; ++n)
     {
         cout<<"第"<<n+1<<"步:";
         Pd[n]->draw();


     }
 }
 void Graphics::task(Base *p)
 {
     fun(p);
 }
 int main()
 {
     Graphics g;
     cout<<"绘图步骤:"<<endl;
     try
     {
         g.task(new Poit(3,5));
         g.task(new Line(1,2,3,5));
         g.task(new Circle(3,4,5));
         g.task(new Line(8,6,21,15));
         g.task(new Poit(6,9));
         g.draw();
     }
     catch(myerror err)
     {
         g.draw();
         cout<<err.what();
     }
     return 0;
 }
 

热点排行