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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include<iostream> using namespace std; struct Size { int breast; int waist; int hips; }; int main() { Size volume1 { 90, 60, 90 }; return 0; } |
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:
The object of this structure can be initialized so:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<iostream> using namespace std; struct Size { int breast; int waist; int hips; } volume1, volume2; // объявляем два объекта int main() { setlocale(LC_ALL, "rus"); volume1 = { 90, 60, 90 }; volume2 = { 100, 100, 100 }; return 0; } |
In c++ there is an opportunity to define a structure without a descriptor (unnamed type):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<iostream> using namespace std; struct // дескриптор отсутствует { int breast; int waist; int hips; } volume1, volume2; // объявляем два объекта int main() { setlocale(LC_ALL, "rus"); volume1 = { 90, 60, 90 }; volume2 = { 100, 100, 100 }; return 0; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include<iostream> using namespace std; struct Size { int breast; int waist; int hips; } volume1, volume2; // объявляем два объекта int main() { setlocale(LC_ALL, "rus"); volume1 = { 90, 60, 90 }; volume2 = volume1; cout << volume2.breast << '/'; cout << volume2.waist << '/'; cout << volume2.hips << endl; return 0; } |
This so-called element-wise or termwise assignment. On the screen:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include<iostream> using namespace std; struct HandsomeMan { char name[16]; int age; int height; bool engKnowledge; }; int main() { setlocale(LC_ALL, "rus"); HandsomeMan Man = {}; cout << "sizeof(Man.name) = " << sizeof(Man.name) << endl; cout << "sizeof(Man.age) = " << sizeof(Man.age) << endl; cout << "sizeof(Man.height) = " << sizeof(Man.height) << endl; cout << "sizeof(Man.engKnowledge) = " << sizeof(Man.engKnowledge) << endl; cout << "Total size of the elements = "; cout << sizeof(Man.name) + sizeof(Man.age) + sizeof(Man.height) + sizeof(Man.engKnowledge); cout << endl << endl; cout << "Size of struct = " << sizeof(Man) << endl; return 0; } |
On the screen:
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.
Only need the structure was called PrettyWoman ;)
strangely, but with the name of the structure in trouble, or rather when the input
volume = { “name”, etc ;
And who taught you this enter?
Where do you see this in the examples?
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.
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;
}
Even again, without any explanation, new terms and lines come out, Even again, without any explanation, new terms and lines come out.