D
D
Daniil Demidko2016-02-26 11:18:12
C++ / C#
Daniil Demidko, 2016-02-26 11:18:12

Implicit inheritance private?!?

I'll just leave the code:

# include < iostream >

class Base {
private:
            int Var = 475 ;
public:
            virtual int Get() const final {
                 return Var;
            }
};

class Two : public Base {} ;

int main () {

     Two two;
     std :: cout << two . Get () ; /// 475, но как  ?
     std :: cin . get () ;

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2016-02-26
@Daniro_San

What's wrong with that? You have made a virtual method with an implementation. Then inherited the class without overriding the method. The method is public. The fact that he refers to private fields inside - does not matter. Private fields are contained in the successor, they are simply not available to him.

D
devalone, 2017-03-29
@devalone

Yes, that's right, that's the beauty of inheritance

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question