Class Questions

I am watching a tutorial on classes just to brush up on them and I have some questions on things that dont make sense. Here is my code:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

using namespace std;

class Enemy
{
    public:
        Enemy(int initHealth, string initName);

        void SetHealth(int HEALTH);
        int GetHealth();

        void SetName(string NAME);
        string GetName();

    private:
        int health;
        string name;
};

Enemy::Enemy(int initHealth, string initName):
{
    SetHealth(initHealth);
    SetName(initName);
}

void Enemy::SetHealth(int HEALTH)
{
    health = HEALTH;
}

int Enemy::GetHealth()
{
    return health;
}

void Enemy::SetName(string NAME)
{
    name = NAME;
}

string Enemy::GetName()
{
    return name;
}

int main()
{
    Enemy enemy(100, "Player");

    return 0;
}


So I can make a getter and setter function to get and set values, the constructor will set values when called, so the constructor just initializes variables and the setter function allows me to change it whenever I want right? so I really dont understand the point of making the constructor because I can just initialize my variables with the setter functions so idk i guess i just dont get the point.
There are some things you can't do with setters and getters.

for example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Foo
{
public:
	Foo();
	void setNum(int newNum);
private:
	const int NUM;
};

void Foo::setNum(int newNum)
{
	NUM = newNum; //illegal
}


you can't assign to NUM because it's const

However
1
2
3
4
5
6
7
8
9
10
class Bar
{
public:
	Bar(const int newNum);

private:
	const int NUM;
};

Bar::Bar(const int newNum) : NUM(newNum){} //perfectly fine 


you can initialize it in your constructor
Last edited on
Initializing an object's members outside the constructor may be, in certain circumstances, one or more of the following:
* Impossible. E.g. if the class contains member references, or if some of the members can be initialized only once and can never be modified after construction (such objects do exist).
* Undesirable. E.g. if the object is supposed to maintain certain conditions throughout its lifetime. A constructor gives you the certainty that you won't accidentally initialize the object into an inconsistent state.
* Inefficient.
Last edited on
ah I see, that makes perfect sense. so in this situation im thinking i really dont need a constructor but then again its always wise to initialize variables at startup so what should i do? Also when initializing do I do this:

1
2
3
4
5
Enemy::Enemy(int initHealth, string initName)
{
    SetHealth(initHealth);
    SetName(initName);
}


...

1
2
3
4
5
6
int main()
{
    Enemy enemy(100, "Player");

    return 0;
}



or this:

1
2
3
4
Enemy::Enemy(int initHealth, string initName): health(initHealth), name(initName)
{

}


...

1
2
3
4
5
6
7
8
9
10
int main()
{
    Enemy enemy(100, "Player");

    cout << enemy.GetHealth();
    cout << " ";
    cout << enemy.GetName();

    return 0;
}



Or are both ways valid?
Last edited on
bump
Or are both ways valid?

Have you tried both? Do they both compile and give you the same answers?

i would favour this:
1
2
3
4
Enemy::Enemy(int initHealth, string initName): health(initHealth), name(initName)
{

}

as this is actual initialisation. whereas this:
1
2
3
4
5
Enemy::Enemy(int initHealth, string initName)
{
    SetHealth(initHealth);
    SetName(initName);
}

is assignment.

http://www.cplusplus.com/forum/articles/17820/
Or are both ways valid?
Yes, but the second one is more efficient because the constructors (if any) of the member variables are called only once
This
1
2
3
4
5
Enemy::Enemy( int initHealth, string initName )
{
    SetHealth(initHealth);
    SetName(initName);
}

is exactly same as this
1
2
3
4
5
6
7
Enemy::Enemy( int initHealth, string initName )
 : health(), name() // default initialization
{
    // change existing objects
    SetHealth(initHealth);
    SetName(initName);
}


Topic archived. No new replies allowed.