answersLogoWhite

0

What are Public and Private inheritance in c plus plus?

Updated: 8/20/2019
User Avatar

Wiki User

11y ago

Best Answer

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.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are Public and Private inheritance in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Demonstrate single inheritance in C plus plus?

struct base1 { // ... }; struct base2 { // ... }; struct derived1 : public base1 // single inheritance { // ... }; struct derived2 : public base1, public base2 // multiple inheritance { // ... };


What are the difference between public and private inheritance in c plus plus?

Public member, fields, methods etc can be accessed from outside of the class. While private members etc can accessed only within the class even "child" classes do not have access to private members, fields etc.


How do you create a class in C plus plus?

class class_name { private: data_members; public: member_functions; };


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.


What if you declare public members rather than private in c plus plus?

Public members in C++ have accessibility to any function that has scope to the instance of the class, whereas private members have accessibility only to functions of that class.


Does c plus plus supports hierarchical inheritance?

Yes.


Do the memory will be created for both private and public data members if the object is created for a class in c plus plus?

Yes.


How ploymorphism and inheritance is different from that in Java and c plus plus?

C++ allows multiple inheritance while Java does not. In my opinion, multiple inheritance is not useful because it can get very confusing very quick. For polymorphism, C++ does early binding by default, while Java does late binding by default. Late binding is more useful than early binding.


What is the difference between public and private in c plus plus?

A private member can only be accessed by other methods of the same class, while a public member can be accessed by methods of any class or by non class code.


What is single inheritance in c plus plus?

Multiple inheritance occurs when a class is derived directly from two or more base classes. class b1 {}; class b2 {}; class d: public b1, public b2 {}; // multiple inheritance class


Programme to implement single and multilevel inheritance taking employee as sample base class in c plus plus?

struct employee { }; struct supervisor : employee { // single inheritance -- a supervisor inherits all the public and protected properties of an employee. }; struct manager : supervisor { // multilevel inheritance -- a manager inherits all the public and protected properties of a supervisor (and therefore an employee). };


Define protected access modifire in c plus plus?

The access privileges in c++ are 1.public 2.private 3.protected and by default its private