简体   繁体   中英

C++ Declaring an inherited constructor?

I'm having difficulties in defining a constructor for a class that inherits the properties of another class

class Transportation {

public:
    int ID;
    string company;
    string vehicleOperator;

    Transportation(int,string,string) {
    }
};

class SeaTransport: public Transportation {

public:
    int portNumber;

    SeaTransport(int)::Transportation(int,string,string) {
    }
};

I'm having issues with line 18 ( SeaTransport(int)::Transportation(int,string,string) ).

The error I receive occurs at the pont where I declare Transportation .

As seen in the code, a class Transportation is the body class and class SeaTransport inherits the properies of Transportation .

Transportation::Transportation(int, std::string, std::string)
+2 overloads

type name is not allowed

This error occurs at the int

typedef std::__cxx11::basic_string std::string
type name is not allowed

and this final error occurs at both string variables.

It seems you mix together scoping and a constructor initializer list.

The double-colon operator :: is for scope, while a constructor followed by a single colon and a list of initializations is an initializer list.

You must declare the SeaTransport constructor to take all the arguments, including those for the parent class (assuming you want to pass them on to the base constructor):

SeaTransport(int port, int id, string company, string operator);

Then in the definition (implementation) of the constructor you "call" the parent constructor in the constructor initializer list:

SeaTransport(int port, int id, string company, string oper)
   : Transport(id, company, oper), // "Call" the parent class constructor
     portNumber(port)  // Initialize the own members
{
}

As Mr Some Programmer Dude said, you've a Scope problem in your code, I will try to answer for your second question which is, how to add featured variables on your constructor.

Same as what you did for the port attribute.

You can define before all your Attribute which is boatNumber as int boadNumber = 0 then, you'll overload your constructor with boatNumber(num) after the initializer operator and int num before the initializer operator.

class Transportation {

public:
    int ID;
    string company;
    string vehicleOperator;

    Transportation(int,string,string) {
    }
    ~Transportation(){}
};

class SeaTransport: public Transportation {

public:
    int portNumber;
    int boatNumber;

   SeaTransport(int num, int port, int id, string company, string oper)
   :Transportation(id, company, oper), boatNumber(num),portNumber(port)  {}

   ~SeaTransport(){}
};

But, if you want to get things more specific, you can create another class which is derived from SeaTransport And then you'll define the number of your boat and more other details, if you want.

I'll draw you an instance of it:

 class Boat: public SeaTransport {

    public:

        int boatNumber;

        Boat(int bNum,int num, int port, int id, string company, string oper):
SeaTransport( num, port, id, company, oper),boatNumber(bNum){}

        ~Boat(){}
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM