Here is some code written in C++. Can you find the error, and how would you fix it without changing the interface?
class Widget
{
public:
Widget() { Init(); }
virtual ~Widget() { Destroy(); }
virtual void Init();
virtual void Destroy();
long GetNum() { return *pNumber; }
void SetNum(long num) { *pNumber = num; }
private:
long *pNumber;
};
class Bolt : public Widget;
{
public:
Bolt() { Init(); }
virtual ~Bolt() { Destroy(); }
virtual void Init();
virtual void Destroy();
long GetNum2() { return *pNumber2; }
void SetNum2(long num) { *pNumber2 = num; }
private:
long *pNumber2;
};
void Widget::Init()
{
pNumber = new long;
}
void Widget::Destroy()
{
delete pNumber;
}
Bolt::Init()
{
pNumber = new long;
pNumber2 = new long;
}
Bolt::Destroy()
{
delete pNumber2;
}
class Widget
{
public:
Widget() { Init(); }
virtual ~Widget() { Destroy(); }
virtual void Init();
virtual void Destroy();
long GetNum() { return *pNumber; }
void SetNum(long num) { *pNumber = num; }
private:
long *pNumber;
};
class Bolt : public Widget;
{
public:
Bolt() { Init(); }
virtual ~Bolt() { Destroy(); }
virtual void Init();
virtual void Destroy();
long GetNum2() { return *pNumber2; }
void SetNum2(long num) { *pNumber2 = num; }
private:
long *pNumber2;
};
void Widget::Init()
{
pNumber = new long;
}
void Widget::Destroy()
{
delete pNumber;
}
Bolt::Init()
{
pNumber = new long;
pNumber2 = new long;
}
Bolt::Destroy()
{
delete pNumber2;
}