I seem to be unable to use a base class as a function parameter, have I messed up my inheritance?
I have the following in my main:
int some_ftn(Foo *f) { /* some code */ }; Bar b; some_ftn(&b);
And the class Bar inheriting from Foo in such a way:
class Bar : Foo { public: Bar(); //snip private: //snip };
Should this not work? I don't seem to be able to make that call in my main function
Constructor cannot be inherited but a derived class can call the constructor of the base class.
Inheritance enables you to create new classes that reuse, extend, and modify the behavior defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class.
Explanation: When the inheritance is private, the private methods in base class are inaccessible in the derived class (in C++). 2.
The private inheritance can introduce unnecessary multiple inheritance. The private inheritance allows members of Car to convert a Car* to an Engine*. The private inheritance allows access to the protected members of the base class. The private inheritance allows Car to override Engine's virtual functions.
By default, inheritance is private. You have to explicitly use public
:
class Bar : public Foo
You have to do this:
class Bar : public Foo { // ... }
The default inheritance type of a class
in C++ is private
, so any public
and protected
members from the base class are limited to private
. struct
inheritance on the other hand is public
by default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With