Classes C ++

c ++ classes, C ++ classes for beginners, privat and public access specifiers, class methods

Starting study classes, we come to an important stage – study of object-oriented programming (OOP) in C++. Classes – a fairly broad topic. So I'll break it into several parts. In this way, beginners will be quite easy to learn the information and understand the basics of this topic. In this lesson we will learn the syntax of C ++ classes, is a qualified member access (the fields) class (private and public). Learn, what methods of a class and how to contact members of the class of software.

C ++ classes like structure, but they have their own features and benefits. To do this, we have defined programmes, structures and functions of separately. In classes they are combined in a single unit, to the private data of a class could only be a function of this class. I liked the illustration from the book

& Quot; Object-Oriented Programming in C ++" P. Laforet (buy with delivery by mail)“Object-oriented programming in c++” P. Laforet (buy with delivery by mail)

c ++ classes, c++ classes for beginners, privat and public access specifiers, class methods

In our first lessons about the OOP, we will consider a simple-to-understand classes. Complex examples will only detract from the important details, relating to the definition of classes and objects. In the final lesson of the classes we summarize all the study and answer questions – What are classes and what are the basic principles of OOP.

Now let's write and let's analyze the following code:

In strings 4 – 19 is a class definition. To declare a class must use the keyword class and give it a name. The braces and describe it, at the end put ; semicolon.

c ++ classes, C ++ classes for beginners, privat and public access specifiers, class methods

After the announcement of the name of the class, it becomes the name of the new data type. With this type of class objects will be created.

The class body (between braces) data can be (basic types of variables and strings ), functions, belonging to the class, structure, classes… All this is more commonly called the members or class fields. Functions, are declared and / or defined in the class body – this class methods. Next and will call them. You just have to remember: class methods – this function, specifically designed to work with data (members) this class.

A look at the new keywords for you – private and public. In C ++, it made to protect certain classes of data from external interference. I.e, that the main function of, for example, no one could appeal directly to the data through an object and make any changes.

c ++ classes, C ++ classes for beginners, privat and public access specifiers, class methods
Attempt to access a private member of the class of main-function

To make the data “closed” It is necessary to place them in the box private . Default, everything announced in class, becomes closed from direct access and is available only to other members or methods of a class. So that, if such data are declared at the top of the class body, the word private may not be used. To declare a public class members, it is necessary to apply the keyword public. In this field are often class methods, who will work with the private (closed) data. Public (open) methods and other members of the class are available from any other part of the code / program, in which the defined class. Sometimes to work you may need to declare a public member of a class or private method.

Pay attention to the definition of the class methods. We do not share in these parameters. This makes changes to the variable number and show it on the screen. The thing is, and class members that are in the same methods Area of ​​visibility. And the methods of a class freely access to the data-member class.

In our code, we have placed the definition of methods directly in the class body, because the definition takes up very little space. But most will have to make it outside the class body or even to another file, and in the class leave only prototypes methods. Look, it looks like the definition of methods outside the class:

To specify, that the function, defined outside the class, it is his method, we must somehow associate the class name with the name of the method. To do this, first write the return type, then the name of the class, further expansion of the operation area of ​​visibility :: (double-colon), the method name and parentheses (with or without parameters): void Number::setNumber() Later, as usual, should the definition of the function body in curly braces.

Methods, defined in the class, are default inline-functions (built-in functions) unlike methods, defined outside the class.

Since the class – is a type, a certain programmer, then we are creating a class we can create objects of this type: Number object; As we discussed earlier, we can not turn to the private members of the class of the main function. But we can easily work with them, through certain methods of the class. So we call these methods through an object and assign the first variable number value, and then show on the screen.

c ++ classes, C ++ classes for beginners, privat and public access specifiers, class methods

For the first introductory lesson, information will be acceptably. The next lesson will contain tasks on classes, for, so you can consolidate their knowledge. Quest for you – before proceeding to the problem-solving, watch the video tutorial. Author Denis Markov.

To support our site – click on the piggy bank and choose any convenient way.

Newsletter of programming:

Classes C ++
4.8 (96.67%) 6 votes

3 thoughts on “Classes C ++

  1. Somewhere (or here in the class descriptions, or earlier, in the description of the structure) I need to say, that the structure, C++ inherited inherited from C, and classes – It is the same.
    By default, only for all members of the structure of the field of view is defined as public, and in classes – as private. Therefore, we are always in the classes override the visibility of some members (public, protected) – down up, from smallest (private) to greater (public, protected). In the structures simply do not need arises – top down.
    But the structure of C ++, Unlike structures C, because of such duality (with classes) They may also have their own functions methods.

  2. на видео уроке, может кому -то будет полезно , не все компиляторы умные , как у Дениса Маркова. У меня например нужно было добавлять указатель this-> чтоб аварийной ситуации не было . Еще очень мало задач по темам, у вас в начале так хорошо все было, получил информацию-получи и задачу по ней: а сейчас выучи и пойми куча информации(не легкой)а потом уже переходи к задаче.

    using namespace std;
    typedef unsigned int ui;

    class accaunt
    char *ID;
    ui balance;
    void setid(char*);
    char * getid();
    void setname(char*);
    char* getname();
    ui getbalance();
    void setbalance(ui);
    void accaunt::setid(char*id)
    this->ID = id;
    char * accaunt::getid()
    return ID;
    void accaunt::setname(char*Name)
    this->Name = Name;
    char * accaunt::getname()
    return Name;

    ui accaunt::getbalance()
    return balance;
    void accaunt::setbalance(ui b)
    this->balance = b;

    int main()
    setlocale(LC_ALL, “rus”);
    accaunt a;

    cout << a.getbalance() << endl;

    cout << a.getname() << endl;

    cout << a.getid() << endl;

    return 0;

Leave a Reply

Your email address will not be published. Required fields are marked *