1 Vozshura

Assignment Programming Using Structures And Classes

Structure is a collection of variables of different data types under a single name. It is similar to a class in that, both holds a collecion of data of different data types.

For example: You want to store some information about a person: his/her name, citizenship number and salary. You can easily create different variables to store these information separately.

However, in the future, you would want to store information about multiple persons. Now, you'd need to create different variables for each information per person:

You can easily visualize how big and messy the code would look. Also, since no relation between the variables (information) would exist, it's going to be a daunting task.

A better approach will be to have a collection of all related information under a single name , and use it for every person. Now, the code looks much cleaner, readable and efficient as well.

This collection of all related information under a single name is a structure.

How to declare a structure in C++ programming?

The keyword defines a structure type followed by an identifier (name of the structure).

Then inside the curly braces, you can declare one or more members (declare variables inside curly braces) of that structure. For example:

struct Person { char name[50]; int age; float salary; };

Here a structure is defined which has three members: , and .

When a structure is created, no memory is allocated.

The structure definition is only the blueprint for the creating of variables. You can imagine it as a datatype. When you define an integer as below:

int foo;

The specifies that, variable can hold integer element only. Similarly, structure definition only specifies that, what property a structure variable holds when it is defined.

Note: Remember to end the declaration with a semicolon (;)

How to define a structure variable?

Once you declare a structure as above. You can define a structure variable as:

Person bill;

Here, a structure variable is defined which is of type structure P.

When structure variable is defined, only then the required memory is allocated by the compiler.

Considering you have either 32-bit or 64-bit system, the memory of is 4 bytes, memory of is 4 bytes and memory of is 1 byte.

Hence, 58 bytes of memory is allocated for structure variable .

How to access members of a structure?

The members of structure variable is accessed using a dot (.) operator.

Suppose, you want to access of structure variable and assign it 50 to it. You can perform this task by using following code below:

bill.age = 50;

Example: C++ Structure

C++ Program to assign data to members of a structure variable and display it.


Here a structure P is declared which has three members , and .

Inside function, a structure variable is defined. Then, the user is asked to enter information and data entered by user is displayed.

You should also check out these structure related tutorials:

Did you mean "Complex" as in complex number with real and imaginary parts? This seems unlikely, so if not you'd have to give an example since "complex" means nothing specific in terms of the C language.

You will get a direct memory copy of the structure; whether that is what you want depends on the structure. For example if the structure contains a pointer, both copies will point to the same data. This may or may not be what you want; that is down to your program design.

To perform a 'smart' copy (or a 'deep' copy), you will need to implement a function to perform the copy. This can be very difficult to achieve if the structure itself contains pointers and structures that also contain pointers, and perhaps pointers to such structures (perhaps that's what you mean by "complex"), and it is hard to maintain. The simple solution is to use C++ and implement copy constructors and assignment operators for each structure or class, then each one becomes responsible for its own copy semantics, you can use assignment syntax, and it is more easily maintained.

Leave a Comment


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