GeeksforGeeks » C/C++ Programming Questions

C++: possible encapsulation failure

(2 posts)
  1. Amit
    guest
    Posted 1 year ago #

    Consider this program.

    #include <iostream>
    using namespace std;
    
    class Base {
    public:
    	virtual void foo() { cout << "Base foo()\n"; }
    };
    
    class Derived : public Base {
    private:
    	void foo() { cout << "Derived foo() \n"; }
    };
    
    int main()
    {
     Base *bp;
     Derived d;
     bp = &d;
     bp->foo();
    }
    

    Output: Derived foo()
    So it is possible to access a private data member this way.

  2. kartik
    Moderator
    Posted 1 year ago #

    nice finding :)


Reply

You must log in to post.

RSS feed for this topic