CS201- Introduction to Programming- Lecture 20

49
Introduction to Programmin Introduction to Programmin Lecture 20 Lecture 20

description

Virtual University Course CS201- Introduction to Programming Lecture No 20 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Transcript of CS201- Introduction to Programming- Lecture 20

Page 1: CS201- Introduction to Programming- Lecture 20

Introduction to ProgrammingIntroduction to Programming

Lecture 20Lecture 20

Page 2: CS201- Introduction to Programming- Lecture 20

In the Last Two In the Last Two LecturesLectures

File HandlingFile Handling– Sequential FilesSequential Files

– Random Access FilesRandom Access Files

Page 3: CS201- Introduction to Programming- Lecture 20

Today’s LectureToday’s Lecture

StructuresStructures UnionsUnions

Page 4: CS201- Introduction to Programming- Lecture 20

StructureStructure

??

Page 5: CS201- Introduction to Programming- Lecture 20

Structure Structure definitiondefinition

struct Studentstruct Student

{{

char name [ 60 ] ;char name [ 60 ] ;char address [ 100 ] ; char address [ 100 ] ; Data of the structure Data of the structure

double gpa ;double gpa ;

} ;} ;

Structure name

}Keyword

Page 6: CS201- Introduction to Programming- Lecture 20

Student s1 , s2 , s3 ;Student s1 , s2 , s3 ;

Structure Name Variable Names}

Page 7: CS201- Introduction to Programming- Lecture 20

StructureStructurestruct addressstruct address{{

char streetAddress char streetAddress [ 100 ] ;[ 100 ] ;char city [ 30 ] ;char city [ 30 ] ;char country [ 30 ] ;char country [ 30 ] ;

}}

Page 8: CS201- Introduction to Programming- Lecture 20

StructureStructurestruct Studentstruct Student

{{

char name [ 60 ] ;char name [ 60 ] ;

address add ;address add ;

double gpa ;double gpa ;

} s1 , s2 , s3 ;} s1 , s2 , s3 ;

Page 9: CS201- Introduction to Programming- Lecture 20

Example 1Example 1struct Cardstruct Card

{{

char *suit ;char *suit ;

char *value ;char *value ;

} ;} ;

Page 10: CS201- Introduction to Programming- Lecture 20

StructuresStructures

Variables of type structureVariables of type structure

Pointers to StructurePointers to Structure

Array of StructureArray of Structure

Page 11: CS201- Introduction to Programming- Lecture 20

Student s [ 100 ] ;Student s [ 100 ] ;Student *sPtr ;Student *sPtr ;

StructuresStructures

Page 12: CS201- Introduction to Programming- Lecture 20

Student stdnt1 , stdnt2 ;Student stdnt1 , stdnt2 ;

stdnt1 + stdnt2 ; stdnt1 + stdnt2 ; //Wrong//Wrong

StructuresStructures

Page 13: CS201- Introduction to Programming- Lecture 20

Student s1 , s2 ;Student s1 , s2 ;

s1 = s2 ;s1 = s2 ;

StructuresStructures

Page 14: CS201- Introduction to Programming- Lecture 20

Example 2Example 2struct Studentstruct Student{{

char name [ 64 ] ;char name [ 64 ] ;char course [ 128 ] ;char course [ 128 ] ;int age ;int age ;int year ;int year ;

} ;} ;

Page 15: CS201- Introduction to Programming- Lecture 20

Initializing Initializing StructuresStructures

Student s1 = { “Amara”,“cs201”,“19”, Student s1 = { “Amara”,“cs201”,“19”, “2002” } ;“2002” } ;

Name

Course name

ageyear

Page 16: CS201- Introduction to Programming- Lecture 20

Initializing Initializing StructuresStructures

s1.name ;s1.name ;

s1.course ;s1.course ;

s1.age ;s1.age ;

s1.year ;s1.year ;

Page 17: CS201- Introduction to Programming- Lecture 20

Initializing Initializing StructuresStructures

s1.age = 20 ;s1.age = 20 ;

s1.name = “Abbas Ali ” ; s1.name = “Abbas Ali ” ; //Wrong//Wrong

strcpy ( s1.name , “Abbas Ali ” ) ;strcpy ( s1.name , “Abbas Ali ” ) ;

Page 18: CS201- Introduction to Programming- Lecture 20

Accessing structure Accessing structure membersmembers

cout << s1.name ;cout << s1.name ;

cout << s1.course ;cout << s1.course ;

cout << s1.age ;cout << s1.age ;

cout << s1.year ;cout << s1.year ;

Page 19: CS201- Introduction to Programming- Lecture 20

Assignment of Assignment of structuresstructures

Student s1 , s2 ;Student s1 , s2 ;

s2 = s1 ;s2 = s1 ;

Page 20: CS201- Introduction to Programming- Lecture 20

Passing Structures Passing Structures to Functionsto Functions

Page 21: CS201- Introduction to Programming- Lecture 20

Call by valueCall by value

Call by Reference Call by Reference XX

Passing Structures to Functions

Page 22: CS201- Introduction to Programming- Lecture 20

int Square ( int int Square ( int x ) ;x ) ;

Page 23: CS201- Introduction to Programming- Lecture 20

x = Square ( 10 ) ;x = Square ( 10 ) ;

Page 24: CS201- Introduction to Programming- Lecture 20

StructuresStructures Simple Variable of type Simple Variable of type

StructureStructure Pointer to StructurePointer to Structure Arrays of StructuresArrays of Structures Function that returns a Function that returns a

StructureStructure Pass the Structure to functionsPass the Structure to functions

Page 25: CS201- Introduction to Programming- Lecture 20

Pointers to Pointers to StructureStructure

Page 26: CS201- Introduction to Programming- Lecture 20

Pointers to Pointers to StructureStructure

Student *sPtr , s1 ;Student *sPtr , s1 ;

sPtr = &s1 ;sPtr = &s1 ;

Page 27: CS201- Introduction to Programming- Lecture 20

Pointers to Pointers to StructureStructure

sPtrsPtr.name ;.name ;

WrongWrong

Page 28: CS201- Introduction to Programming- Lecture 20

**sPtrsPtr.name ;.name ;

Pointers to Pointers to StructureStructure

Page 29: CS201- Introduction to Programming- Lecture 20

*( sPtr ).name ;*( sPtr ).name ;

Pointers to StructurePointers to Structure

Page 30: CS201- Introduction to Programming- Lecture 20

*sPtr *sPtr ->name ;->name ;

Same asSame as

s1.names1.name

Pointers to StructurePointers to Structure

Page 31: CS201- Introduction to Programming- Lecture 20

Arrays of StructuresArrays of Structures

Page 32: CS201- Introduction to Programming- Lecture 20

Student s [ 100 ] ;Student s [ 100 ] ;s [ 0 ].name ;s [ 0 ].name ;s [ 1 ].name ; s [ 1 ].name ; s [ 2 ].name ;s [ 2 ].name ;

..

..

..s [ 99 ].name ;s [ 99 ].name ;

Arrays of Arrays of StructureStructure

Page 33: CS201- Introduction to Programming- Lecture 20

sizeof ( s1 ) ;sizeof ( s1 ) ;

Page 34: CS201- Introduction to Programming- Lecture 20

Example 3Example 3struct Studentstruct Student{{

char firstName [ 30 ] ;char firstName [ 30 ] ;char lastName [ 30 ] ;char lastName [ 30 ] ;char course [ 15 ] ;char course [ 15 ] ;char rollNo [ 10 ] ;char rollNo [ 10 ] ;int age ;int age ;float gpa ;float gpa ;

} ;} ;

Page 35: CS201- Introduction to Programming- Lecture 20

Student s [ 10 ] ; Student s [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ )for ( int i = 0 ; i < 10 ; i ++ ){{

cout << “Please enter the student's last name : " ;cout << “Please enter the student's last name : " ; cin >> s [ i ].lastName ;cin >> s [ i ].lastName ; cout << “Please enter the student's first name : cout << “Please enter the student's first name :

" ;" ; cin >> s [ i ].firstName ;cin >> s [ i ].firstName ; cout << " Please enter the student's course : " ;cout << " Please enter the student's course : " ; cin >> s [ i ].course ;cin >> s [ i ].course ; cout << " Please enter the student's Roll No. : " ;cout << " Please enter the student's Roll No. : " ; cin >> s [ i ].rollNo ;cin >> s [ i ].rollNo ; cout << " Please enter the student's grade : " ;cout << " Please enter the student's grade : " ; cin >> s [ i ].grade ;cin >> s [ i ].grade ; cout << " Please enter the student's age : " ;cout << " Please enter the student's age : " ; cin >> s [ i ].age ;cin >> s [ i ].age ; cout << " Please enter the student's GPA : " ;cout << " Please enter the student's GPA : " ; cin >> s [ i ].gpa ;cin >> s [ i ].gpa ;}}

Example 3

Page 36: CS201- Introduction to Programming- Lecture 20

Student getData ( ) Student getData ( ) ;;

Return Type

Function Name

Example 4

Page 37: CS201- Introduction to Programming- Lecture 20

Example 4Example 4ifstream inFile ifstream inFile

( “myText.txt” ) ;( “myText.txt” ) ;

void main ( )void main ( )

{{

Student s1 ;Student s1 ;

s1 = getData ( ) ;s1 = getData ( ) ;

inFile.close ( ) ;inFile.close ( ) ;

}}

Page 38: CS201- Introduction to Programming- Lecture 20

Example 4Example 4Student getData ( )Student getData ( )

{{

Student tempStudent ;Student tempStudent ;

inFile >> tempStudent.firstName ;inFile >> tempStudent.firstName ;

inFile >> tempStudent.lastName ;inFile >> tempStudent.lastName ;

inFile >> tempStudent.course ;inFile >> tempStudent.course ;

inFile >> tempStudent.rollNo ;inFile >> tempStudent.rollNo ;

inFile >> tempStudent.age ;inFile >> tempStudent.age ;

inFile >> tempStudent.gpa ;inFile >> tempStudent.gpa ;

return tempStudent ;return tempStudent ;

}}

Page 39: CS201- Introduction to Programming- Lecture 20

UnionUnion

Page 40: CS201- Introduction to Programming- Lecture 20

UnionUnionunion intOrCharunion intOrChar

{{

int i ;int i ;

char c ;char c ;

} ;} ;

Page 41: CS201- Introduction to Programming- Lecture 20

UnionUnion

C

iBoth Variable occupy the same space in memory

Memory

Page 42: CS201- Introduction to Programming- Lecture 20

UnionUnionunion intOrDoubleunion intOrDouble

{{

int ival ;int ival ;

double dval ;double dval ;

} ;} ;

Page 43: CS201- Introduction to Programming- Lecture 20

UnionUnion

iVal

dValBoth Variable occupy the same space in memory

Memory

Page 44: CS201- Introduction to Programming- Lecture 20

UnionUnion

intOrDoubleintOrDouble u1 ; u1 ;

u1.ival = 10 ;u1.ival = 10 ;

cout << u1.ival ;cout << u1.ival ;cout << u1.dval cout << u1.dval ; ; incorrect incorrect

valuevalue

Page 45: CS201- Introduction to Programming- Lecture 20

UnionUnion

u1.dval = 100.0 ;u1.dval = 100.0 ;cout << u1.ival ; cout << u1.ival ; incorrect incorrect

valuevalue

Page 46: CS201- Introduction to Programming- Lecture 20

Example 5Example 5char c ;char c ;int x ;int x ;x = ‘a’ ;x = ‘a’ ;cout << x ;cout << x ;x = x * 256x = x * 256cout << x cout << x ;;

::

Page 47: CS201- Introduction to Programming- Lecture 20

Example 5Example 5 x = x +’b’ ;x = x +’b’ ; cout << x ;cout << x ;

Page 48: CS201- Introduction to Programming- Lecture 20

Example 5Example 5

Union iandcUnion iandc

{{

char c [ 4 ] ;char c [ 4 ] ;

int x ;int x ;

} ;} ;

Page 49: CS201- Introduction to Programming- Lecture 20

Today we Today we studiedstudied

StructuresStructures UnionsUnions