Fundamental question in C++
-
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?
-
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?
-
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!"
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++?
-
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++?
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" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/xml>