zeeks
zeeks

Reputation: 794

call class constructor as a method

I have 2 classes, TournamentMember and Player where Player is derived from TournamentMember. Each of the classes has parametric constructor.

TournamentMember has this constructor:

TournamentMember(const char* name, std::string country, int height); 

Player class has this constructor:

Player(int number, int goals, std::string position, std::string feet);

I create an object from the Player class like this:

Player soccer_player(40, 34, "goalkeeper", "right");

Each soccer player should have 7 properties which are name, country, height, number, goals, position, feet. 4 of those properties are set from Player soccer_player(40, 34, "goalkeeper", "right"); and the left 3 should be assigned from the class TournamentMember. How do I do that? I know a way is from methods (set methods) but I would like to do it with constructors, possibly sth like this: (I know the example below is wrong)

soccer_player.TournamentMember("John", "USA", 170);

Thank you

Upvotes: 1

Views: 411

Answers (3)

bitcode
bitcode

Reputation: 343

You said Player is derived from TournamentMember. So the answer is very simple:

In your Player's constructor you do this:

Player::Player(int a, int b, const char* c, const char* d) : TournamentMember(c, b, a) // this is calling the TournamentMember constructor!!! Just like you wanted =D
{
    //Do the rest of the construction here
}

EDIT:

This is the answer for your updated question (please bear in mind that in this example I am copying the address of the const char* and not the value. In a real program this is wrong, but for the sake of simplicity I disregarded such issues):

This is how your TournamentMember constructor should look like:

TournamentMember::TournamentMember(const char* _name, const char* _country, int _height)
{
    name = _name;
    country = _country;
    height = _height;
}

This is what your Player constructor should look like:

Player::Player(int _number,
               int _goals,
               int _height,
               const char* _name,
               const char* _country,
               const char* _position,
               int _feet) : TournamentMember(_name, _country, _height)
{
    number = _number;
    goals = _goals;
    position = _position;
    feet = _feet;
}

Upvotes: 3

user4581301
user4581301

Reputation: 33932

You need to use one of the most poorly taught, in my opinion, things in all C++. The member initialization list!

In order to satisfy TournamentMember, Player's constructor needs to pick up a bit more syntax, the initializer list, and the variables that need to be passed into TournamentMember.

Player(int number, 
       int goals, 
       std::string position, 
       std::string feet,
       const char* name, 
       std::string country, 
       int height) : TournamentMember(name, country, height)
{
    // constructor body
    // any member variables set here can be set along with TournamentMember
    // in the initializer list. Often this means the constructor's body is 
    // completely empty.
}

The code after the colon is the member initialization list. This allows you to call a member variable or base class constructor before executing the body of the class's constructor.

If you want to initialize a base class, do it here.

Member variables must be constructed before entering the body of the constructor so that they are ready for use. Typically the default constructor is used. If you have a member variable without a default constructor, use the initializer list.

If you have a complex member variable you need to initialize and don't want to have to default construct, create a temporary, and then copy the temporary into the member variable, for example pre-loading a vector or an object with computationally intensive construction logic you'd rather not repeat, here's the place to do it.

In addition there are often optimizations the compiler can apply when using the initializer list

Further reading: http://en.cppreference.com/w/cpp/language/initializer_list

Upvotes: 2

user007
user007

Reputation: 2172

Since your Player class is derived from TournamentMember class, and neither of them has a no-parameter constructor, you need to use an initialization list in the Player constructor to explicitly call the TournamentPlayer constructor, like this:

class Player {
    Player(int a, int b) : TournamentPlayer(a, b) {
      //Parameters are just an example, you can pass parameters just as you want!
    }
}

More about initialization list :: C++ Member Initialization List

EDIT::

Considering the edited question, you need to realize that the constructor of any class is only called once and by itself (when the object is created), and cannot be called explicitly. And in case you want to set all the 7 properties through the constructor, you will have to change the definition of Player constructor, and then use it to initialize the variables of TournamentPlayer class. Something like this::

class Player {
    Player(int number, int goals, std::string position, std::string feet, const char* name, std::string country, int height) : TournamentPlayer(name, country, height) {
      //Parameters are just an example, you can pass parameters just as you want!
    }
}

When you create an object of the derived class, before entering the body of the derived class constructor, the no-parameter constructor of the base class is called (if you do not specify it explicitly in the initialization list), which means, the variable of the base class are initialized before the variable of derived class. So, if you want to set the variables with a constructor, this is the way to do it!

Upvotes: 2

Related Questions