The basics of programming in c++ for beginners

Structures in C ++. Part 2

With C ++ structures we have previously learned in the previous article. Continue to introduction.

Definition structure is desirable to have outside main() functions. Then it will be able to work with other programmer-defined function, as shown in our example of the first part. This declaration called external.

Инициализация. The elements of the structure can be initialized directly when declaring object.

We declare the object volume1 in string 13 and initializing it.  It appears to initialize the array elements – in braces and separated by commas. These data will be recorded in the relevant elements of the structure of order. In what order are defined in the structure elements – and then will record values. The operation was still possible not to use (according to the C ++ 11).

Consider the following example for initializing a more complex structure WonderfulWoman:

c++ structures, structure c ++, struct c++. nested structures

The object of this structure can be initialized so:

c++ structures, structure c ++, struct c++. nested structures

But if the structure contains more than two or three elements-it is advisable not to do. It's a bit confusing and complicates the readability and understanding of the program.

Announcement of objects. Create the structure object can be directly during its determination. To do this, you must give the name of an object between a semicolon and a closing brace:

In c++ there is an opportunity to define a structure without a descriptor (unnamed type):

In this case it is necessary declare objects in determining the structure. In the main function, you will not be able to create other objects of this structure, since no descriptor. This technique makes sense to apply, if the structure of objects will be very small – 1 or 2.

Assignment (=) for structures. For objects of the same structure can be used the assignment operator = . Operation assign elements of one object values ​​of the elements of the second object.

This so-called element-wise or termwise assignment. On the screen:

c++ structures, structure c ++, struct c++. nested structures

It worked – all elements of the structure of the objectvolume2 steel elements are objectvolume1.

Memory occupied structure. Consider the example: using the operatorsizeof, find out how much memory each structure element individually. We calculate the total amount of memory elements. Then applysizeof to the object structure and see, sizes that do not match.

On the screen:

c++ structures, structure c ++, struct c++. nested structures

sizeof showed us how much memory each structure element HandsomeMan. We have summarized these values ​​and received 25. That is, the idea of ​​the structure should take 25 bytes of RAM. But when we apply the operator sizeof  to the structure – get the value of 28.

The size of the object structure is not always equal to the sum of its elements size.  This happens because of the alignment elements of different lengths. Therefore, the structure can meet the anonymous memory locations. If you need to know how many actually occupies in the memory structure – receivedsizeof , as in the example.

This video is from the previous lesson. Who has not watched – take time

We learned how to define structure, how to declare and initialize their objects. If we need to create more than 2-3 structure of the object, then it is better to create an array of structures. This topic was discussed in the next article.

6 thoughts on “Structures in C ++. Part 2

  1. strangely, but with the name of the structure in trouble, or rather when the input
    volume = { “name”, etc ;

  2. given an array, which stores information about the train schedule on this-
    dnyashny day: train number, name (i.e.. from where to where, for example, new-
    Russian-Moscow), time of arrival at the station and time of departure (clock,
    minutes). For this time to define, some of the trains are now
    station.

  3. You want to display the data of those students, whose math assessment 8 or 9 and last name begins with A. Why can not you can not compare the address and anything else in the if statement??
    #include
    using namespace std;

    struct Grades // this structure we put in the structure of the Student
    {
    int physics;
    int maths;
    int informatics;
    int chemistry;
    };

    struct Student
    {
    char surname[32];
    int year_of_birth;
    int number_of_groope;
    Grades semester_grades; // nested structure(evaluation per semester)
    double average_score;//GPA
    };

    void showData( Student *Obj);
    void showData_input(Student *Obj);

    int main()
    {
    setlocale(LC_ALL, “rus”);

    Student data[1];

    for (int i = 0; i < 3; i )
    {
    cout << i + 1 << " student: " << endl;
    showData_input(&data[i]);
    showData(&data[i]);
    if ((&data[i].surname[0]) == 'A' && (&data[i].semester_grades.maths == 8 || 9))
    {
    showData(&data[i]);
    }

    }

    system("pause");
    return 0;
    }

    void showData( Student *Obj)
    {
    setlocale(LC_ALL, "rus");
    cout << "Фамилия: " <surname << endl;
    cout << "Год рождения: " <year_of_birth << endl;
    cout << "Номер группы: " <number_of_groope << endl;
    cout << " Evaluation per semester: \nFizika: " <semester_grades.physics << "Математика: " <semester_grades.maths << "Информатика: " <semester_grades.informatics<<"Химия: " <semester_grades.chemistry << endl;
    cout << "Средний балл: "<average_score;
    cout << endl;

    }
    void showData_input(Student *Obj)
    {
    setlocale(LC_ALL, "rus");
    cout << "__________________" << endl;
    cout << "Input student data" << endl;
    cout << "__________________" << endl;

    cout <> Obj->surname;
    cout <> Obj->year_of_birth;
    cout <> Obj->number_of_groope;
    cout <> Obj->semester_grades.physics >> Obj->semester_grades.maths >> Obj->semester_grades.informatics >> Obj-> semester_grades.chemistry;
    cout <> Obj->average_score;
    cout << endl;

    }

  4. Even again, without any explanation, new terms and lines come out, Even again, without any explanation, new terms and lines come out.

Leave a Reply

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