So... I have been doing some tutorials from this site and others. I believe it will take me longer than most to completely understand why all the lines are needed to make a class.
I have been working on a tutorial that I believe is discontinued but I would like to understand some of the lines that I don't understand. I will post the class and I will put comments by what I don't understand..and hopefully if someone is not too busy they could help me get a better understanding.
MonsterDataClass.h // First Question is: Is the Class always the .h?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
#define MAX 256 // What does this line do?
class cMonsterData
{
public:
int Health;
char Name[MAX]; // Okay I believe this is a var declaration but why does it say [MAX]?
void Print(); // Why are we using void? Could we not use void?
void SetName(char sName[MAX]); // Why is this line even needed? What does it do?
void SetHealth(int iHealth); // Why is this line even needed? What does it do?
};
MonsterDataClass.cpp // Why is this .cpp with no main? I always
understood everything had to have a main?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include "MonsterDataClass.h"
void cMonsterData::Print() // Why are we printing again? We did this in the .h?
{
cout << "Name: " << cMonsterData::Name << endl; // What are the :: for?
cout << "Health: " << cMonsterData::Health << endl << endl;
}
void cMonsterData::SetHealth(int iHealth) // What doe this line do?
{
cMonsterData::Health = iHealth; // I know this line sets the new health.
}
void cMonsterData::SetName(char sName[MAX]) // What does this line do?
{
strcpy(cMonsterData::Name, sName); // I know this copies the new data into the name variable.
}
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include "MonsterDataClass.h"
int main()
{
cMonsterData Godzilla;
Godzilla.SetHealth(100);// How does the compiler understand the word Set?
Godzilla.SetName("GoDzIlLa!1!1");
Godzilla.Print(); //Why do we keep printing?..lol
Godzilla.Health = 200; // How does the compiler understand what health is?
strcpy(Godzilla.Name,"CelestialKey is Godzilla!")
Godzilla.Print();// lol another print
getchar();
return 0;
}
Thanks for any help guys. I know there is alot... but heck even if you can only explain1 or 2 of them that would be amazing. Sorry about not knowing that much yet. Thanks again!
char Name[MAX]; // Okay I believe this is a var declaration but why does it say [MAX]? Name is a char array, with the size of 256
void Print(); // Why are we using void? Could we not use void? Because we don't want to return a value. Yes, we could not use void, say int Print();, maybe you want to return -1 to indicate that the function failed or non negative on success.
void SetName(char sName[MAX]); // Why is this line even needed? What does it do?
You can read it as, SetName is a function that takes a char array as an argument and it does not return a value
void SetHealth(int iHealth); // Why is this line even needed? What does it do?
SetHealth is a function that takes an int as an argument and it does not return a value
Why is it in this line: void SetHealth(int iHealth); There is an i before Health?
Well, the argument can't have the same name as the member variable... How would you do the copying then? I'm not sure which one would override the other but the fact that only one would be dominant means that you can't access the other. The argument's name could be anything, it could be like this for example:
1 2 3 4
void cMonsterData::SetHealth(int asdf) // What doe this line do?
{
cMonsterData::Health = asdf; // I know this line sets the new health.
}
I believe the prefix 'i' is put to denote that the argument is an integer, much like the prefix 's' in the argument of the following function indicates that that argument is a string
EDIT: Ok, if the argument has the same name as the member it's the argument that overrides the member but you can access the member like this->member; and still do the copying. Consider the following: