#include <iostream>
using namespace std;
class CPolygon {
protected:
int width, height;
public:
virtual int area ()
{ return (0); }
};
class CRectangle: public CPolygon {
public:
int area () { return (width * height); }
};
Has compilation warning
Class '[C@1a9e0f7' has virtual method 'area' but non-virtual destructor
How to understand this warning and how to improve the code?
[EDIT] is this version correct now? (Trying to give answer to elucidate myself with the concept)
#include <iostream>
using namespace std;
class CPolygon {
protected:
int width, height;
public:
virtual ~CPolygon(){};
virtual int area ()
{ return (0); }
};
class CRectangle: public CPolygon {
public:
int area () { return (width * height); }
~CRectangle(){}
};
class CRectangle: public CPolygon { public: virtual int area () { return (width * height); } };
? – Woolpackvirtual ~CRectangle() {}
as well. As I said, restating that these functions are virtual is simply good form, it's not required by the language in any way. – Nawrockivirtual ~CPolygon(){};
Meanwhile @Nawrocki does not have the semicolon in the above example? – Enunciation;
is totally superfluous. All by itself, it's just an empty statement. Sometimes you want an empty statement as the body of awhile
orfor
loop where everything is done with side-effects. I've never seen one used in the middle of a declaration, and I'm certain that it's inclusion was accident or confusion. – Nawrocki;
does no harm, but also does no good. A chaotic neutral semicolon? Maybe it's because he formerly hadvirtual ~CPolygon()=0;
— which seems to be another common form of the virtual destructor (other than the Stay Puft marshmallow man). I'm not clear whether the =0 version is really the same. It's the "pure" kind right? – Enunciation