LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Possible uses for classes. (https://www.linuxquestions.org/questions/programming-9/possible-uses-for-classes-385256/)

RHLinuxGUY 11-21-2005 02:51 PM

Possible uses for classes.
 
I just about completely understand how a class works, and already I think I would know how to use it, in lets say a game...

class Player
{
string playername;
int playerhealth;
public:
void stats(char,int);
}

void Player::stats(string a, int b)
{
playername = a;
playerhealth = b;
}

int main()
{
Player pa, pb;
pa.stats("p1",100);
pb.stats("p2",100);
return 0;
}

... from what I see, it could be usefull. But I would like to know other situations where the classes will/may come in handy.

Mistro116@yahoo.com 11-21-2005 02:57 PM

This is just what I've come to conclude so far in my own personal experience:

Classes are used as organization in programming, so that you don't have 100000000 functions in one file, and helps you organize with respect to the purpose of the functions by separating them into class files.

In object oriented programming, however, some languages allow you make objects from classes, so you could have an array of Players, from the example you posted, where you could have any number of players playing a game, while keeping track of each of the Players attributes.

Classes are sort of like paragraphs in a paper. You'd never know what the hell was going on if you didn't add a space and an indent mark after your thought was finished :)

Enjoy.

Mistro116

xhi 11-21-2005 05:12 PM

>> Classes are sort of like paragraphs in a paper. You'd never know what the hell was going on if you didn't add a space and an indent mark after your thought was finished :)

Classes are more like a paragraphs in a book that once the first page is written, the paragraphs can then go on to write themselves, and also spellcheck, publish and then buy themselves. :)

You are on the right track.. keep reading and writing and you will soon grasp the entire concept of why. The toughest part of learning C++ is not the actual syntax, it is getting your head wrapped around OOP and the reasons and possibilities.

chrism01 11-21-2005 07:06 PM

Classes define the strucure and methods of a possible object.
An object is an instantiation of of a class def.
To put that another way, classes are like a struct def in C, but you have to create (instantiate) a usable copy (object) to do anything with it.

RHLinuxGUY 11-21-2005 10:10 PM

Thanks you guys, because of you guys/gals, I've been graspin c++ more thoroughly whenever I ask a question here. I'm confident if I look at something in different paradimes I soon become a full C++ pro..grammer. :P


All times are GMT -5. The time now is 03:41 AM.