虚函数代码例子纠错

forestassure 2003-08-21 11:58:41
这段代码用来理解虚函数的用法:
你可以调试一下,有一点小问题,特求解!
原文可到
http://www.assuredigit.com/
论坛--〉数字水印/图像处理/模式识别/密码学/计算机视觉


#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

class shape
{
public:
shape();
virtual ~shape();
virtual void draw();

};

// 别的类用这个派生

class circle : public shape
{
public:
circle(); ~circle();
void draw(){cout<<"circle"<<endl;}
};
class rectangle : public shape
{
public:
rectangle(); ~rectangle();
void draw(){cout<<"rectangle"<<endl;};

};
// 再给一个shapewrap封装一下
class shapewarp
{
protected:
shape *object;
public:
shapewrap(char* type)
{
if (type=="circle")
object=new circle;
else if (type=="rectangle")
object=new rectangle;
else
{
}
}
~shapewarp()
{delete object;}
void draw() { object->draw(); }
};


int main(int argc, char* argv[])
{
char* str="circle" ;
//const mystr
shapewarp * p = new shapewarp(str);
p->draw ();
return 0;
}
...全文
19 4 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
forestassure 2003-08-21
多谢指教!所言甚是!
修改后:
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

class shape
{
public:
shape(){};
virtual ~shape(){};
virtual void draw() = 0;

};

// 别的类用这个派生

class circle : public shape
{
public:
circle(){};
~circle(){};
void draw(){cout<<"circle"<<endl;}
};
class rectangle : public shape
{
public:
rectangle(){};
~rectangle(){};
void draw(){cout<<"rectangle"<<endl;};

};
// 再给一个shapewrap封装一下
class shapewarp
{
protected:
shape *object;
public:
shapewarp(const string& type)
{
if (type=="circle")
object=new circle;
else if (type=="rectangle")
object=new rectangle;
else
{
object = NULL ;
}
}
~shapewarp()
{
if(object)
delete object;
}
void draw()
{
if(object)
object->draw();
}
};



int main(int argc, char* argv[])
{
const string str("circle1") ;
//const mystr
shapewarp * p = new shapewarp(str);
if(p)
{
p->draw ();
delete p;
}
return 0;
}


  • 打赏
  • 举报
回复
DisplayWorld 2003-08-21
int main(int argc, char* argv[])
{
char * str="circle";

shapewarp * p = NULL;
p = new shapewarp(str);

if(p)
{
p->draw();
delete p;
}

return 0;
}
  • 打赏
  • 举报
回复
DisplayWorld 2003-08-21
int main(int argc, char* argv[])
{
char * str="circle";

shapewarp * p = NULL;
p = new shapewarp(str);

if(p)
{
p->draw();
delete p;
}

return 0;
}
  • 打赏
  • 举报
回复
DisplayWorld 2003-08-21
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

class shape
{
public:
shape(){}
virtual ~shape(){}

public:
virtual void draw() = 0;
};

class circle : public shape
{
public:
circle(){}
virtual ~circle(){}

public:
void draw() { cout << "circle" << endl; }
};

class rectangle : public shape
{
public:
rectangle(){}
virtual ~rectangle(){}

public:
void draw() { cout << "rectangle" << endl; }

};

class shapewarp
{
protected:
shape *object;

public:

//
// 你注意了,构造函数名都能写错真行 shapewrap?是什么东西?
// 还有你的代码风格欠佳,助长出错的根本这是
//
shapewarp(char* type)
:object(NULL)
{
if (stricmp("circle", type) == 0)
{
object = new circle;
}
else if (stricmp("rectangle", type) == 0)
{
object = new rectangle;
}
else
{
// ...
}
}
virtual ~shapewarp() { if(object) delete object; }

public:
void draw() { if(object) object->draw(); }
};


int main(int argc, char* argv[])
{
char * str="circle";

shapewarp * p = NULL;
p = new shapewarp(str);

if(p)
{
p->draw();
}

return 0;
}
  • 打赏
  • 举报
回复
相关推荐
发帖
数据结构与算法
加入

3.2w+

社区成员

数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
申请成为版主
帖子事件
创建了帖子
2003-08-21 11:58
社区公告
暂无公告