Anda di halaman 1dari 2

Question2

Assignment C++

Q.2: Explain Structures? How they can be utilized to define user defined data types?

Answer:

STRUCTURES Structure is a collection of variables under a single name. Variables can be of
STRUCTURES
Structure is a collection of variables under a single name. Variables can be of any type: int, float, char
etc. The main difference between structure and array is that arrays are collections of the same data type
and structure is a collection of variables under a single name.
Structures can be utilized to define user defined data types by declaring the structure and
defining structure variables.
As we know that in C++ everything is defined by the user must be declared so that complier
makes sense of it. The declaration provides the blueprint for the compiler, so whenever such
things appear in front if the complier it does not worry about it. To declare an structure the
keyword struct
is used at the starting and the structure name follows. The structure means
is also refereed as the tag. Then in the curly braces the variables with their data types are
declared and these variables are known as the member of the structure. And at the last the
structure is terminated by the semicolon. In this way we can make our own data types and can
use in main. The syntax for the simple structure is as,
Struct home
{
int intialcost;
char direction;
int price;
};

We can use structures by defining structure variables, as in above example when the structure

is declared now it will behave as the new data type i.e. the structure name as home

data type and the variable attached to it will have the data type of type home(having 1 integer and 2 float) to define the structure variable we declare the variables following structure name and you can define as many structure variables you can.

will be the

09CE37

6

Question2

Syntax:

Assignment C++

home citizen, defence, qasimabad, latifabad;

int plot;

As we see above the syntax is same as both are now data type.But home is also a data type but with multiple data types.

int is integer data type but

but with multiple data types. int is integer data type but The definition of the structure

The definition of the structure variable set aside the memory for the variable. The amount of memory depends up on the number and type of the members of the structure. In above structure variables citizen, defence, qasimabad, latifabad will occupy 18 bytes of memory ( 4 bytes of int , 10 bytes of char, 4 bytes of int). In this way we can make our own data type with Structures.

bytes of int , 10 bytes of char, 4 bytes of int). In this way we

09CE37

7