default constructor for child classes?

Hi,

is it possible to make default constructor for derived classes? Or can default constructor only be the parent class itself?

TY
Any class can have any constructor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//parent class
{
mName = "DefaultName";
mPosition = Vector3(0.0f, 0.0f, 0.0f);
mVelocity = Vector3(0.0f, 0.0f, 0.0f);
mFuelLevel = 100;
mDamage = 0;
}
Spaceship::Spaceship(const string& name,
const Vector3& pos,
const Vector3& vel,
int fuel,
int damage)
{
mName = name;
mPosition = pos;
mVelocity = vel;
mFuelLevel = fuel;
mDamage = damage;
}


//child class
class FighterShip : public Spaceship
{
public:
FighterShip(
const string& name,
const Vector3& pos,
const Vector3& vel,
int fuel,
int damage,
int numMissiles);
private:
int mNumMissiles;
};
FighterShip::FighterShip(const string& name,
const Vector3& pos,
const Vector3& vel,
int fuel,
int damage,
int numMissiles)
: Spaceship(name, pos, vel, fuel, damage)
{
mNumMissiles = numMissiles;
}


is it possible to to make FighterShip fighter()? (because we didnt even define a default constructor for FighterShip, which has one more method added to its parameter)
is it possible to to make FighterShip fighter()?


Only if FighterShip has a default constructor.. Which yours doesn't. So with that code, no, you can't default construct a FighterShip.

You could make a default constructor for it, though:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class FighterShip : public Spaceship
{
public:
    // make this constructor the default constructor
    FighterShip(
        const string& name = "Nameless FighterShip",
        const Vector3& pos = Vector3(0,0,0),
        const Vector3& vel = Vector3(0,0,0),
        int fuel = 100,
        int damage = 100,
        int numMissiles = 0);

private:
    int mNumMissiles;
};


Now that constructor will act as the default constructor, which means you can create Fighterships like so:

 
FighterShip defaultfighter;



PS: Learn to indent. Indentation and whitespace are your friend. Makes code much easier to read and follow.
Last edited on
sry i meant FighterShip fighter...(without (), my bad)

i how would i make a default AND an user defined constructor? (as same as it is in parent class - you have 2 options: default or optional constructor)

i want to be able to do all of these things:

Spaceship defaultspaceship;
Spaceship optionalspaceship(......);
FighterShip defaultfighter;
FighterShip optionalfighter(........);
i how would i make a default AND an user defined constructor? (as same as it is in parent class - you have 2 options: default or optional constructor)

No, you have way more than those two options.
You can have any number of constructors in a class, since you can overload constructors like any other function.
There are 3 main TYPES of constructors. Default, Copy and "normal".
A default constructor is merely a constructor with:
A) No input parameters
B) Default values for the parameters
A copy constructor is a constructor in which you input an object of the same class you wish to instantiate an object off, copying the values the inputted object had.
A normal constructor is just all that remains, a constructor that can have default values, for which you must supply input apart from another object of the same class.
There is no such thing as a user-defined constructor, since ALL are user-defined when written in your code.
Ok, i figured out everything now, thanks to you!
Topic archived. No new replies allowed.