Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ inheritance - inaccessible base?

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

like image 273
bandai Avatar asked Jan 31 '11 02:01

bandai


People also ask

Which of the following Cannot be inherited from the base class?

Constructor cannot be inherited but a derived class can call the constructor of the base class.

What can be inherited from 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.

When the inheritance is private the private methods in base class are?

Explanation: When the inheritance is private, the private methods in base class are inaccessible in the derived class (in C++). 2.

Is it beneficial to inherit a base class in private mode?

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.


2 Answers

By default, inheritance is private. You have to explicitly use public:

class Bar : public Foo

like image 25
Jim Buck Avatar answered Nov 22 '22 06:11

Jim Buck


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.

like image 151
Andrew Noyes Avatar answered Nov 22 '22 06:11

Andrew Noyes