incorrect information on the polymorphism page
Jul 22, 2019 at 11:51am Jul 22, 2019 at 11:51am UTC
Hello!
I propose to correct the error.
on this page:
http://www.cplusplus.com/doc/tutorial/polymorphism/
snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
// dynamic allocation and polymorphism
#include <iostream>
using namespace std;
class Polygon {
protected :
int width, height;
public :
Polygon (int a, int b) : width(a), height(b) {}
virtual int area (void ) =0;
void printarea()
{ cout << this ->area() << '\n' ; }
};
class Rectangle: public Polygon {
public :
Rectangle(int a,int b) : Polygon(a,b) {}
int area()
{ return width*height; }
};
class Triangle: public Polygon {
public :
Triangle(int a,int b) : Polygon(a,b) {}
int area()
{ return width*height/2; }
};
int main () {
Polygon * ppoly1 = new Rectangle (4,5);
Polygon * ppoly2 = new Triangle (4,5);
ppoly1->printarea();
ppoly2->printarea();
delete ppoly1;
delete ppoly2;
return 0;
}
8.3.5 Delete
if the static type of the object to be deleted is different from its dynamic
type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type
shall have a virtual destructor or the behavior is undefined.
Jul 22, 2019 at 12:06pm Jul 22, 2019 at 12:06pm UTC
Topic archived. No new replies allowed.