Using Classes

Started by
15 comments, last by Zahlman 19 years, 3 months ago
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!!!!
Computers are worlds of Exploration?
Advertisement
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;
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.
pex.
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!
HHC 1st Battalion, 5th Infantry Regiment1st Brigade (SBCT), 25th Infantry DivisionRest In Peace SSG Julian Melo
Quote:Original post by USArmyMortars
Ok, 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.
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]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
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.
ok thanks guys for the input. I will just practice using them both and find out what will best suite me.
Computers are worlds of Exploration?
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<<"Please chose again"<<endl;
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";
}

Computers are worlds of Exploration?
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]
HHC 1st Battalion, 5th Infantry Regiment1st Brigade (SBCT), 25th Infantry DivisionRest In Peace SSG Julian Melo

This topic is closed to new replies.

Advertisement