method binding in C++ -


class shape {     public:         virtual void draw() = 0;         virtual void area() { . . .}         . . . };  class circle : public shape {     public:         void draw() { . . . }         . . . };  class rectangle : public shape {     public:         void draw() { . . . }         . . . };  class square : public rectangle {     public:         void draw() { . . . }         . . . };  rectangle* r = new rectangle; r->draw(); // (1)  r = new square; r->draw(); // (2)  shape* sh = new circle; sh->area(); // (3)  square* sq = new square; sq->draw(); // (4) 

(1),(2) dynamic binding, there's no doubt think

(3) since class derived shape don't override method area, it's resolved shape::area() compiler?

(4) no class derived sqaure class, sq can reference square type, means static method binding occurs?

is there wrong?? in advance.

binding wise seems true . above code have memory leaks

rectangle* r = new rectangle; r->draw(); // (1)  r = new square; r->draw(); // (2) 

here in allocating memory r pointer twice , can free memory once, hance memory leak..


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -