C++ Structures

Need for Structures:


• Arrays can be used to represent a group of data items that belongs to the same type, such as int or float.

• However, if we want to represent a collection of data items of different types using a single name, then we cannot use array.

• C++ supports a user-defined data type known as STRUCTURE.

• A Structure is convenient tool for handing a group of logically related data items.

Structure Declaration:

Declaration of a structure specifies the grouping of various data items into a single unit.

Syntax:

Struct structure name

{

datatype member1; datatype member2;
…..
}
• Structure declaration can be written in a program either above main() or inside main()

• The structure declaration starts with the structure header, which consist of the keyword struct followed by a structure name.

• Structure name can be used for creating structure variables.

• The individual members of the structure are enclosed between the curly braces and

they can be of the same or different data type.

For example:

struct  student

{

int roll_no; char name[25]; char branch[15]; int marks; };

structure name -> student

Structure definition:

Structure definition creates structure variables and allocates storage space for them. Structure variable can be created at the point of declaration itself, or by using the structure name explicitly as and when required.

Syntax:

struct structure name var1,var2….; struct keyword is an optional. Var1,var2 -> structure variables.

Example: struct student s1; (or) student s1;

Memory allocation of the members the structure student: struct student

{

int roll_no; char name[25]; char branch[15]; int marks; };

student s1;

The structure variables can be created during the declaration of a structure as follows:

struct  student

{

int roll_no; char name[25]; char branch[15]; int marks; }s1;

Accessing Structure Members:

C++ provides the period or dot(.) operator to access the members of a structure independently.

Dot operator connects a structure variable and its member.

Syntax: structure variable. member name struct student

{

int roll_no; char name[25]; char branch[15]; int marks; }s1;

Each member of the structure can be accessed using structure variable. S1.name S1.roll_no

S1.branch

S1.marks

Structure Initialization

Similar to the standard data type, structure variables can be initialized at the point of their definition.

Consider the following structure declaration struct student

{

int roll_no; char name[25]; char branch[15]; int marks; };

The members of the structure student can be initialized during variable creation itself.

Student s1={5,”abc”,”computer”,240};

Example: Structure member initialization at the point of definition: #include<iostream.h> //structure declaration struct date

{

int day; int month; int year; }; void main()

{

date d1={14,10,1983}; date d2={03,11,1984}; cout<<”Birthday”; cout<<d1.day<<”-“<<d1.month<<”-“<<d1.year; cout<<d2.day<<”-“<<d2.month<<”-“<<d2.year;

}

Output:

Birthday: 22-07-2016 03-11-1984

Difference between structure and class


S.No.
Structure
Class
1.
Default  access  level  for  its
Default access level for its members

members are public
are public and protected
2.
It can not be inherited
It can be inherited
3.
It contains data members
It   contains   data   members   and


member functions
4.
We cant initialize value to the
We can assign values to variables

variables inside the struct body
inside the class
5.
There is no data hiding
Data is hidden


UNION

Union Declaration:

Declaration of a union specifies the grouping of various data items into a single

unit.

Syntax:

union union name

{

datatype member1; datatype member2;

--------------------

datatype member’n’;

};

union union name var1,var2,…;

The initialization, accessing of union members through union variable are similar to structure.

Eg. Program: union person

{

char name[25]; int age;

float salary; }p1;

void main()

{

cout<<”Enter details of the person”; cin>>p1.name>>p1.age>>p1.salary; cout<<”Name:” << p1.name; cout<<”Age:” << p1.age; cout<<”Salary:” << p1.salary;

}




Difference between structure and Union


S.No.
Structure
Union
1.
Allocates storage space for
Allocates one common storage space for all

all its members separately.
its  members.  Union  finds  that  which  of  its


member needs high storage space over other


members and allocates that much space
2.
We can access all members
We can access only one member of union at

of structure at a time.
a time.
3.
It  occupies  higher  memory
It   occupies   lower   memory   space   over

space.
structure.
4.
Structure example:
Union example:

struct student
union student

{
{

int mark;
int mark;

char name[6];
char name[6];

double average;
double average;

};
};

Previous Post Next Post