Skip to main content
gamedev.net
🔒 Locked

Inheritance

Started by dave 6 replies 450+ views
Original Post
dave
dave
Ok, 1. I have a base class where when it is inherited from some functions need to be private and some need to be public in the inheriting class. How do i do this? 2. If the base class has interface functions and i inherit from the base class, does the inheriting class have to provide bodies for those functions. ace
Mattman
Mattman
If using Java or C/C++, you can use the "protected" keyword when declaring variables. This makes variables private to external calls while allowing them to be inherited by other classes.
dave
dave
but i want them to become private in the inheriting class,

ace
snk_kid
snk_kid
Quote:
Original post by ace_lovegrove
1. I have a base class where when it is inherited from some functions need to be private and some need to be public in the inheriting class. How do i do this?


inherit privately, this will make all public & protected members private in the derived class, to bring a privately inherited members into public or protected access use the "using" keyword:

struct foo {	void bar() const {}	void foobar() const {}};struct foobar : private foo {  using foo::bar; //now bar member function in public access.};


warning private inheritance does not make a is-a relationship, you can not aim a base pointer to derived instances if you inherit privately, private inheritance is simillar to composition.

Quote:
Original post by ace_lovegrove
2. If the base class has interface functions and i inherit from the base class, does the inheriting class have to provide bodies for those functions.


your only forced to override if its a pure virtual member function (which can have a default implementation) and the derived class needs to be instancable.

i.e.
struct foo {  virtual void bar() = 0;};void foo::bar() { /* ... */ } //optional default implementation.struct foobar : foo { // foobar must override bar to be instancable.   void bar() {}};
dave
dave
thanks, rate++
moagstar
moagstar
When the protected members are inherited they are still private to external classes.

2. I assume you are talking about pure virtual functions, in which case yes you do have to provide bodies for the functions(if you intend to instanciate the derived class) otherwise the compiler will complain about you attempting to instantiate an abstract class. If you want to provide the option for derived classes to 'choose' which members to override you could replace the pure virtual function with an empty bodied virtual function e.g.

class Base{public:   virtual void onKeyboard() {}   virtual void onMouse()    {}   virtual void onJoystick() {}   virtual void onRender() = 0;   virtual void onShutdown() = 0;   virtual void onInitialise() = 0;};class Derived : public Base{public:   void onKeyboard() { /* do something */ }   void onRender()   { /* draw something */ }   void onInitialise() { /* initialise everything */ }   void onShutdown() { /* shut everything down */ }};


This allows you to choose which member functions MUST be specialised in a derived class and those which the derived class can choose not to specialise. In the example above the initialise, render, and shutdown are pure virtuals meaning that they must be implemented if the derived class is to be instantiated, while the onKeyboard and onMouse functions are empty virtual functions, so you could choose to implement them, but you don't have to.
moagstar
moagstar
Quote:
Original post by snk_kid
pure virtual member function (which can have a default implementation)


How do you provide a default implementation for a pure virtual?
snk_kid
snk_kid
Quote:
Original post by moagstar
Quote:
Original post by snk_kid
pure virtual member function (which can have a default implementation)


How do you provide a default implementation for a pure virtual?


i've shown an example above.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.