12
STRUCTURE IN C ++ 1

STRUCTURE IN C++ · STRUCTURE IN C++ 1. A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: STRUCTURE IN C++ · STRUCTURE IN C++ 1. A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your

STRUCTURE IN C++

1

Page 2: STRUCTURE IN C++ · STRUCTURE IN C++ 1. A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your

A Structure is a collection of variables of different data typesunder one name.

A structure defines a new user defined data type for your currentprogram using which items of different data types can be can beprocessed as a single unit.

Difference between an Array and a Structure

The Array contains elements of a single data type(homogeneous elements) where as a Structure can containelements of different data types (heterogeneous members).

The elements of the Array are contiguous in memory whereasthe elements of a Structure may not be contiguous.

2

Page 3: STRUCTURE IN C++ · STRUCTURE IN C++ 1. A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your

Syntax to define a Structure:

struct structurename

{ datatype membername1;

datatype membername2;...

} ;

For Example:

struct Student

{ int rollno;char name[20];float marks;

} ;

Definition of a structure ( i. e. a new user defined data type ) does not allocate any memory.

Memory is allocated only when we create variables of this new data type. ( For Eg. Bytes consumed by each variable of Student type=26 Bytes)

3

Page 4: STRUCTURE IN C++ · STRUCTURE IN C++ 1. A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your

void main()

{ Student S1, S2 ; // variables of Student type

S1.rollno = 101;

strcpy ( S1.name, “aman” );

S1.marks = 95;

cout << “ Enter details for Student 2”;

cout << “ Enter Student Rollno”;

cin >> S2.rollno ;

cout << “ Enter Student Name”;

gets ( S2.name ) ;

cout << “ Enter Student Marks”;

cin >> S2.marks ;

cout << S1.rollno << endl;

cout << S1.name << endl;

cout << S1.marks << endl;

}4

101

aman

95

S1

Page 5: STRUCTURE IN C++ · STRUCTURE IN C++ 1. A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your

Structure variables can also be declared by clubbing Structure definition and Variable declaration together

struct Student

{ int rollno;char name[20];float marks;

} S1, S2 ; //global variables of Student type

The Structure name/tag can be omitted from the above definition , but the drawback is no more variables of its type can be created later on.

struct

{ int rollno;char name[20];float marks;

} S3 , S4 ; // only variables of this structure type

5

Page 6: STRUCTURE IN C++ · STRUCTURE IN C++ 1. A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your

Initialization of structure members can be done at the time of creation of structure variable

struct date

{

int day;

int month;

int year;

} ;

void main()

{

date d1= {31,12,2016}; // initializing d1

date d2= {15,11,2017}; // initializing d2

}

6

31

12

2016

d1

15

11

2017

d2

Page 7: STRUCTURE IN C++ · STRUCTURE IN C++ 1. A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your

We can assign a variable of a structure as a whole to another variable of the same structure type where in each member of the structure variable receives the corresponding members value from the assigned variable

struct X{int a;float b;} S1;

struct Y{int c;float d;} S2 , S3;

void main(){cin>>S1.a>>S1.b; cin>>S2.c>>S2.d;S1=S2; // Error since different structure typesS3=S2; // valid member by member assignment}

7

2

4

S1

3

6

S2

3

6

S3

Page 8: STRUCTURE IN C++ · STRUCTURE IN C++ 1. A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your

When any member of the structure is a structure variable itself, then it is a nested structure.struct date{ int day;

int month;int year;

} ;struct Student{ int rollno;

char name[20];float marks;date dob; // variable of date data type

} ;void main(){ Student stu1 ;cin >> stu1.rollno ;gets ( stu1.name);cin >> stu1.marks;cin >> stu1.dob.day >> stu1.dob.month >> stu1.dob.year ; Student stu2= { 101, “ravi”, 55.5 , { 10, 08, 2001 } } ; // initialization of stu2}

While Initialization in nested structures, internal braces {} are optional and are ignored by compiler.

8

101

ravi

55.5

10

08

2001

stu2

Page 9: STRUCTURE IN C++ · STRUCTURE IN C++ 1. A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your

struct Student

{ int rollno; char name[20];int marks[5];

} ;void main(){ Student stu ;cin >> stu.rollno ;gets ( stu.name);for( int i=0; i<5 i++)cin >> stu.marks[i];}

9

101

Sumit

90 95 100 85 92

S.marks[0] S.marks[1] S.marks[2] S.marks[3] S.marks[4]

stu

Page 10: STRUCTURE IN C++ · STRUCTURE IN C++ 1. A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your

Structure can be passed to functions by value or by reference. When passed by value the called function creates its own variables of the specified structure type and assigns the actual variables values member by member to it. When passed by reference the formal structure variables are just an alias name of the actual variables./* Program to add two times given in hours and minutes using functions */

struct time{int hh;int mm;};time Add(time,time); //fn prototype

void main(){time t1, t2, t3;cout<<"Enter First Time " <<endl;cout<<" Hours ";cin>>t1.hh;cout<<" Minutes ";cin>>t1.mm;cout<<"Enter Second Time "<<endl;cout<<" Hours " ;cin>>t2.hh;cout<<" Minutes ";cin>>t2.mm;t3 = Add( t1 , t2 ); //fn callcout<<"Total Time is " <<t3.hh<<" Hours : " <<t3.mm<<" Minutes"<<endl;}

10

time Add( time ft, time st){ time tt;tt.hh = ft.hh + st.hh + ( ft.mm + st.mm )/ 60;tt.mm = ( ft.mm + st.mm ) % 60;return tt;}

Page 11: STRUCTURE IN C++ · STRUCTURE IN C++ 1. A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your

#include<iostream.h>struct distance{int feet;int inch;};struct volume{distance length;distance breadth;distance height;};

float cubicfeet( volume v){float l= v.length.feet + (float)v.length.inch/12;float b= v.breadth.feet + (float)v.breadth.inch/12;float h= v.height.feet + (float)v.height.inch/12;return l*b*h;}

11

void main()

{

volume V;

cout<<"Enter Length " <<endl;

cout<<" Feet ";

cin>>V.length.feet;

cout<<" Inches ";

cin>>V.length.inch;

cout<<"Enter Breadth " <<endl;

cout<<" Feet ";

cin>>V.breadth.feet;

cout<<" Inches ";

cin>>V.breadth.inch;

cout<<"Enter Height " <<endl;

cout<<" Feet ";

cin>>V.height.feet;

cout<<" Inches ";

cin>>V.height.inch;

float ans= cubicfeet(V);

cout<< "volume is "<<ans<< " cubic feet";

}

Page 12: STRUCTURE IN C++ · STRUCTURE IN C++ 1. A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your

/* Program to display the Average marks of each student */

#define MAX 3

struct student

{

long rollno;

char name[20];

float marks[5];

};

void Average(student[]); //prototype

void main()

{

student S[MAX];

for ( int i=0 ; i < MAX ; i++ )

{

cout<<"Student "<<(i+1)<<endl;

cout<<"Enter rollno :";

cin>>S[i].rollno;

cout<<"Enter name :";

gets(S[i].name);

for(int j=0; j<5 ;j++)

{ cout<<"Enter marks" <<(j+1) <<" :";

cin>>S[i].marks[j];

}

}

Average(S);

} 12

void Average( student st[])

{

for ( int i=0 ; i < MAX ; i++ )

{ cout<<"--------------------"<<endl;

cout<<"Student "<<(i+1)<<endl;

cout<<"Rollno :"<<st[i].rollno<<endl;

cout<<"Name :"<<st[i].name<<endl;

int sum=0;

for(int j=0; j<5 ;j++)

{ sum+=st[i].marks[j]; }

cout<<"Average Marks :"<<(float)sum/5<<endl;

}

}