1

Below is my base class:

Vehicle
{
protected: //members being inherited
public:
    void getDetails();
};

This is my derived class:

class Car : protected Vehicle
{}

now I am trying to access the public function of base class through my derived class object. according to my understanding the public functions of base class are inherited by the derived class, so it should work.

Car obj2;
obj2.getDetails(); /*Public func of base class*/

But I am getting error like: 'Vehicle::getDetails' not accessible because 'Car' uses 'protected' to inherit from 'Vehicle'

Can someone explain this? what i am missing?

Magzash
  • 11
  • 2
  • What you're missing is the "normal" *public* inheritance. – Some programmer dude Sep 15 '21 at 07:29
  • 1
    Why are you using `protected` inheritance if you want the methods to stay `public`? Maybe [Difference between private, public, and protected inheritance](https://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance) could be worth the look. – Quimby Sep 15 '21 at 07:30
  • @Quimby I am just learning OOPs concepts, shifting from C. So can you elaborate why that would be bad practice if you can? – Magzash Sep 15 '21 at 07:33
  • @Magzash No worries, it's not bad practice, it's just incorrect usage. `protected` inheritance makes all base class's public methods `protected`, hence the name. Since you want `getDetails` to remain public, you want `public` inheritance. See the answer I linked, it has nice overview of how the keywords work. `protected` means it can only be called from the inside the class and from inside the derived classes. `private` only allows the former. – Quimby Sep 15 '21 at 08:14

2 Answers2

3

You're using the "protected" access specifier when Car inherits from Vehicle. This means that protected members are inherited as they are, but public members are inherited as protected too, because that's the highest accessibility level you're allowing. So the getDetails function is protected in Car, meaning you can't access it outside the class. Using public instead should solve your problem:

class Car : public Vehicle
YallowRvn
  • 31
  • 4
  • Thanks a lot.. This was so so useful to wrap my head around the concept.. This line -"So the getDetails function is protected in Car, meaning you can't access it outside the class." was so useful. What I did was accessed the getDetails() function in another function of Car class, and it worked like a charm. void Car::getCarD(){this->getDetails();} – Magzash Sep 15 '21 at 07:43
  • @Magzash Just a note about your last sentence. What you observed was that the access rules work based on types, not objects. So any `Car` object can access private fields of any other `Car` object. – Quimby Sep 15 '21 at 08:17
1

You might better understand this in therms of pseudo OOP in C.

class Car : protected Vehicle
{};

Is similar to

class Car
{
 protected:
  Vehicle vehicle;  // hidden or unnamed member.
};

The call

obj2.getDetails();

Can be imaging like a call to the hidden member

obj2.vehicle.getDetails();

obj2.vehicle is not accessible because it is protected and the public getDetails is not accessible in the protected member.

You can replace protected: with public: or private:, and see how the access modifiers work.

273K
  • 29,503
  • 10
  • 41
  • 64