RedFox
RedFox

Reputation: 85

Inheritance problems with constructrors

I have three classes. The base class is called Jet:

class Jet
{
public :
    Jet(int fuel);
    void fly();

protected:
    int mfuel;
};

Jet::Jet(int fuel): mfuel(fuel) {}

The second class is called F16:

class F16 : public Jet
{
public:
    F16(int fuel, int numMissiles);
    void fireMissile();

protected:
    int mNumMissles;
};

F16::F16(int fuel, int numMissiles): Jet(fuel), mNumMissles(numMissiles) {}

A third class B18 inherits everything normally from F16:

class B18 : public F16
{
public:
    B18(int fuel, int numMissiles, int bullets);
    void fireBullets();

private:
    int mBullets;

};

Now my question is is there a way in the member member initializer here :

B18::B18(int fuel, int numMissiles, int bullets) :
        F16(fuel, numMissiles), mBullets(bullets) {} 

...so i can just erase int fuel from B18, and inherit only the numMissiles from F16? For example:

B18::B18(int numMissiles, int bullets) :
        F16(numMissiles), mBullets(bullets) {}

Something like the above (which of course doesn't work, and probably has logical errors).

Upvotes: 1

Views: 87

Answers (3)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361682

Your comment gave more information about the problem. So I'm quoting here for the sake for visibility:

The thing that i was experimenting if there is a possibility to initialize just a part of F16 to the b18 like just the numMussiles part and not the fuel also from the constructor...in short words to choose what i want to inherit in the member initializator

It looks like overloaded constructors can solve your problem. Add another constructor to your class F16, so you can choose appropriate constructor in the derived class based on what you want to initialize.

Here is what you can do:

class F16 : public Jet
{
public:
  F16(int numMissiles); //<------ add this
  F16(int fuel,int numMissiles);
  void fireMissile();

  protected:
  int mNumMissles;
};

then you can choose constructor based on what you want to initialize:

//unchanged : from your code!
B18::B18(int numMissiles,int bullets): F16(numMissiles),mBullets(bullets) {}

It chooses the first constructor from F16. Note that it is better to initialize the class fully, which means initialize every member to some [default] values (even if you don't need them for particular case).

Upvotes: 1

user2001505
user2001505

Reputation: 1

Not quite sure what you mean exactly, since you can inherit from a nonexistent value. But maybe what you're trying to do involves default parameters? So for example, you'd have:

class Jet
{
public :
   Jet(int fuel);
   void fly();

protected:
   int mfuel;
};

Jet::Jet(int fuel): mfuel(fuel) {}

class F16 : public Jet
{
public:
   F16(int numMissiles = 10, int fuel = 20);
   void fireMissile();

protected:
   int mNumMissles;
};

F16::F16(int numMissiles, int fuel): Jet(fuel), mNumMissles(numMissiles) {}

class B18: public F16
{
public :
   B18(int bullets, int numMissiles, int fuel);
   B18(int bullets);
   void firebullets();

private:
   int mBullets;
};

B18::B18(int bullets, int numMissiles, int fuel) : F16(numMissiles, fuel), mBullets(bullets) {}
B18::B18(int bullets): F16(), mBullets(bullets) {}

In this way, creating a B18 object with a single int parameter will call the F16 constructor using its default parameters.

Upvotes: 0

Panzercrisis
Panzercrisis

Reputation: 4750

I'm doubtful that inheritance in that case can be completely blocked, but you did mention the member initializer line of code. In that you should be able to just do something like this:

B18::B18(int numMissiles,int bullets): F16(30,numMissiles),mBullets(bullets)   {}

You may already know that though. Another option would be something like this:

public:
    F16(int numMissiles, int fuel = 45);

and:

B18::B18(int numMissiles,int bullets): F16(numMissiles),mBullets(bullets)   {}

I'm not sure if that's exactly what you're needing, but maybe it'll help. Again I may be wrong about this, but I'm kind of doubtful that you can just keep a base class's variables from being set up through inheritance. The closest thing would be to leave pointer variables uninitialized.

Upvotes: 2

Related Questions