classes and objects

+2 abdullh mohamed nageeb · March 2, 2015
Hi bucky I'm abdullh and you already that as I guess, now my problem is I don't know how to do a class, and I watched all tutorials.
Unfortunately I didn't solved it . So can you help me in that ?

Post a Reply

Replies

Oldest  Newest  Rating
+1 Earl Hudson · March 5, 2015
//In order to help you, we first need to see a little of the code you have tried to write.
//Seeing your code allows people to see what you've done wrong, and give you proper help.
//A class is really nothing more that variables and functions, grouped together in a container. Nothing more.
//If for example you have a player, and you want to set his attributes, like a name, intelligence, knowledge, physique, stamina, agility, and charisma... you put them in a container of "player" type.



#include <iostream>
#include <string>

using namespace std;

class Player {   // declare your class, and name it.
private:              // Now you set your access specifier to private because you don't want people to
                           // have access to your variables directly.
                          // These are called members. They are members of your class.
string Name;
int Intl;
int Know;
int Phys;
int Stam;
int Agility;
int Chrs;

                          // Just like you do anywhere else in the program, except you do not assign a value.
                          // Since they are private, you develop a method to access them. That's where your
                         // functions come in to play. In a class they are called methods. Since you want to
public:             // access your methods You need to change the access specifier to public.

void set_Name (string name) {     // This is called a set method, it's used to set the value
Name = name;                                 // to your member Name. through your method set_Name.
}
void set_Intl ( int intelligence) {   //  These methods are set to type void because like elsewhere
Intl=intelligence;                            //  in your program, they return no value.
};


string get_Name() {                        // Now you can also use a get method. These DO return a value.
return Name;                                   // This returns a string value set in the player class for the variable Name.
}

int get_Intel() {                                // This returns a int value set in the player class for the variable Intel.
return Intl;                                     // Make a set for every member.
}

};                                              // This closes the class.



int main()
{
    Player User;                                 /* Call the class and make an object
                                                    The class is player, and the object is Earl.
                                                    From now on, you only need to refer to anything in the 
                                                    as Earl. Use a dot separator  called a Dot access specifier.
                                                    The format is  Object.method... */

    User.set_Name ("Earl");                      // This line sets the value "Earl" into the object member Name.
    
    string Myname = User.get_Name();             // This method that's that value and puts it in Myname.

    cout << Myname << endl;                      // You may now print it, or do anything you want with it.

    return 0;                                    // Closes the program.
}



That explains a class line by line for you... Also keep in mind, this is the most basic class I can think of. You are not limited to set and get methods. You can make them into any type of function you wish...   I hope it helps
0 abdullh mohamed nageeb · March 15, 2015
hi,
English please ..... cause I don't understand :(:(:(
0 Earl Hudson · March 15, 2015
I put a comment on just about every line to explain what it does. But okay, we'll go through it one more time with a little more detail for you.

A Class  is anything you want it to be, Look around you, you see all kinds of examples of a Class . You see a computer screen, a keyboard, a computer... maybe some cables You look at all those things and refer to it as your "computer system" right? Well, that's a Class.. A Class is a group of variables and function that are specifically for those variables. I bet if you looked around you'd see a newspaper laying around. A Newspaper is a great example of a Class too. It has sections that all come together to make up a Class. In a newspaper you have 1. the headlines, 2. World news, 3. Local news, 4 comics, 5 sports section, 6 the obituaries. and you have other things too. Paper name, number of pages, number of photos. and so on. Since it has parts, or more than one t hing that makes up a news paper we can make that into a Class .
Each thing you want to put into your Class  is called a member. These members are represented by variables. 

So let's tell the computer you want to make a newspaper Class. How do we do that? Easy enough. We just tell the computer that's what we want to do. Give the keyword class, a name for your class, and and open curly bracket.

class NewsPaper {

Before we add the members, we need to set what is called Visibility modifiers. You have private, public , and protected.
Let's not worry about protected right now. That leaves public and private. Public would allow other people to have direct access to your members. They could modify them in all kinds of ways. We want to protect them, so we'll want to make them private. Private means no one can access them directly, not even you.  Not to worry. we'll take care of that too. We add this to the code.

class NewsPaper {
          private: 

Now lets list those members. You can use int, bool, float, or anything you want and in any combination. put your members eaxactly the same way you define any variable without a value.  type  name  semicolon

int pagenumbers;      // number of pages
int NumberPhotos    // Number of pictures
string headlines;        // one variable 
string worldnews;      // for each
string local news;      // section
string comics;
string sports;
string obituaries;

Okay, that'll do for an example. Now think about things you can do with the news paper. 1. read a section, 2. throw it away. 3. tear it up. 4 ball it up. 5 stuff it in your shoes. Now everything you do to that paper you apply a method. You read it from left to right, you read it from top to bottom... That's one method of reading the paper. Throwing it away required a method. You could throw away each section after you read it, or wait and throw it all away at once. That's your method of trashing the paper. You can tear it up one sheet at a time, that's one method, or you could ball up a section at a time, that could be another method.

These methods are simply functions. That's it. No magic, no hocus pocus. These functions (properly called methods) will allow us to access the private members we just put in. Since we want people to use use these to modify the private members, we make them public,

public:

Okay, lets define the protoypes here. 

int CountPages();
int PhotoNumber();
string readHeadlines();
string readWorldnews();
string readLocalNews;();
string readComics();
string readSports();
string read obits();

Put in all the methods you wish here, just remember, these are prototypes. You will make the actual functions outside the class. Now let's close the class with a closing curly bracket and semicolon.
 
};

So your class (Newspaper) will look like this :

class NewsPaper {
          private: 

int pagenumbers;     
int NumberPhotos    
string headlines;        
string worldnews;     
string local news;     
string comics;
string sports;
string obituaries;

          public:
int CountPages();
int PhotoNumber();
string readHeadlines();
string readWorldnews();
string readLocalNews;();
string readComics();
string readSports();
string read obits();
};
 
I made these prototypes without arguments to pass or use, but the odds are you will need to put some arguments in them. 
Now, when you write your functions they will will call the prototypes, which in turn access the members. Once you have them written, there is but one thing left to do, and that is make an instance of your class.
In your main, just type your class name, and the name of your object. Like:

newspaper Sunday;

Now Sunday is an object of the newspaper class. It's just that easy to write a simple, basic no frills class. You can learn about constructors, deconstructors, Scope resolution and as well, they are for classes that slightly more complex. As a personal note I'd like to say Writing a class is the easiest thing to do, and one thing that will give you many problems. The more you put in your class, the easier it is to cause an error. Watch buckys videos and try to make a class of your own. Some people take weeks to figure out it is nothing more  than a container to hold your variables and functions pertaining to one item. That's fine, take the time, write your own classes as practice, then learn about contructors, deconstructors... You'll see how they make your life easier. Then learn about scope. This will give you a migraine if you don't know about scope. Watch buckys videos, over and over until you get it. They look scary at first, but they're pretty easy.

Enjoy.
  • 1

C++

107,338 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator