Classes constructors and getter and setter

I tried to create an example to practice classes and I am a little bit confused.
As I understood:

Constructor are used to initialize classes.

And the getter and setter makes data accessible without making it public.

I am still a little bit confused:
What are the differences between constructors and setter and getter?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
         class Unit
        {
            protected int health;

            public int GetHealth()
            {
                return health;
            }

            public void SetHealth(int p_health)
            {
                health = p_health;
            }

            public Unit() // default constructor
            {
                health = 100;
            }

            public Unit(int p_fuel)
            {
                health = p_fuel;
            }
        };

        class Footman : Unit
        {
            private int weapons { get; set; } // what could be the usage of this

            public Footman(int damage)
            {
                weapons = damage;
            }

        };
A constructor is called when the object is created. The constructor should initialize all member variables so that the object is ready to use.

A getter is a function that returns the value of a member variable.

A setter is a function that changes the value of a member variable.

Is the code that you have posted C#?
Is it is C#, how did you know?

Also the differences are little.
How did I know? I didn't know for sure because I have never used C# but I have seen people posting C# code here before. The giveaway was that public/protected/private was applied for each member and the use of the { get; set; } syntax.
Yes it's obvious protected: int health in c++ instead of what I wrote. I should have adapted the code for c++ since this is a c++ forum, but at a first glance I thought that the differences are not very noticeable.
Topic archived. No new replies allowed.