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. Base class pointer to Derived Class object??

Base class pointer to Derived Class object??

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 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.
  • A Offline
    A Offline
    Ash20
    wrote on last edited by
    #1

    what is advantage of "Base class pointer to Derived Class object" ? class Base { public: Base(){ cout<<"Constructor: Base"<fun1(); I guess it is having same behaviour. Please let me know wht is advantage of using base class pointer to derived class object? Thanks\ Ash.

    C C 2 Replies Last reply
    0
    • A Ash20

      what is advantage of "Base class pointer to Derived Class object" ? class Base { public: Base(){ cout<<"Constructor: Base"<fun1(); I guess it is having same behaviour. Please let me know wht is advantage of using base class pointer to derived class object? Thanks\ Ash.

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

      It is in fact extremly powerfull and widely used. This is what is called 'polymorphism' (if you google for it, you'll probably find tons of information). The principle is that you declare some virtual function in your base class that can be redefined by a derived class. In this way the derived class can have a different behavior as its parent. And, if you have several different derived classes that inherit from the same base class, they all can be treated as a base class but have different behavior (and, so, they can all be stored in an array for example). If you want a typical example: suppose that you write a program to draw shapes (triangles, squares, circles, ...). What you would do in that case is have a base class CShape that has a virtual method Draw. Each derived class (CSquare, CCircle, CTriangle, ...) will specialize this Draw method for itself. All the shapes, as they are inherited by the same base class can be manipulated exactly the same way (they will be stored in an array, and whenever you need to redraw your drawing, you will call Draw from all the shapes in your array, no matter what shape it really is). This is an extremly important concept in OOP.


      Cédric Moonen Software developer
      Charting control [v1.2]

      1 Reply Last reply
      0
      • A Ash20

        what is advantage of "Base class pointer to Derived Class object" ? class Base { public: Base(){ cout<<"Constructor: Base"<fun1(); I guess it is having same behaviour. Please let me know wht is advantage of using base class pointer to derived class object? Thanks\ Ash.

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

        Try using virtual functions. This gives your base class pointer a behaviour depending on the class where it is derived from.

        class CBase
        {
        public:
        virtual void OpenDoor( ) = 0;
        };

        class House: public CBase
        {
        public:
        virtual void OpenDoor( )
        {
        AfxMessageBox(_T("Open door of house") );
        }
        };

        class Car: public CBase
        {
        public:
        virtual void OpenDoor( )
        {
        AfxMessageBox(_T("Open door of Car") );
        }
        };

        CBase* pObject = new House; pObject->OpenDoor( );

        A 1 Reply Last reply
        0
        • C codeII

          Try using virtual functions. This gives your base class pointer a behaviour depending on the class where it is derived from.

          class CBase
          {
          public:
          virtual void OpenDoor( ) = 0;
          };

          class House: public CBase
          {
          public:
          virtual void OpenDoor( )
          {
          AfxMessageBox(_T("Open door of house") );
          }
          };

          class Car: public CBase
          {
          public:
          virtual void OpenDoor( )
          {
          AfxMessageBox(_T("Open door of Car") );
          }
          };

          CBase* pObject = new House; pObject->OpenDoor( );

          A Offline
          A Offline
          Ash20
          wrote on last edited by
          #4

          Thanks Koos, I got wht i ws searching for..

          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