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. Fundamental question in C++

Fundamental question in C++

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
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.
  • Y Offline
    Y Offline
    Yajnesh Narayan Behera
    wrote on last edited by
    #1

    Now let us have two classes one is a base class & there is a virtual method. Again there is a derived class & we override that method & the access is private, through the base class pointer we assign the derived class object & call the virtual method then which method will be called & why?

    R 1 Reply Last reply
    0
    • Y Yajnesh Narayan Behera

      Now let us have two classes one is a base class & there is a virtual method. Again there is a derived class & we override that method & the access is private, through the base class pointer we assign the derived class object & call the virtual method then which method will be called & why?

      R Offline
      R Offline
      rp_suman
      wrote on last edited by
      #2

      The derived one will be called. This will be decided at run-time, as the pointer is pointing to a derived class, the derived class version should be called.

      -- "Programming is an art that fights back!"

      Y 1 Reply Last reply
      0
      • R rp_suman

        The derived one will be called. This will be decided at run-time, as the pointer is pointing to a derived class, the derived class version should be called.

        -- "Programming is an art that fights back!"

        Y Offline
        Y Offline
        Yajnesh Narayan Behera
        wrote on last edited by
        #3

        You are correct. Let me give the snapshot. class BaseClass { public: virtual void Display(); }; void BaseClass:isplay() { printf("Hello BaseClass\n"); } class DerivedClass : public BaseClass { private: void Display(); }; void DerivedClass:isplay() { printf("Hello DerivedClass\n"); } int main(int argc, char* argv[]) { printf("Hello World!\n"); BaseClass* pBaseClass = NULL; DerivedClass* pDerivedClass = new DerivedClass(); pBaseClass = pDerivedClass; pBaseClass->Display(); delete pDerivedClass; return 0; } In this case the method is private in the derived class, so if we can call this private method from another class then what is the meaning of encapsulation in C++?

        T 1 Reply Last reply
        0
        • Y Yajnesh Narayan Behera

          You are correct. Let me give the snapshot. class BaseClass { public: virtual void Display(); }; void BaseClass:isplay() { printf("Hello BaseClass\n"); } class DerivedClass : public BaseClass { private: void Display(); }; void DerivedClass:isplay() { printf("Hello DerivedClass\n"); } int main(int argc, char* argv[]) { printf("Hello World!\n"); BaseClass* pBaseClass = NULL; DerivedClass* pDerivedClass = new DerivedClass(); pBaseClass = pDerivedClass; pBaseClass->Display(); delete pDerivedClass; return 0; } In this case the method is private in the derived class, so if we can call this private method from another class then what is the meaning of encapsulation in C++?

          T Offline
          T Offline
          ThatsAlok
          wrote on last edited by
          #4

          Yajnesh Narayan Behera wrote:

          In this case the method is private in the derived class, so if we can call this private method from another class then what is the meaning of encapsulation in C++?

          buddy that calling the method which is declared public in base class, though it pointing to the address of private method of derived class. static Binding and encapsulation are eligible till compile time, where compiler check daat integrity and language rule.. runtime check is not applicable in c++. thats why you could even call private member at runtime with twist!

          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
          Never mind - my own stupidity is the source of every "problem" - Mixture

          cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/xml>

          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