PDA

View Full Version : [tut]Classes/structs



TheLastSecond
11-06-2009, 01:36 PM
ok well if you have been stuying C++ for a while its time that you finally get into true OOP
Object Oriented Programming
now C++ is an OOP
and the point of OOP is so that like other programmers can easily and quickly see whats going on what the variables do and much easier to edit and work with
this is one reason C++ has scopes of course there are other reasons
such as how many of you do this


int main(){

int a;
int b;
int c;
int d;
bool e;
float f;

cout << a;
}

and so on and make all of your variables gloabal inside of main
this is not clean OOP
however technicly the code above is clean because there is only the global scope and the local scope main

however classes were made for C++ so that programmers could take an object and make features

now a class and structures could be used to maybe hold the values of players and always should be
now the syntax for the class is this


class name {
}name(optional);
[php]

now so if i were to make a class for a player i would do this

[php]
class Player {
private :
int health;
int strength;
int speed;
public :
void attack();
void defend();
void move();
};


now i am going to explain this
first im making a class named Player
then im using a new command
private :
i will go into private later
then public :
i will go over public later
then you see some function definitions
the same clean C++ rule applies that you should make a function definition in a class then make the actual function later
now in order to implement this into a code you could do this



#include <windows.h>
#include <iostream>

using namespace std;

class Player {
private:
int health;
int strength;
int speed;
public:
void attack();
void defend();
void move();
};

int main(){

Player guy;

guy.attack();

}

ok now if you compiled this you would get an error because i used attack without defining it but besides that
you know the class so lets go to int main now
you see Player guy;
this is making guy have value of Player Player is now acting like a variable type
ect saying the C++ programmers made int using a class or struct they would have done

class int {
++(increment)
--(decrement)
value can be from 10000 to 1321432
};

now dont get the idea that will work they didnt use clasees and dealt with binary when making the types but it gives you the idea
then you see this


guy.attack();

things made from classes use the dot operator to access the things in the class
so lets say attack made health decrease
then it would guys health would decrease when guy.attack(); was called
now
lets say i tired to do this


guy.health = 100;

i would get an error however not for what you think
this is where public and private come in
private means that what ever is in there cannot be accessed directly
however they can be access by the things in the public and public can be accessed directly
now all of your ints bools and floats and all the variables should be private in the class
this is because we dont want programmers to worry about the details
then have them accessed throught the fuctions in public

so what if we did this


class test{
private:
int health;
public:
int getHealth();
void SetHealth();
};

int main(){

test itest;
thealth = test.getHealth();
test.setHealth();
}

now assumming this was the code for getHealth


int test::getHealth(){
return health;
}

then thealth would get the value of health in the class
now lets say setHealth was this


void test::setHealth(){
health = 100;
}

this would make the value of tests health = 100

understand them a bit more now?
now why is there a ; after the closing bracket for a class this is because
you can declare your class variables without making them in the main
ex


class superman {
int health;
}SUPERMAN;

you could also make multiple ones at a time


class superman {
int health;
}SUPERMAN,HELLO,THISGUY;


now the diffrences about structs and classes is data structures dont have public or private thats it same declarations and everything


struct hi {
stuff
};

now i have spent an hour on this and i will go back and proof read please reply

YouTuber
11-06-2009, 01:43 PM
Nice Tut
This should help alot of people

TheLastSecond
11-06-2009, 01:48 PM
thanks dude i like to here comments like that

Royce
11-06-2009, 04:50 PM
test test;

You couldn't declare a class like this if the class variable 'test' is now considered a keyword.

Same as saying:
int int;


test myTest;

TheLastSecond
11-06-2009, 05:28 PM
really hmmmm are you sure
cause in d3d my vertex class is call it

its

struct CVERTEX {
float x,y,z;
DWORD color;
}CVERTEX;

and it works fine
kk well anyways thanks for the feedback i will check on that issue
if it is true thanks for the tip