Structures

75 %
25 %
Information about Structures

Published on February 5, 2009

Author: archikabhatia

Source: slideshare.net

Description

Use of structures in C++ programming

STRUCTURES User defined derived data types: Class Structure Union Enumeration Typedef

User defined derived data types:

Class

Structure

Union

Enumeration

Typedef

It is a collection of dissimilar variables referenced under one name. For eg: to store a student’s information (i.e. name, roll no, class, marks, grade ) or to store date’s information (i.e day, month, year ) : structures come to help. Syntax: struct tag { type var_name; type var_name; type var_name; } structure-variables; STRUCTURES

It is a collection of dissimilar variables referenced under one name.

For eg: to store a student’s information (i.e. name, roll no, class, marks, grade ) or to store date’s information (i.e day, month, year ) : structures come to help.

Syntax: struct tag { type var_name; type var_name; type var_name; } structure-variables;

Defining a structure and declaring a structure Defining a structure struct stutype { short roll no; short class; float marks; char grade; } Declaring a structure stutype senior_student, junior_student;

Defining a structure

struct stutype

{

short roll no;

short class;

float marks;

char grade;

}

Declaring a structure

stutype senior_student, junior_student;

Defining a structure and Declaring a structure struct stutype { short roll_no; short class; float marks; char grade; } senior_student, junior_student;

Defining a structure and Declaring a structure

struct stutype

{

short roll_no;

short class;

float marks;

char grade;

} senior_student, junior_student;

Referencing of individual structure elements using dot operator senior_student.roll_no; senior_student.class; senior_student.marks; senior_students.grade; junior_student.roll_no; junior_student.class; junior_student.marks; junior_students.grade;

senior_student.roll_no;

senior_student.class;

senior_student.marks;

senior_students.grade;

junior_student.roll_no;

junior_student.class;

junior_student.marks;

junior_students.grade;

STORAGE OF STRUCTURE ELEMENTS IN MEMORY Senior_Student marks class roll_no grade Total memory requirement for the variable senior_student of the type stutype is : 2+2+4+1 = 9 bytes

INITIALISING STRUCTURE ELEMENTS senior_student.roll_no=1; senior_student.class=11; senior_student.marks=70; senior_students.grade=‘a’; OR stutype senior_student = { 01, 11, 70, ‘a’};

senior_student.roll_no=1;

senior_student.class=11;

senior_student.marks=70;

senior_students.grade=‘a’;

OR

stutype senior_student = { 01, 11, 70, ‘a’};

Structure Assignments Note: structure assignments are possible only if both the structures are of same structure. struct one { int a; } struct two { int a; } one s1,s3; two s2; cin>>s1.a; s2=s1; // incorrect b/c though s1 and s2 have similar elements but are of different type ‘one’ and ‘two’ s3=s1; // correct

Note: structure assignments are possible only if both the structures are of same structure.

struct one { int a; }

struct two { int a; }

one s1,s3;

two s2;

cin>>s1.a;

s2=s1; // incorrect b/c though s1 and s2 have similar elements but are of different type ‘one’ and ‘two’

s3=s1; // correct

Nested Structures and Containership Containership e.g. struct addr { int houseno; char area[26]; char city[26]; char state[26]; }; struct emp { int empno; char name[26]; char desig [16]; addr address; float basic; } worker;

Containership

e.g.

struct addr

{

int houseno;

char area[26];

char city[26];

char state[26];

};

struct emp

{

int empno;

char name[26];

char desig [16];

addr address;

float basic;

} worker;

Structure within a Structure ( nested structure ) struct stu { int admn_no; int roll_no; char name[30]; int class; float marks[5]; struct add { int house_no; char city[20]; } address; } nur_stu[100], jr_stu[100], sr_stu[100];

struct stu

{

int admn_no;

int roll_no;

char name[30];

int class;

float marks[5];

struct add

{

int house_no;

char city[20];

} address;

} nur_stu[100], jr_stu[100], sr_stu[100];

Accessing nested structure elements worker.empno=123; worker.address.houseno =12 worker.address.area=“rohini”; Etc. Note: Dot operator can be used more than once in a single statement to approach the nested elements.

worker.empno=123;

worker.address.houseno =12

worker.address.area=“rohini”;

Etc.

Note: Dot operator can be used more than once in a single statement to approach the nested elements.

Structures and Arrays Arrays within a structure Array of a structure

Arrays within a structure

Array of a structure

Arrays within structures struct student { int roll_no; char name[21]; float marks[5]; } learner; To access the marks of 1 st subject of the learner we need to use: learner.marks[0]

struct student

{

int roll_no;

char name[21];

float marks[5];

} learner;

To access the marks of 1 st subject of the learner we need to use:

learner.marks[0]

Need for structure is explained along with Structures and Arrays For eg. To store details of students (admn_no, name, roll_no, address, marks) where there are 100 nursery students, 100 junior students and 100 senior students. Approach 1: nur_stu_admn_no[100]; jr_stu_admn_no[100]; nur_stu_name[100]; jr_stu_name[100]; nur_stu_roll_no[100]; and so on total 5 * 3 = 15 array nur_stu_address[100]; type variables will be created. nur_stu_marks[100];

For eg. To store details of students (admn_no, name, roll_no, address, marks) where there are 100 nursery students, 100 junior students and 100 senior students.

Approach 1:

nur_stu_admn_no[100]; jr_stu_admn_no[100];

nur_stu_name[100]; jr_stu_name[100];

nur_stu_roll_no[100]; and so on total 5 * 3 = 15 array

nur_stu_address[100]; type variables will be created.

nur_stu_marks[100];

Approach 2 using structure struct stu { int admn_no; int roll_no; char name[30]; int class; float marks[5]; struct add { int house_no; char city[20]; } address; } nur_stu[100], jr_stu[100], sr_stu[100]; Note: The same problem can be solved using 3 variables.

struct stu

{

int admn_no;

int roll_no;

char name[30];

int class;

float marks[5];

struct add

{

int house_no;

char city[20];

} address;

} nur_stu[100], jr_stu[100], sr_stu[100];

Passing structures to functions There are two ways: Passing individual structure elements Passing the entire structure

There are two ways:

Passing individual structure elements

Passing the entire structure

Passing structure elements to functions E.g. struct date { short day; short month; short year; } bdate; Func(bdate.day, b.date.month, bdate.year); // fn call Can be called like normal variables either using calll by value or call by reference.

E.g.

struct date

{

short day;

short month;

short year;

} bdate;

Func(bdate.day, b.date.month, bdate.year); // fn call

Can be called like normal variables either using calll by value or call by reference.

Passing entire structure to functions WAP to input the details of students (admn_no, name, roll_no, address, marks) where there are 100 nursery students, 100 junior students and 100 senior students. Create a function to input the details.

WAP to input the details of students (admn_no, name, roll_no, address, marks) where there are 100 nursery students, 100 junior students and 100 senior students. Create a function to input the details.

Passing structure by reference WAP to input distance in feet and inches. Compute the sum of the two distances.

WAP to input distance in feet and inches. Compute the sum of the two distances.

Returning structure from functions WAP to input distance in feet and inches. Compute the sum of the two distances and return the sum.

WAP to input distance in feet and inches. Compute the sum of the two distances and return the sum.

Reference It is a derived data type Provides an alternative name to a variable E.g int total; int &sum=total; total = 100; cout<<“sum = “<<sum; cout<<“total = “<<total; Output will be 100100

It is a derived data type

Provides an alternative name to a variable

E.g int total; int &sum=total; total = 100; cout<<“sum = “<<sum; cout<<“total = “<<total;

Output will be 100100

typedef typedef does not create a new data type rather provides an alternative name for the standard types. reference provides an alias name for a variable and typedef provides an alias name for a data type. E.g. typedef float amount ; // new name for flaot has been created by typedef amount loan, saving, installment ; // variable of amount type (i.e. float type) are being created.

typedef does not create a new data type rather provides an alternative name for the standard types.

reference provides an alias name for a variable and typedef provides an alias name for a data type.

E.g. typedef float amount ; // new name for flaot has been created by typedef

amount loan, saving, installment ; // variable of amount type (i.e. float type) are being created.

#define preprocessor directive Begins with a pound/hash symbol ( # ) Preprocessing phase occurs before a program is compiled Allows us to define symbolic names and constants E.g. # include<iostream.h> # define PI = 3.14159 void main() { int r = 10; float cir; cir= PI = ( r*r); cout<<“Area of circle : “<<cir; } # define name= “Computer Science C++”

Begins with a pound/hash symbol ( # )

Preprocessing phase occurs before a program is compiled

Allows us to define symbolic names and constants

E.g. # include<iostream.h>

# define PI = 3.14159

void main()

{

int r = 10;

float cir;

cir= PI = ( r*r);

cout<<“Area of circle : “<<cir;

}

# define name= “Computer Science C++”

MACROS #define can be used to create macros . Macros are expressions . After the preprocessing the macro is replaced by the text. The text replacement for a macro is known as macro expansion . E.g. #include<iostream.h> #define square(x) x*x void main() { int value = 3; cout<<square(value); }

#define can be used to create macros .

Macros are expressions .

After the preprocessing the macro is replaced by the text.

The text replacement for a macro is known as macro expansion .

E.g. #include<iostream.h>

#define square(x) x*x

void main()

{

int value = 3;

cout<<square(value);

}

While defining macros, make sure to use parenthesis to get correct results E.g. # define PI = 3.14159 #define circle_area(x) PI * X *X and You are using it as: area= circle_area(c+2); area = 3.14159 * c + 2 * c + 2 To get correct results: # define circle_area ( (x) ) PI * ( x ) * ( x )

While defining macros, make sure to use parenthesis to get correct results

E.g. # define PI = 3.14159

#define circle_area(x) PI * X *X and

You are using it as: area= circle_area(c+2);

area = 3.14159 * c + 2 * c + 2

To get correct results:

# define circle_area ( (x) ) PI * ( x ) * ( x )

You can also define macros with multiple arguments E.g. # define rectangle_area ( a, b) ( (a) * (b)) rectarea = rectangle_area(x+4, y+7) would expand to: rectarea= ( (x+4) * (y+7) );

You can also define macros with multiple arguments

E.g. # define rectangle_area ( a, b) ( (a) * (b))

rectarea = rectangle_area(x+4, y+7) would expand to:

rectarea= ( (x+4) * (y+7) );

Add a comment

Related presentations

Related pages

Structure - Wikipedia, the free encyclopedia

Structure is an arrangement and organization of interrelated elements ... Structures are applicable to people in how a society is as a system organized by ...
Read more

Pogo - Chantier Naval Structures

Le chantier Structures. Notre concept; Historique; Nos voiliers de croisière. Pogo 30; Pogo 12,50; Pogo 50; Les premiers modèles. Pogo 8,50; Pogo 10,50 ...
Read more

Structures: Building Activities for Kids | Exploratorium

Structures Around the World: Hands-on activites investigating scale and structure. Teacher tested for the elementary classroom. Activities available in ...
Read more

Structures – Wikipedia

Structures (oftmals auch Struc/tures) ist eine Metal-Band aus dem kanadischen Toronto, welche Metalcore mit Elementen des Progressive Metals verbindet.
Read more

Structures - definition of structures by The Free Dictionary

It struck him, as he gazed at the admirable structures and the wonderful precautions of their sagacious inmates, that even the brutes of these vast wilds ...
Read more

it-structures Lexware Gold Partner in Berlinit-structures ...

it-structures Lexware Gold Partner in Berlin. Spezialist für Lexware. Microsoft Spezialist für Netzwerke und Client-Server der It-Dienstleister in Berlin
Read more

Chantier naval STRUCTURES, constructeur des voiliers POGO ...

Structures. Our concept; History; Cruising sailboats. Pogo 30; Pogo 12,50; Pogo 50; The early designs. Pogo 8,50; Pogo 10,50; Racing sailboats. Pogo 3 ...
Read more

Structures - Journal - Elsevier

Research Journal of The Institution of Structural Engineers. Structures aims to publish internationally-leading research across the full breadth of ...
Read more

Structurae: International Database and Gallery of Structures

Structurae is an international database and gallery for bridge engineering, structural engineering and civil engineering including individual structures ...
Read more

Structurae - Internationale Datenbank für Ingenieurbauwerke

Structurae ist eine internationale Datenbank und Bildergalerie für Ingenieurbauwerke und konstruktiv anspruchsvolle Architektur. Neben den einzelnen ...
Read more