answersLogoWhite

0


Best Answer

Ideally, none. Base classes should have no knowledge whatsoever of their derived classes and therefore should have no access to any members declared in the derived classes. However, derived class methods are accessible via virtual functions declared in the base class. The correct methods (overrides) are called via the virtual table, but the base class itself requires no specific knowledge of the derived class in order to call these methods. This makes sense since a base class cannot know in advance all the classes that may or may not derive from it in the future.

If a method must be guaranteed to be implemented by a derived class, a pure-virtual method can be declared in the base class instead. This renders the base class abstract, meaning you cannot instantiate an object from the base class, only from a derivative that fully implements (or inherits) all the pure-virtual methods.

Conversely, a derived class can access all the public and protected members of its base class.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

8y ago

No,base class object can not call derived class object..because when the ref-id of child class or parent class object is stored in the reference variable of base class..only then we can call the methods of base class.

otherwise no base class object is allowed to access the private methods of child class..

Yes, of course it can. If the base class implicitly calls one of its own virtual methods then the most-derived override of that method is invoked automatically. The base class doesn't even need to know the runtime type of its derived class because the call is invoked via the vtbl (virtual table), which is simply a table of function pointers which exists for every class that declares or inherits a virtual function. The virtual function mechanism exists so that interfaces can be declared for classes that have yet to be defined.

Alternatively, if the base class uses runtime type information (RTTI) to determine its derived class it can invoke ANY method of that derived class. This is not recommended, however, because it adds an expensive runtime overhead that you simply don't get when invoking virtual functions (other than the indirection required to invoke the function through a function pointer). There is a memory overhead in using virtual functions, but the performance and extensibility it offers more than outweighs the additional memory costs. Also, every time you add a new class to the hierarchy you are forced to update the base class to accommodate it (which usually entails updating every member function that uses runtime type information), which not only adds to the maintenance cost, it increases the chances of errors creeping in (such as when you forget to update a base class function) and it forces every derived class to be recompiled along with their consumers, which increases compile times.

Note that base classes can invoke private and protected methods in their derived classes simply by declaring the base class a friend of the derived class. Again, this is not recommended because base classes should (ideally) know nothing about their derivatives, as is the case when derived classes do not yet exist. A derived class implementer who does not have access to the base class source would find it impossible to achieve the desired behaviour.

You can, of course, ignore these recommendations within your own programs, but when distributing your base classes outwith an enclosed hierarchy (where consumers cannot and are not expected to create their own derivatives) you ignore these recommendations at your own peril.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How much a base class members can access of derived class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What do you mean by protected derivation of a sub class from base class?

When you derive a class (the sub-class) from a base class using protected access, all public members of the base class become protected members of the derived class, while protected members of the base class will remain protected. Private members are never inherited so they remain private to the base class. By contrast, if you use public inheritance, the public members of the base class remain public to the derived class, while protected members of the base class remain protected in the derived class. If you use private inheritance, both the public and protected members of the base class become private to the derived class. Note that accessibility cannot be increased, only reduced or left the same. That is, a protected member of a base class cannot be inherited as a public member of a derived class -- it can only be declared private or remain protected. Note also that accessibility is viewed from outside of the derived class. That is, all members of a base class other than the private members are inherited by the derived class and are therefore fully accessible to the derived class. But from outside of the derived class, all base class accessibility is determined by the access specified by the type of inheritance.


Is it true that a derived class inherits all the members of its base class?

False. A derived class inherits the public and protected members of its base class. Private members of the base class cannot be inherited.


How can the base class data be accessed from database directly using derived class?

In order to access a base class member the base class member must be declared protected or public. Private data is only accessible to members of the same class and to friends of the class. I'm not entirely sure what you mean by accessing from a database. A derived class does not require a database in order to access its base class members, it only requires protected access at the very least. Note that although you can declare derived classes to be friends of their base classes and thus allow private access, it would be an unusual design since base classes should never know anything about any of their derivatives.


What is a public class?

A public class is a base class declared with public inheritance: class base { // ... }; class derived : public base { // ... }; In the above example, base is a public class of derived, thus derived is regarded as being a type of base. The derived class inherits all the public and protected methods of its base. Protected methods are accessible to the derived class, its derivatives and their friends. If base were declared protected, its public methods become protected methods of derived. The base class is then an implementation detail of derived; only members of derived, its derivatives and their friends can treat derived as being a type of base. If declared private, the public and protected methods of base become private methods of derived. The base class is then an implementation detail of derived; only members of derived and its friends can treat derived as a type of base.


What is visibility mode what are the different inheritance visibility modes support by c plus plus?

There is no such thing as visibility mode in C++. Visibility is a function of information hiding but that relates to the way in which implementation details can be obfuscated within binary executables and libraries where only the interface need be exposed in a plain-text header file. This has nothing whatsoever to do with object oriented programming since information hiding is also possible in C. You probably meant access specifiers. There are three levels: private, protected and public. Private access limits access to the class and to friends of the class. Protected is the same as private but extends access to derivatives of the class. Public access imposes no limits. In terms of inheritance, the specified access level determines the accessibility of the protected and public members of the base class (private members are never inherited and will always remain private to the base class). in essence, members with access greater than the specified inheritance are reduced to the specified access. Thus if you specify protected inheritance, all public members of the base class become protected members of the derivative, while private inheritance reduces all public and protected members to private access. You may also reduce access to specific base class members simply be redeclaring them with the appropriate access.

Related questions

What do you mean by protected derivation of a sub class from base class?

When you derive a class (the sub-class) from a base class using protected access, all public members of the base class become protected members of the derived class, while protected members of the base class will remain protected. Private members are never inherited so they remain private to the base class. By contrast, if you use public inheritance, the public members of the base class remain public to the derived class, while protected members of the base class remain protected in the derived class. If you use private inheritance, both the public and protected members of the base class become private to the derived class. Note that accessibility cannot be increased, only reduced or left the same. That is, a protected member of a base class cannot be inherited as a public member of a derived class -- it can only be declared private or remain protected. Note also that accessibility is viewed from outside of the derived class. That is, all members of a base class other than the private members are inherited by the derived class and are therefore fully accessible to the derived class. But from outside of the derived class, all base class accessibility is determined by the access specified by the type of inheritance.


Difference bitween various visibility modes used c plus plus?

By visibility I assume you mean member accessibility. C++ uses three levels of accessibility: private, protected and public. Private members are only accessible to the class itself and friends of the class. Protected members are the same as private members except derived classes also have access. Public members are fully accessible. With regards inheritance, base class members with greater access than that specified are reduced to the specified access in the derived class. Thus public inheritance has no effect on base class member access. Protected inheritance reduces public members of the base class to protected members of the derived class. Private inheritance reduces both public and protected members of the base class to private members of the derived class. Private members of the base class are never inherited, thus they always remain private to the base class. Note that access to base class members can never be increased through inheritance, only reduced or kept the same. However, as well as defining an overall inheritance access, you can also specify member-wise inheritance access. Thus you could use public inheritance overall, but specify certain public members of the base class to be protected or private in the derived class and/or certain protected members of the base class to be private members of the derived class.


Is it true that a derived class inherits all the members of its base class?

False. A derived class inherits the public and protected members of its base class. Private members of the base class cannot be inherited.


How can the base class data be accessed from database directly using derived class?

In order to access a base class member the base class member must be declared protected or public. Private data is only accessible to members of the same class and to friends of the class. I'm not entirely sure what you mean by accessing from a database. A derived class does not require a database in order to access its base class members, it only requires protected access at the very least. Note that although you can declare derived classes to be friends of their base classes and thus allow private access, it would be an unusual design since base classes should never know anything about any of their derivatives.


What is public derivation in object-oriented programming?

Public derivation or public inheritance means that all the public members of the base calls are declared public in the derived class while the protected members remain protected. Protected inheritance means all the public members of the base class are declared protected in the derived class, as are the protected members. Private inheritance means all the public and protected members of the base class are declared private in the derived class. Private members of the base class are never inherited and are therefore unaffected by inheritance. Note that regardless of the type of inheritance specified, individual non-private members of the base class can be inherited with public or protected access as required of the derived class. The type of inheritance can be therefore be thought of as being the default inheritance for all base class members which can (optionally) be overridden for specific members where required.


What is a public class?

A public class is a base class declared with public inheritance: class base { // ... }; class derived : public base { // ... }; In the above example, base is a public class of derived, thus derived is regarded as being a type of base. The derived class inherits all the public and protected methods of its base. Protected methods are accessible to the derived class, its derivatives and their friends. If base were declared protected, its public methods become protected methods of derived. The base class is then an implementation detail of derived; only members of derived, its derivatives and their friends can treat derived as being a type of base. If declared private, the public and protected methods of base become private methods of derived. The base class is then an implementation detail of derived; only members of derived and its friends can treat derived as a type of base.


What is visibility mode what are the different inheritance visibility modes support by c plus plus?

There is no such thing as visibility mode in C++. Visibility is a function of information hiding but that relates to the way in which implementation details can be obfuscated within binary executables and libraries where only the interface need be exposed in a plain-text header file. This has nothing whatsoever to do with object oriented programming since information hiding is also possible in C. You probably meant access specifiers. There are three levels: private, protected and public. Private access limits access to the class and to friends of the class. Protected is the same as private but extends access to derivatives of the class. Public access imposes no limits. In terms of inheritance, the specified access level determines the accessibility of the protected and public members of the base class (private members are never inherited and will always remain private to the base class). in essence, members with access greater than the specified inheritance are reduced to the specified access. Thus if you specify protected inheritance, all public members of the base class become protected members of the derivative, while private inheritance reduces all public and protected members to private access. You may also reduce access to specific base class members simply be redeclaring them with the appropriate access.


What are Public and Private inheritance in c plus plus?

Public, protected and private inheritance determine how the public and protected base class members are inherited by the derived class. Private members are never inherited and are therefore unaffected by the type of inheritance (they remain private to the base class). The following table summarises how inheritance affects accessibility of base class members with respect to the derived class: public inheritanceprotected inheritanceprivate inheritancepublic member of base classpublic member of derived classprotected member of derived classprivate member of derived classprotected member of base classprotected member of derived classprotected member of derived classprivate member of derived classprivate member of base classprivate member of base classprivate member of base classprivate member of base class Note that accessibility to individual public and protected base class members can be overridden within the derived class, regardless of the type of inheritance specified.


Which base class member functions are not inherited by a derived class?

Derived classes only inherit the protected and public members of their base classes. Private member functions cannot be inherited by a derived class.


Does Derive class inherits?

The derived class inherits all members and member functions of a base class.


Can you access in drive class from base class private data?

Base class should no knowledge about derived classes. The "private" modifier on a data member means private to the class which defined it. Base class cannot directly reference/access the private data member of the derived class, and the derived classes cannot access the private data member defined in the base class. Either way the accessing the private data member should be done via properties or getters


What do you mean by inheriting the interface?

In object oriented programming, a derived class inherits the protected and public members of its base class. Those members therefore define the interface that is inherited by the derived class. The derived class may augment that interface to provide a more specialised implementation of the interface, without the need to re-write the generic interface of the base class. The implication is that the derived class is a more specialised form of the base class.