Advertisement

Text RPG game question

Started by May 26, 2001 08:25 PM
12 comments, last by MARS_999 23 years, 8 months ago
From what I have seen it is used to protect data and hide it from being changed by unknown functions?

Windows 98/SE/ME SUCKS!
Deal with it!
if(windows crashes)
run Linux
else
yeah right!!

RESIST WINDOWS XP!!!!!!!!!!
RESIST .NET TECH!!!!!!!!!!!
Imo it makes it look cleaner too.

class human
{
int age;
int weight;
int height;
public:
void GetAge()
{
return(int age);
}
int SetAge(int hage)
{
age = hage;
}
// and so on with the rest
};

human Tom;

Tom.SetAge(5);

cout << "Tom is " << Tom.GetAge() << " years old\n";


This looks a lot cleaner and safer then using only public. Not to mention less mix ups. I hope i got the coding right since i am still learning too.

- Goblineye Entertainment
Advertisement
The deal with variables in a class is they are to be private by default. In a struct they are public. You can if you want make variables private in struct''s but you will have to do it this way
struct human
{
private:
int age;
int gold;
int level;
public:
void GetAge()
{
return(int age);
}
int SetAge(int hage)
{
age = hage;
}
};//end of class

This way is no differnt than the class metioned before but uses a struct instead! Just remember structs are public by default and classes are private by default. I think I will be moving to classes instead of structs myself once I mess around with constructors and deconstructors instead of letting the compiler do it. =) Not allowing others to modify your varibales unless you let them I think is the point that is to be made with classes?

The main advantage of classes over structs that I know of is this. Let's say you define a struct with all of it's variables and functions. Then you instantiate several of those structs like "MyStruct a, b, c, d, e;" and so on. In your RAM, memory is allocated for each member variable and function of EACH struct that you make. In a class, however, memory is ONLY given for the member variables of each class (the stuff that actually changes from instantiation to instantiation). Memory is allocated for the member functoins of the class, but all instantiations reference that same memory so as not to waste memory. Not to mention classes are better because you can do cool stuff like inheritance.

I think all of that is correct. If not, I'm sorry.





Edited by - Live2Code on May 30, 2001 2:10:16 AM
while(my_life[years++]!=null) { code();}

This topic is closed to new replies.

Advertisement