1

I am learning C++ and came across this code where the constructor is initialised without declaring member variables. Also, the object is created without any parameters. Wouldn't a default constructor be called instead?

Also, if this class is inherited, will the derived class have access to x and y?

// Example program

#include <iostream>
#include <string>

class game
{
public:
    game(int x = 0, int y = 100); // Do they get defined as members?
    int z; 
};

game::game(int x, int y) : z(x)
{
    std::cout << x;   
}

int main()
{
    game g; // passed w/o parameters.
    return 0; 
}
5
  • They could have access to x and y through a base class constructor called from the derived class. Commented Jun 8, 2017 at 13:14
  • 1
    Since you give default arguments to all of the arguments of the constructor, it becomes the default constructor since it can be called without specifying any arguments. Commented Jun 8, 2017 at 13:14
  • 1
    And as with any function, arguments behaves as local variables, and go out of scope once the function returns. That happens for constructors as well. Neither constructor arguments nor constructor local variables becomes member variables of the class. Commented Jun 8, 2017 at 13:15
  • 2
    x and y are parameters of the constructor only, they are not members of the object. You might want to solidify your understanding using a good C++ book. Commented Jun 8, 2017 at 13:19
  • Oh Ok. Interesting. Thank you so much. I tried to display the value of x by using a display function. It says x was not declared in scope. class game { public: game(int x = 0, int y = 100); int z; void display(); };int main() { game g; g.display(); return 0; } Commented Jun 8, 2017 at 13:22

1 Answer 1

2

Also the object is created without any parameters. Wouldn't a default constructor be called instead?

You declare your constructor as follows:

game(int x = 0, int y = 100);

And the implementation:

game::game(int x, int y) : z(x)
{
    std::cout << x;   
}

Since you have specified default arguments for the constructor, when you call:

game g;

It is the same as calling:

game g {0, 100};

Since the default arguments are provided for the constructor.

game(int x = 0, int y = 100); // Do they get defined as members.

They don't get defined as members, unless you set their values to members in the class. x and y both go out of scope at the end of the constructor.

Also, if this class is inherited will the derived class have access to x and y?

Not directly, which you can see as follows:

class Derived : public game
{
public:
    Derived();
};

Derived::Derived() : game(100, 100) //Or whatever arguments
{
    //...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I got it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.