The basics of programming in c++ for beginners

Classes C ++

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 Object-Oriented Programming in C++” P. Laforetc ++ 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, which concern the definition of classes and objects. In the final lesson about classes, we will summarize everything we have learned 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 class is defined. Sometimes you may need to declare a public member of a class or a private method to work.

Pay attention to the definition of the class methods. We do not share in these parameters. This makes changes to the variablenumber 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 defaultinline-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 variablenumber 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.

4 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. Video tutorial, can be useful to someone, , Not all compilers are smart , like Denis Markov. I like this- it was necessary to add a pointer> so it was not an emergency . Yet very few problems on the topics, you have in the beginning everything was so well, received information and task-get over it: and now to learn and understand a bunch of information(not easy)and then proceeds to task.
    #include
    #include

    using namespace std;
    typedef unsigned int ui;

    class accaunt
    {
    private:
    char * ID;
    char*Name;
    ui balance;
    public:
    accaunt(void);
    void setid(char*);
    char * getid();
    void setname(char*);
    char* getname();
    ~ accaunt(void);
    IU getBalance();
    void setbalance(ui);
    };
    accaunt::accaunt()
    {
    }
    accaunt::~ accaunt()
    {
    }
    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;

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

    int main()
    {
    setlocale(LC_ALL, “rus”);
    accaunt a;
    a.setbalance(123);
    a.setid(“444”);
    a.setname(“Where”);

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

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

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

    return 0;
    }

  3. Withdrawing 57 356 $. Go to withdraw > https://forms.yandex.com/cloud/65e6f69bf47e7306ad1bf0da?hs=d1a64614ffe22ed5ce0f7946c54330d2& says:

    7f835p

Leave a Reply to Jury Cancel reply

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