# Using Classes

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

## Recommended Posts

I looked at gametutorials.com and cprogramming.com and my programming book. I still dont know how to use them it is kind of confusing. This is for someone who is willing to expain to a noob how to use and access it.
class Point{
private:
int x, y;
public:
void set(int new_x, int new_y);
int get_x();
int get_y();
};

THANK YOU!!!!

##### Share on other sites
Point p;p.set(10,20);int z = p.get_x()+p.get_y();//orPoint* p = new Point;p->set(10,20);int z = p->get_x()+p->get_y();// when done delete pdelete p;p = 0;

##### Share on other sites
Ok. first, you dont need the "private:" line since it is in private by default in classes (but not in structs).
by using it, do you mean how to create your own classes? right now Point can't do anything, its functions has no body..

pex.

##### Share on other sites
Ok, basically, a 'Class' is a glorified 'Struct'. The only major differences are Encapsulation and it allows functions as well.

Encapsulation is the ability to deem members of the class private or public whereas a private member can only be access from inside the class, and a public member can be accessed anywhere. Example

class Point{private:   int x, y;public:   void set(int new_x, int new_y);   int get_x();   int get_y();};Point cPoint;cPoint.x = 2;    // ERROR.. cannot access this member because it is privatecPoint.y = 3;    // ERROR.. cannot access this member because it is privatecPoint.set(2,3); // This will work because it is a Public function of the class

Now, a class also provides you with the ability to create functions that are specific to that class only. For example

class Point{private:   int x, y;public:   void set(int new_x, int new_y); //This is a class function declaration   int get_x();   int get_y();};void Point::set(int new_x, int new_y) //this is a class function definition{   x = new_x; //This sets the Private member 'x' to the passed parameter 'new_x'   y = new_y; //This sets the Private member 'y' to the passed parameter 'new_y'}

The reason why i was able to set the Private members of the class is because it is being done within the class itself (void Point::set(int new_x, int new_y))

I hope that helps!

##### Share on other sites
Quote:
 Original post by USArmyMortarsOk, basically, a 'Class' is a glorified 'Struct'. The only major differences are Encapsulation and it allows functions as well.

Not quite: structs can have everything that classes can. In C++, the only difference between structs and classes is the default visibility: members of a struct are public by default, classes are private. The following example are identical:

struct MyStruct {  int field1, field2;};// same as abovestruct MyStruct {public:  int field1, field2;};class MyClass {  int field1, field2;};// same as aboveclass MyClass {private:  int field1, field2;};

You'll commonly see STL implementations use structs instead of classes for simple types that have no data.

Of course, it's considered good practice to use 'class' for types that are 'object like'. Struct should be reserved for data-only constructs.

##### Share on other sites
classes also have constructors (i dont think structs have them, correct me if im wrong cause i dont use them much)

Also with classes you can have inheritance i dont know if i should be gettting into this sence your new to programming but y not:
class Object{  public:     Object() {x = 0; y = 0; I_Image = NULL;};     int x,y;     Image* I_Image;     void Draw(Image* Destination);};class Player: public Object{  public:    Player(int New_X,int New_Y,Image* New_Image,int New_Health) {x = New_X; y = New_Y; I_Image = New_Image;Health = New_Health;};//as you can see here the player class is a type of object so it retains all the variables such as x y and image but it also brings its own new variables too like Health    int Health;};

if this is really confusing dont worry this is one of the harder things dealing with classes

hope i helped [smile]

##### Share on other sites
Quote:
 In C++, the only difference between structs and classes is the default visibility: members of a struct are public by default, classes are private.

Not true - structs inherit publicly by default, classes privately:

struct Base{	int x;};struct Derived11 : Base{};class Derived21 : Base{};int main(){	Base b;	Derived11 d11;	Derived21 d21;	b.x = 0;// Next line OK	d11.x = 0;// Next line fails	d21.x = 0;	return 0;}

Quote:
 classes also have constructors (i dont think structs have them, correct me if im wrong cause i dont use them much)

Sorry - you're wrong. The only difference is the default member and inheritance protection levels.

Of course, someone will now proceed to prove me wrong [smile]

Jim.

##### Share on other sites
ok thanks guys for the input. I will just practice using them both and find out what will best suite me.

##### Share on other sites
How can I use the following code in class to actually change data to allow combat to be calculated or ubdated after damage has been taken when a player has chosen a character.

cout<<"Choose a Character number and press ENTER: \n";
cin>>choice;
cout<<"\n";

if(choice < 1 || choice > 4){cout<<"You chose a wrong Number.\n";
cout<<"\n";
NewGame();}

switch(choice)
{
case 1:
cout<<"\n";
cout<<"You chose the Warrior: \n";
cout<<"\n";
cout<<"Strength : "<<War_status.get_strength()<<endl;
cout<<"Dextarity : "<<War_status.get_dextarity()<<endl;
cout<<"Vitality : "<<War_status.get_vitality()<<endl;
cout<<"Magic : "<<War_status.get_magic()<<endl;
cout<<"\n";
GamePlay();
break;
case 2:
cout<<"\n";
cout<<"You chose the Knight: \n";
cout<<"\n";
cout<<"Strength : "<<Kni_status.get_strength()<<endl;
cout<<"Dextarity : "<<Kni_status.get_dextarity()<<endl;
cout<<"Vitality : "<<Kni_status.get_vitality()<<endl;
cout<<"Magic : "<<Kni_status.get_magic()<<endl;
cout<<"\n";
GamePlay();
break;
case 3:
cout<<"\n";
cout<<"You chose the Elf: \n";
cout<<"\n";
cout<<"Strength : "<<Elf_status.get_strength()<<endl;
cout<<"Dextarity : "<<Elf_status.get_dextarity()<<endl;
cout<<"Vitality : "<<Elf_status.get_vitality()<<endl;
cout<<"Magic : "<<Elf_status.get_magic()<<endl;
cout<<"\n";
GamePlay();
break;
case 4:
cout<<"\n";
cout<<"You chose the Wizard: \n";
cout<<"\n";
cout<<"Strength : "<<Wiz_status.get_strength()<<endl;
cout<<"Dextarity : "<<Wiz_status.get_dextarity()<<endl;
cout<<"Vitality : "<<Wiz_status.get_vitality()<<endl;
cout<<"Magic : "<<Wiz_status.get_magic()<<endl;
cout<<"\n";
GamePlay();
break;
}

}

void GamePlay()
{

srand(time(NULL));

cout<<"You Entered the Game";
}

##### Share on other sites
Think about the examples above. Just create one class. Then use private member variables to hold the class information (i.e. strength, dextarity, vitality, magic).

Now use 'setters and getters' Put these FUNCTIONs in the public section of the class.

Create a class.h (or whatever you wish to name it) to hold your class declaration:
class Class_Name{private:  int strength;public  int get_Strength(); //Getter  void set_Strength(int new_strength); //Setter}

Now, create a class.cpp (or whatever you wish to name it) to hold your class definitions:
#include "class.h" //your class header //getterint Class_Name::get_Strength(){  return strength;}//settervoid Class_Name::set_Strength(int new_strength){   strength = new_strength;}

Now just add all the other class data in this one, and you should be golden!

I hope you have a good understanding [smile]

##### Share on other sites
Okay I have done that but How can I get it to when a person enters the game that I can have the states appear up and they can start fighting because I have already have done what you said and created an enemy cpp and weapons and spells cpp. I just do not know how to initiate the combat sequince and THANKS FOR YOU HELP!!!!

##### Share on other sites
Well I am now having headaches. I used that advice and now it cant find my links. I saved my hero information as Hero_Class.cpp

Then I loaded it in the preprocessor #include <Hero_Class.h> and it says no such file or directory exists when I try to compile it.

##### Share on other sites
use quotes and not < >. That is my suggestion.

#include "Hero_Class.h"

##### Share on other sites
The angel brackets (or however these are called: < >) in #include are for including a file that can be found in the compiler directories or in one of the directories that are in PATH.
The quotes (" ") are for including a file from the specified path.
#include "Hero_Class.h"
is same as looking for a file in Windows Explorer in path:
./Hero_Class.h (. means current directory)
so you can do "../Headers/Hero_Class.h" if Hero_Class.h is in Headers directory and Headers is inside the same directory as the current directory is.

P.S. Have you tried this website? it has a very good c/++ tutorial.

pex.

##### Share on other sites
Thanks for the reply and I have a great book. I sent my code to my freind who is a programmer as well and he used VC++.net and the samething happened to him. The program cannot find the Hero_Class.h file. It says there is no .h file when there is and it is in the same path.

##### Share on other sites
Quote:
 Original post by C_Programmer0101Well I am now having headaches. I used that advice and now it cant find my links. I saved my hero information as Hero_Class.cpp Then I loaded it in the preprocessor #include and it says no such file or directory exists when I try to compile it.

If the bolded words are true, that is your problem