Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of "Public" in a derived class declaration?

People also ask

Can a derived class access public members?

As a quick refresher, public members can be accessed by anybody. Private members can only be accessed by member functions of the same class or friends. This means derived classes can not access private members of the base class directly! This is pretty straightforward, and you should be quite used to it by now.

What is public derived class?

public inheritance makes public members of the base class public in the derived class, and the protected members of the base class remain protected in the derived class. protected inheritance makes the public and protected members of the base class protected in the derived class.

What is the use of public in C++?

When preceding a list of class members, the public keyword specifies that those members are accessible from any function. This applies to all members declared up to the next access specifier or the end of the class.

What happens when a public data member is accessed by a derived class in public private protected mode?

Public mode: If we derive a sub class from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in derived class.


The default is private inheritance. take this example:

class B { };
class D: B { };

uses private inheritance as its the default. This means that D gets all the protected and public fields and methods that B has (if we actually declared any), but can't be cast to a B. Therefore, this code fails:

void foo(B* argument) {}
foo(new D);                   //not allowed

If D publicly inherited from B, then a D could be cast to a B, and this function call would be fine.

The second difference is that all the protected and public members in B become private members in D.

What does this actually mean? Public inheritance means D IS_A B, but private inheritance means "is implemented in terms of". Inheriting D from B means you want to take advantage of some of the features in B, but not because D IS_A B or because there's any conceptual connection between B and D. :D


Without that 'public' 'Employee' would become a private base class of 'Manager'.

Classes declared with keyword 'class' have their members private by default, and have their base classes private by default.

Classes declared with keyword 'struct' have their members public by default, and have their base classes public by default.