structure variables

• 04-30-2011
jackson6612
structure variables
Hi :)

A structure defines a collection of related data (Data could be in different forms, e.g. variables and constants are two types of data. Am I correct?) about something as a new type (I think using "type" is not a good choice here because in itself a structure has no 'type', what do you say?) with an identifier. In other words, a structure, let's say St1, combines two or more kinds of variables under the same umbrella, and then whenever some other variable, x, is declared using St1 x; one needs to assign as much values to x as there are in the St1. A structure is an 'umbrella' variable declaration. Please correct me.

These points should be remembered:
1: The variable, such as x above, declared using a structure cannot be read or written like a normal variable such as int y. e.g. You cannot write: cin >> x, or, cout << x.
2: One structure variable could be assigned to another variable of the same structure. e.g. If x and z both are St1 variables, then it can be written: x = y.
3: The comparison of structure variables cannot be carried out using relational operators like regular variables. e.g. One cannot do: x > z.
4: A structure can be nested within another structure.

Please review the above material and please make corrections if necessary. Thanks a lot.

And if you run or check out the output of the below given code you will notice that the second tab character "\t" produces more space than the first. Notice the distance between "1" and "2", and between "2" and "3".

Sample code:
Code:

```#include <iostream> #include <cstdlib> using namespace std; struct date {     int day; int month; int year; }; int main () {     date D1, D2;     {         cout << "Enter the data for Day No. 1" << endl;         cout << "Enter Day: "; cin >> D1.day;         cout << "Enter Month: "; cin >> D1.month;         cout << "Enter Year: "; cin >> D1.year;     }         cout << " " << endl;     {         cout << "Enter the data for Day No. 2" << endl;         cout << "Enter Day: "; cin >> D2.day;         cout << "Enter Month: "; cin >> D2.month;         cout << "Enter Year: "; cin >> D2.year;     }     cout << " " << endl;     cout << "Day No. 1 Detail: " << D1.day << "\t" << D1.month << "\t" << D1.year << endl;     cout << "Day No. 2 Detail: " << D2.day << "\t" << D2.month << "\t" << D2.year << endl;     system("pause"); }```
Output:
Code:

```Enter the data for Day No. 1 Enter Day: 1 Enter Month: 2 Enter Year: 3 Enter the data for Day No. 2 Enter Day: 4 Enter Month: 5 Enter Year: 6 Day No. 1 Detail: 1    2      3 Day No. 2 Detail: 4    5      6 Press any key to continue . . .```
• 04-30-2011
Salem
> 3: The comparison of structure variables cannot be carried out using relational operators like regular variables. e.g. One cannot do: x > z.
They can in C++, if you provide an implementation for the appropriate operators.

Also, in C++, this class is the same as a struct
Code:

```class foo {   public: };```
And this struct is the same as a class
Code:

```struct foo {   private: };```
The only difference is the default visibility of members.

structs, classes, unions and arrays are collectively called aggregate types
• 05-01-2011
Elysia
Quote:

Originally Posted by jackson6612
A structure defines a collection of related data (Data could be in different forms, e.g. variables and constants are two types of data. Am I correct?) about something as a new type (I think using "type" is not a good choice here because in itself a structure has no 'type', what do you say?) with an identifier. In other words, a structure, let's say St1, combines two or more kinds of variables under the same umbrella, and then whenever some other variable, x, is declared using St1 x; one needs to assign as much values to x as there are in the St1. A structure is an 'umbrella' variable declaration. Please correct me.

A struct creates a new type, because you can only create new variables by giving them a type.

Quote:

1: The variable, such as x above, declared using a structure cannot be read or written like a normal variable such as int y. e.g. You cannot write: cin >> x, or, cout << x.
3: The comparison of structure variables cannot be carried out using relational operators like regular variables. e.g. One cannot do: x > z.
Incorrect. You can do this provided you tell the compiler how to do it. The compiler knows nothing of your structure, so it cannot know how to perform those operations.
But you can tell the compiler how to do it by overloading operators. Then it works fine.

Quote:

2: One structure variable could be assigned to another variable of the same structure. e.g. If x and z both are St1 variables, then it can be written: x = y.
4: A structure can be nested within another structure.
Correct.
• 05-03-2011
jackson6612
Thanks a lot, Elysia. You are so nice.

Best wishes
Jackson