Calling a Base class constructor from a derived class.

Hi everyone, I'm just a beginner so don't judge me. I have searched on Google before I came here but it didn't help me really.
Here is the problem: I have an abstract class called Flight and two classes which are derived from Flight. Their names are RegularFlight and CharterFlight. How do I call the constructor from Flight class in my derived classes, so my derived classes can inherit the protected members of Flight class?

Code:

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
class Flight
{
protected:
     char* startDestination;
     char* finalDestination;
     char* takeoffDate;
     char* takeoffTime;
     int numberOfReservedSeats;
public:
     Flight();
     ~Flight();
     Flight(char* sDest, char* fDest, char* takeoffD, char* takeoffT, 
            int reservations);
};


#include"Flight.h"
class RegularFlight : public Let
{
private:
     int numberOfSeatsOnThePlane;
public:
     void reserveOneSeat;
     RegularFlight();
     RegularFlight(int numOfSeatsOnThePlane);
     ~RegularFlight();
};


#include"Flight.h"
class CharterFlight : public Let
{
private:
     int numberOfSeatsInRegularPlane;
     int numberOfSeatsInIrregularPlane;
public:
     CharterFlight();
     CharterFlight(int regular, int irregular);
     ~CharterFlight();
};



I didn't write all the functions I use in this program since I guess it's not needed right now. I only have to say that class Flight contains pure virtual functions which are later predefined in derived classes.
The constructor Flight(char* sDest, char& fDest, char*takeoffD, char* takeoffT,
int reservations) is used to initiate protected members of my abstract class Flight and I'd like to call it with the constructors RegularFlight(int numOfSeatsOnThePlane) and CharterFlight(int regular, int irregular).

I'd like to inherit the protected members of my base class to my derived classes since I need to make a program that has as little as possible code duplication. That means that both classes use the same members, and the only members that are different are defined in each subclass as private.

I hope that I didn't confuse you very much because I have a lot of this going in my mind. :D Every help is appreciated.





Last edited on
Initializer list. See example at the bottom of: http://en.cppreference.com/w/cpp/language/initializer_list


PS. Please use code tags, when posting code. See http://www.cplusplus.com/articles/jEywvCM9/
Sorry, I didn't know about the code tags. Anyway, here is something I have written:

1
2
3
4
5
6
7
8
RegularFlight(char* sDest, char* fDest, char* takeoffD, char* takeoffT,
                     int reservations, int numOfSeatsOnThePlane)
   :Flight(sDest, fDest, takeoffD, takeoffT, reservations) {};


CharterFlight(char* sDest, char* fDest, char* takeoffD, char* takeoffT,
                   int reservations, int regular, int irregular)
   :Flight(sDest, fDest, takeoffD, takeoffT, reservations) {};



Is this correct?



Last edited on
That looks correct to me, did you try compiling it?
Is this correct?

Well, your opening post doesn't declare constructors of the same form as your later definitions. But assuming you've added them, the answer is almost!

For the RegularFlight constructor you also need to init the member variable numberOfSeatsOnThePlane...

1
2
3
4
RegularFlight(char* sDest, char* fDest, char* takeoffD, char* takeoffT,
                     int reservations, int numOfSeatsOnThePlane)
   :Flight(sDest, fDest, takeoffD, takeoffT, reservations)
   ,numberOfSeatsOnThePlane(numOfSeatsOnThePlane) {};


And similarly for CharterFlight, but in this case you've got numberOfSeatsInRegularPlane and numberOfSeatsInIrregularPlane to worry about.

Andy
Last edited on
Okay, thanks everyone. I've completed it. :)
Topic archived. No new replies allowed.