Sign in to follow this  

Classes in C++

This topic is 3939 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having kind of a hard time understaning classes and what they are used for. I have skipped this chapter in the book I am reading because I really am not understanding whats going on. Any links to tuts or articals that explain classes better would be greatly appreciated. Thank you, Glyvin

Share this post


Link to post
Share on other sites
thank you for the response, I am reading the teach yourself C++ in 21 days book and the classes chapter just kinda confused me, is it something that I will need to use and when will I need to use it?

Share this post


Link to post
Share on other sites
A class is like a blueprint of and object. It defines it's shape and behaviour.

E.g. let's have a class Human. The class could have attributes like hair_colour, size, weigth and other attributes describing its shape. But it also can have functions like walk(), eat() and similar which would work on the object, depend on its attributes and maybe even alter some states.

You would be an instance, an object of a Human class and you would have access to all the functions and attributes defined in the class Human.

I am sure there are enough technical tutorials out that will explain you the declaration and implementation of classes and objects, it won't be difficult once you understand that an object is an abstraction of an actual physical object ( at least this was the intention of it originally ).

Edit to your second response:
Yes, you will need classes when programming object oriented, it is basically the core of that paradigm.
Now that you know the abstract meaning of classes and object, you will understand that technically it is nothing but a way to organise code.

Share this post


Link to post
Share on other sites
If your book is one that refrains from sucking, then you're probably already USING classes. Does your book use std::string, std::cout, std::cin, or std::vector? Those are classes. In terms of game design, think of an object you might want to include in your game, like an enemy. An enemy has certain attributes, like a name, health, attack power, defense, speed, etc. It may be carrying items that are dropped when it dies. An enemy can also perform certain actions, like attacking, running away, performing spells, etc. And that right there gives is most of the class design!
class Enemy
{
// attributes
std::string name;
int health;
int attackPower;
int defense;
int speed;
public:
// actions
int attack(); // returns a number that represents strength of attack. This number is based upon it's attributes with a random variation
void run(); // the enemy runs away
int castSpell(int spellID); // given a certain spell, this will return a number that represents the strength of attack with a random variation
void heal(int healAmount); // heals self a certain amount
void damage(int damangeAmount); // specifies amount of damage to deal to enemy
bool dead(); // returns true if enemy is dead, false otherwise
const std::string &getName();
// this constructor sets all attributes when we create the enemy
Enemy(const std::string &s_name, int s_health, int s_attack, int s_defense, in s_speed);
};
// player's class defined in a similar way
int main()
{
Enemy troll("Dimble The Troll", 1000, 200, 140, 10);
Player guy("Insert Username Here", 2000, 340, 200, 15);
while(true) // play until we break(in this case, when someone dies
{
int guyAttack = guy.attack();
std::cout << guy.getName() << " deals " << guyAttack << " to " << troll.getName() << std::endl;
troll.damage(guyAttack);
if( troll.dead() )
{
std::cout << troll.getName() << " is dead!" << std::endl
break;
}
int trollAttack = troll.attack();
std::cout << troll.getName() << " deals " << trollAttack << " to " << guy.getName() << std::endl;
guy.damage(trollAttack);
if( guy.dead() )
{
std::cout << guy.getName() << " is dead!" << std::endl
break;
}
}
return 0;
}

Of course this is just meant to show the basic idea behind classes. In reality there are many things horribly wrong with my code that can and should be improved on. I didn't compile it, of course, and it's unfinished anyway. In reality Enemy and Player are behaving in pretty much identical ways and we could just make one class for them both. There's no input from the user. There's duplicate code that can be removed if we treat Enemy and Player the same. Etc.

Share this post


Link to post
Share on other sites
A class is how you create your own data type. After all, you want your data to represent the "things" that are important in your code, and the creators of the language can't have predicted what those would be, so they give you only the lowest-common-denominator ones (like int etc. to represent numbers, as well as a few more complex ones like std::string - which in fact are built using this class mechanism), and then give you the ability to create your own. Of course, they have to be built up out of something; we build them up by using the existing types to store the bits of information that are relevant to representing (modelling) our "thing", and adding 'member functions' to represent what our things can "do".

Share this post


Link to post
Share on other sites

This topic is 3939 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this