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. Inheritance!!!

Inheritance!!!

Scheduled Pinned Locked Moved C / C++ / MFC
oopquestion
3 Posts 3 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.
  • N Offline
    N Offline
    Nilesh Hamane
    wrote on last edited by
    #1

    Please see the code bellow.

    #include <iostream>
    #include <stdlib.h>

    using namespace std;

    class Cpolygon
    {
    protected:
    int width, height;
    public:
    void setvalues (int a, int b)
    {
    width=a;
    height=b;
    }
    };

    class Crectangle:public Cpolygon
    {
    public:
    int area ()
    {
    return(width*height);
    }
    };

    class Ctriagle:public Cpolygon
    {
    public:
    int area()
    {
    return((width*height)/2);
    }
    };

    int main(int argc, char *argv[])
    {

    Crectangle rect;
    Ctriagle tril;

    Cpolygon *Poly1 =▭
    Cpolygon *Poly2 =&tril;

    Poly1->setvalues(4,5);
    Poly2->setvalues(4,5);

    cout<<Poly1->area()<<endl;
    cout<<Poly2->area()<<endl;

    system("PAUSE");
    return 0;
    }

    I have two questions. 1.

    Cpolygon *Poly1 =▭
    Cpolygon *Poly2 =&tril;

    What does these two lines indicate? 2. Instead of creating two objects of base class can we use only one object of base class to set the values. i.e.

    Cpolygon *Poly;

    So that

    Poly->setvalues(4,5);

    need to call only once and create the objects of derived class and show the outputs i.e.

    Crectangle *rect;
    Ctriagle *tril;

    cout<<rect->area()<<endl;
    cout<<tril->area()<<endl;

    will it work? :confused: Thanks for reading.

    C C 2 Replies Last reply
    0
    • N Nilesh Hamane

      Please see the code bellow.

      #include <iostream>
      #include <stdlib.h>

      using namespace std;

      class Cpolygon
      {
      protected:
      int width, height;
      public:
      void setvalues (int a, int b)
      {
      width=a;
      height=b;
      }
      };

      class Crectangle:public Cpolygon
      {
      public:
      int area ()
      {
      return(width*height);
      }
      };

      class Ctriagle:public Cpolygon
      {
      public:
      int area()
      {
      return((width*height)/2);
      }
      };

      int main(int argc, char *argv[])
      {

      Crectangle rect;
      Ctriagle tril;

      Cpolygon *Poly1 =▭
      Cpolygon *Poly2 =&tril;

      Poly1->setvalues(4,5);
      Poly2->setvalues(4,5);

      cout<<Poly1->area()<<endl;
      cout<<Poly2->area()<<endl;

      system("PAUSE");
      return 0;
      }

      I have two questions. 1.

      Cpolygon *Poly1 =▭
      Cpolygon *Poly2 =&tril;

      What does these two lines indicate? 2. Instead of creating two objects of base class can we use only one object of base class to set the values. i.e.

      Cpolygon *Poly;

      So that

      Poly->setvalues(4,5);

      need to call only once and create the objects of derived class and show the outputs i.e.

      Crectangle *rect;
      Ctriagle *tril;

      cout<<rect->area()<<endl;
      cout<<tril->area()<<endl;

      will it work? :confused: Thanks for reading.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Before answering your question, I think there should be a small modification in your code of the Cpolygon class: you should have a virtual area function:

      class Cpolygon
      {
      ...
      virtual int area() = 0;
      };

      Otherwise those two lines won't compile:

      cout<<Poly1->area()<<endl;
      cout<<Poly2->area()<<endl;

      First question: you have two pointers to a base class and you make them point to derived classes. This would be more or less similar as this:

      Cpolygon *Poly1 = new Crectangle();
      Cpolygon *Poly2 =new Ctriangle();

      Which is seen more often. In fact the whole purpose of this code is to show you the use of polymorphism (I suggest you google for it to have a lot more examples). The base principle is that you can manipulate objects of different types (Crectangle and Ctriangle) exactly the same way: they all implement a known interface (Cpolygon). This way, you can store them all in a container and you don't need to know which exact type you are manipulating. Each time you call a virtual function (like area()), it will be "redirected" to the correct type. This is one of the most basic and fundamental principle of object oriented programming.

      Cédric Moonen Software developer
      Charting control [v2.0] OpenGL game tutorial in C++

      1 Reply Last reply
      0
      • N Nilesh Hamane

        Please see the code bellow.

        #include <iostream>
        #include <stdlib.h>

        using namespace std;

        class Cpolygon
        {
        protected:
        int width, height;
        public:
        void setvalues (int a, int b)
        {
        width=a;
        height=b;
        }
        };

        class Crectangle:public Cpolygon
        {
        public:
        int area ()
        {
        return(width*height);
        }
        };

        class Ctriagle:public Cpolygon
        {
        public:
        int area()
        {
        return((width*height)/2);
        }
        };

        int main(int argc, char *argv[])
        {

        Crectangle rect;
        Ctriagle tril;

        Cpolygon *Poly1 =▭
        Cpolygon *Poly2 =&tril;

        Poly1->setvalues(4,5);
        Poly2->setvalues(4,5);

        cout<<Poly1->area()<<endl;
        cout<<Poly2->area()<<endl;

        system("PAUSE");
        return 0;
        }

        I have two questions. 1.

        Cpolygon *Poly1 =▭
        Cpolygon *Poly2 =&tril;

        What does these two lines indicate? 2. Instead of creating two objects of base class can we use only one object of base class to set the values. i.e.

        Cpolygon *Poly;

        So that

        Poly->setvalues(4,5);

        need to call only once and create the objects of derived class and show the outputs i.e.

        Crectangle *rect;
        Ctriagle *tril;

        cout<<rect->area()<<endl;
        cout<<tril->area()<<endl;

        will it work? :confused: Thanks for reading.

        C Offline
        C Offline
        cheetach
        wrote on last edited by
        #3

        A new comers' reply: For question 1: Try the step below: Add "virtual int area() =0;"(of course it is a public member) in the definition of class Cpolygon. For the lesson, search virtual class on the Internet. For question 2: I admire your imagination. It is possible, but not in your way. See the use of "constructor". Especially "default constructor."

        With ears and eyes.

        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