虚函数代码例子纠错
这段代码用来理解虚函数的用法:
你可以调试一下,有一点小问题,特求解!
原文可到
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;
}