虚函数代码例子纠错

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;
}
...全文
53 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
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;
}

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧