1

I dont exactly get the use of a constructor For example:

class car
{
    public:
    string name;
    long int price;
    int mileage
    bool ownedByPlayer;
};

Here, why will I use a constructor like this,

class car
{
    public:
    string name;
    long int price;
    int mileage
    bool ownedByPlayer;
    car()
   { 
       ownedByPlayer = false;
   }
};

if I can simply define the variable ownedByPlayer where I have declared it. Like: bool ownedByPlayer = false; ?

  • 1
    It doesn't make very much difference for the no-args constructor, but you can write constructors that take arguments. For instance you might write a constructor `car(string name, long price, int mileage, bool owned)` that sets the values of the member variables to the values passed to the constructor. – Nathan Pierson Mar 22 '21 at 16:02
  • Indeed, you don't have to. Even more, static initialization wad recommended over explicit one ever since it's been introduced. – bipll Mar 22 '21 at 16:03
  • 4
    whats the use of a constructor that does nothing useful? ... Sometimes constructors actually need to do something to construct an object – 463035818_is_not_an_ai Mar 22 '21 at 16:03
  • 3
    C++ didn't let you do `bool ownedByPlayer = false;` inside a class before C++11. – NathanOliver Mar 22 '21 at 16:04
  • 1
    Does this answer your question? [Why we do need constructors?](https://stackoverflow.com/questions/8749248/why-we-do-need-constructors), [Use of constructor in C++](https://stackoverflow.com/questions/6574825/use-of-constructor-in-c). – rustyx Mar 22 '21 at 16:06
  • @rustyx unfortunately, both answers are quite incomplete. On the other hand, I do not think SO format is appropriate for *complete* answer to the question of the meaning of the life, **constructors** and everything. – SergeyA Mar 22 '21 at 16:08
  • 1
    Are you asking why this class must always have a constructor, or are you asking if you need to write that constructor yourself or if the default one the compiler generates is good enough? – Ben Voigt Mar 22 '21 at 16:19
  • So, a beginner like me should not consider any importance of constructors because I really didnt understand any usage – Qwerty Prasad Mar 23 '21 at 05:46

1 Answers1

2

In your case there is no need to write the constructor. Actually it is recommended to not write a constructor that does nothing but initialize members with default values. Though the correct way would be to use the member initializer list:

car() : ownedByPlayer(false) {}

And since C++11 you can use a default member initializer instead:

class car {
    // ...
    bool ownedByPlayer = false;
    // ...
};

Sometimes constructors actually need to do something. Consider a car registers itself somewhere after being constructed, then your class could look like this:

class car
{
    public:
    string name;
    long int price;
    int mileage;
    car(string name,long int price, int mileage) : name(name),price(price),mileage(mileage) {
        CarRegistrationFacility::register_car(*this);
    } 
};

Actually I found it difficult to make up an example for a constructor that does more than initializing members, because thats what constructors do. However, when your constructor does more than that, then that "more" happens in the body of the constructor.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • The uses of constructors goes way up when you remember that you can overload them. `[std::vector](https://en.cppreference.com/w/cpp/container/vector/vector) has like 10 constructors. – Mooing Duck Mar 22 '21 at 16:16
  • @MooingDuck good point, though I am on the edge of deleting this. maybe I'll improve it later, if the question doesn't get closed – 463035818_is_not_an_ai Mar 22 '21 at 16:18
  • Something like `template car(std::piecewise_construct_t, std::tuple nameArgs, std::tuple priceArgs, std::tuple mileageArgs)`, which I admit is total overkill for `car` as defined here? – Caleth Mar 24 '21 at 15:18