0

OOP is giving me a bad headache :( because I understand how constructors work but I don't understand what are they there for.

I have a class called Player that looks like this:

class Player
{
private:
    std::string name;
    int health;
    int xp;
public:
    //Overloaded Constructor
    Player(std::string n, int h, int x) 
        {
        name = n;
        health = h;
        xp = x;
        }       
};

int main()
{
    Player slayer("Jimmy", 1000, 200);
    return 0;
}

Insted of using the constructor there (I have no idea what it does or what is it for) I could use set_name and get_name methods where I would set the name - and do the same with health and xp - and then get the name using functions. So, a constructor replaces functions???

What's the purpose of it if it doesn't replace set & get then? Also, the same constructor above I can use it to initialize my attributes and avoid garbage:

Player(std::string n, int h, int x) : name{n}, health{h}, xp{x} {}

But then I can also do this:

Player(std::string n, int h, int x) : name{n}, health{h}, xp{x} { name = n; health = h; xp = x;}

I don't understand, what does all that mean? I know in the first case I am initializing it, and in the second I am saying that the name health and xp equals whatever I am going to set it to.

Knowing all this information, I still don't know what is a constructor and what should I use it for. It's very confusing.

  • 2
    A constructor doesn't replace setters. It just allows you to specify the initial state of your object. If it makes sense for your type to be default constructed then you can leave out the constructor. But that doesn't make sense for all types. – François Andrieux Dec 30 '20 at 19:36
  • How would you create a `const` object without a constructor? – alter_igel Dec 30 '20 at 19:38
  • 2
    Just because this particular class does not strictly need a constructor, that doesn't mean that no classes in C++ need a constructor. Try to write a class with a reference or a `const` member that does not have an explicit constructor, and see how well that works out for you. – Sam Varshavchik Dec 30 '20 at 19:39
  • Think about the `string` class. `string` has a pointer which points to the string data. Because it has a constructor, it's impossible to create one with an invalid pointer! Imagine if you had to write `string s; s.set_string("hello world");` instead of `string s = "hello world";` – user253751 Dec 30 '20 at 19:41
  • a constructor is just a function you can't not call – user253751 Dec 30 '20 at 19:42
  • Maybe this will help: https://stackoverflow.com/questions/926752/why-should-i-prefer-to-use-member-initialization-lists – rustyx Dec 30 '20 at 19:42
  • 1
    Last of your snippet has not sense at all, because you're initializing variable twice. Construction with member inizialization list is preferred because is easy to optimize, and it wasn't allow before C++11 – Moia Dec 30 '20 at 19:43
  • @user253751 so, suffice to say, constructors are there only to initialize, that's all, right? – Luigi Istratescu Dec 30 '20 at 19:43
  • 1
    @LuigiIstratescu Well it's up to you what to use them for. Usually they are used for initializing things that have to be initialized. If your `name`, `health` and `xp` don't have to be initialized, then you're allowed to not initialize them. As you get more experience with programming, you *might* find that it's helpful to initialize things like these, or you might not. – user253751 Dec 30 '20 at 19:44
  • 1
    @LuigiIstratescu Yes, the purpose of a constructor is to setup the initial state of your object. A construct is run when a new instance of an object is created, then no constructor is ever executed again for that instance. Note that you can also write constructors such that they can have side effects other than purely initializing the object, but the purpose of the constructor is still to do work when constructing a new instance of a type. – François Andrieux Dec 30 '20 at 19:47
  • @user253751 great! thanks, I get it now. Also, thanks Francois Andrieux! – Luigi Istratescu Dec 30 '20 at 19:51

1 Answers1

1

A constructor serves a very important purpose in C++. With a properly written constructor, you know that an object is always valid. It can never be allocated but somehow in an invalid state.

Typically, if your class has a set method which sets all of its state, you would simply have the constructor execute set with a proper set of default values. Then you don't need to duplicate code, and your object has the nice property of being in a valid state from the beginning.

Tumbleweed53
  • 1,491
  • 7
  • 13