Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. polymorphism question - child pointer to parent class

polymorphism question - child pointer to parent class

Scheduled Pinned Locked Moved C / C++ / MFC
oopquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zecodela
    wrote on last edited by
    #1

    hi all, anyone have a idea the meaning of the bold lines. using child pointer to a the parent class. class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } //virtual int area (void) =0; virtual int area (void) {return 0;} void printarea (void) { cout << this->area() << endl; } }; class CTriangle: public CPolygon { public: int area (void) { return (width * height / 2); } int test; }; int main () { CRectangle rect; CTriangle trgl; CPolygon * ppoly1 = ▭ CPolygon * ppoly2 = &trgl; CTriangle * ptrgl = (CTriangle *) ▭ ppoly1->set_values (4,5); ppoly2->set_values (4,5); ptrgl->set_values (4,5); ppoly1->printarea(); ppoly2->printarea(); ptrgl->printarea(); ptrgl->test = 1; cout << ppoly1->area() << endl; cout << ppoly2->area() << endl; cout << ptrgl->area() << endl; system("PAUSE"); return EXIT_SUCCESS; }

    S 1 Reply Last reply
    0
    • Z zecodela

      hi all, anyone have a idea the meaning of the bold lines. using child pointer to a the parent class. class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } //virtual int area (void) =0; virtual int area (void) {return 0;} void printarea (void) { cout << this->area() << endl; } }; class CTriangle: public CPolygon { public: int area (void) { return (width * height / 2); } int test; }; int main () { CRectangle rect; CTriangle trgl; CPolygon * ppoly1 = ▭ CPolygon * ppoly2 = &trgl; CTriangle * ptrgl = (CTriangle *) ▭ ppoly1->set_values (4,5); ppoly2->set_values (4,5); ptrgl->set_values (4,5); ppoly1->printarea(); ppoly2->printarea(); ptrgl->printarea(); ptrgl->test = 1; cout << ppoly1->area() << endl; cout << ppoly2->area() << endl; cout << ptrgl->area() << endl; system("PAUSE"); return EXIT_SUCCESS; }

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      The definition of CRectangle isn't present so I can't tell if the cast makes sense. I have to say it look like nonsense to me however. Also, as a general rule avoid C-style casts.

      Steve

      Z 1 Reply Last reply
      0
      • S Stephen Hewitt

        The definition of CRectangle isn't present so I can't tell if the cast makes sense. I have to say it look like nonsense to me however. Also, as a general rule avoid C-style casts.

        Steve

        Z Offline
        Z Offline
        zecodela
        wrote on last edited by
        #3

        Let me restate my question below code can be compile and run with no problem. but, i confused how the bold line works. I have not initialzed a CTriangle object in the bold line but i can access the data by a CTriangle pointer. I dont think it is a good practice but i am quite confused how it works...

        class CPolygon {
        protected:
        int width, height;
        public:
        void set_values (int a, int b)
        { width=a; height=b; }
        //virtual int area (void) =0;
        virtual int area (void)
        {return 0;}
        void printarea (void)
        { cout << this->area() << endl; }
        };
        class CTriangle: public CPolygon {
        public:
        int area (void)
        { return (width * height / 2); }
        int testdata;
        };

        int main () {
        CPolygon poly;
        CTriangle trgl;
        CPolygon * ppoly2 = &trgl;
        CTriangle * ptrgl = (CTriangle *) &poly;
        ppoly2->set_values (4,5);
        ptrgl->set_values (4,5);
        ppoly2->printarea();
        ptrgl->printarea();
        ptrgl->testdata = 100;
        cout << ppoly1->area() << endl;
        cout << ppoly2->area() << endl;
        cout << ptrgl->area() << endl;
        cout << ptrgl->testdata << endl;
        system("PAUSE");
        return EXIT_SUCCESS;
        }

        S 1 Reply Last reply
        0
        • Z zecodela

          Let me restate my question below code can be compile and run with no problem. but, i confused how the bold line works. I have not initialzed a CTriangle object in the bold line but i can access the data by a CTriangle pointer. I dont think it is a good practice but i am quite confused how it works...

          class CPolygon {
          protected:
          int width, height;
          public:
          void set_values (int a, int b)
          { width=a; height=b; }
          //virtual int area (void) =0;
          virtual int area (void)
          {return 0;}
          void printarea (void)
          { cout << this->area() << endl; }
          };
          class CTriangle: public CPolygon {
          public:
          int area (void)
          { return (width * height / 2); }
          int testdata;
          };

          int main () {
          CPolygon poly;
          CTriangle trgl;
          CPolygon * ppoly2 = &trgl;
          CTriangle * ptrgl = (CTriangle *) &poly;
          ppoly2->set_values (4,5);
          ptrgl->set_values (4,5);
          ppoly2->printarea();
          ptrgl->printarea();
          ptrgl->testdata = 100;
          cout << ppoly1->area() << endl;
          cout << ppoly2->area() << endl;
          cout << ptrgl->area() << endl;
          cout << ptrgl->testdata << endl;
          system("PAUSE");
          return EXIT_SUCCESS;
          }

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          The fact that it works is a fluke. The local poly is NOT a CTriangle. It just so happens that in the two classes the vtable[^] pointer is at the same location as are the first two members (width and height). Accessing testdata (via ptrgl) is wrong in this code. Also the wrong instance of virtual functions will be called. If I could give a single piece of advice to new C++ programmers it would be to stop casting yourself into strife.

          Steve

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups