Upload
bilal-ahmed
View
10
Download
2
Tags:
Embed Size (px)
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 20 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]
Citation preview
Introduction to ProgrammingIntroduction to Programming
Lecture 20Lecture 20
In the Last Two In the Last Two LecturesLectures
File HandlingFile Handling– Sequential FilesSequential Files
– Random Access FilesRandom Access Files
Today’s LectureToday’s Lecture
StructuresStructures UnionsUnions
StructureStructure
??
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
Student s1 , s2 , s3 ;Student s1 , s2 , s3 ;
Structure Name Variable Names}
StructureStructurestruct addressstruct address{{
char streetAddress char streetAddress [ 100 ] ;[ 100 ] ;char city [ 30 ] ;char city [ 30 ] ;char country [ 30 ] ;char country [ 30 ] ;
}}
StructureStructurestruct Studentstruct Student
{{
char name [ 60 ] ;char name [ 60 ] ;
address add ;address add ;
double gpa ;double gpa ;
} s1 , s2 , s3 ;} s1 , s2 , s3 ;
Example 1Example 1struct Cardstruct Card
{{
char *suit ;char *suit ;
char *value ;char *value ;
} ;} ;
StructuresStructures
Variables of type structureVariables of type structure
Pointers to StructurePointers to Structure
Array of StructureArray of Structure
Student s [ 100 ] ;Student s [ 100 ] ;Student *sPtr ;Student *sPtr ;
StructuresStructures
Student stdnt1 , stdnt2 ;Student stdnt1 , stdnt2 ;
stdnt1 + stdnt2 ; stdnt1 + stdnt2 ; //Wrong//Wrong
StructuresStructures
Student s1 , s2 ;Student s1 , s2 ;
s1 = s2 ;s1 = s2 ;
StructuresStructures
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 ;
} ;} ;
Initializing Initializing StructuresStructures
Student s1 = { “Amara”,“cs201”,“19”, Student s1 = { “Amara”,“cs201”,“19”, “2002” } ;“2002” } ;
Name
Course name
ageyear
Initializing Initializing StructuresStructures
s1.name ;s1.name ;
s1.course ;s1.course ;
s1.age ;s1.age ;
s1.year ;s1.year ;
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 ” ) ;
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 ;
Assignment of Assignment of structuresstructures
Student s1 , s2 ;Student s1 , s2 ;
s2 = s1 ;s2 = s1 ;
Passing Structures Passing Structures to Functionsto Functions
Call by valueCall by value
Call by Reference Call by Reference XX
Passing Structures to Functions
int Square ( int int Square ( int x ) ;x ) ;
x = Square ( 10 ) ;x = Square ( 10 ) ;
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
Pointers to Pointers to StructureStructure
Pointers to Pointers to StructureStructure
Student *sPtr , s1 ;Student *sPtr , s1 ;
sPtr = &s1 ;sPtr = &s1 ;
Pointers to Pointers to StructureStructure
sPtrsPtr.name ;.name ;
WrongWrong
**sPtrsPtr.name ;.name ;
Pointers to Pointers to StructureStructure
*( sPtr ).name ;*( sPtr ).name ;
Pointers to StructurePointers to Structure
*sPtr *sPtr ->name ;->name ;
Same asSame as
s1.names1.name
Pointers to StructurePointers to Structure
Arrays of StructuresArrays of Structures
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
sizeof ( s1 ) ;sizeof ( s1 ) ;
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 ;
} ;} ;
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
Student getData ( ) Student getData ( ) ;;
Return Type
Function Name
Example 4
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 ( ) ;
}}
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 ;
}}
UnionUnion
UnionUnionunion intOrCharunion intOrChar
{{
int i ;int i ;
char c ;char c ;
} ;} ;
UnionUnion
C
iBoth Variable occupy the same space in memory
Memory
UnionUnionunion intOrDoubleunion intOrDouble
{{
int ival ;int ival ;
double dval ;double dval ;
} ;} ;
UnionUnion
iVal
dValBoth Variable occupy the same space in memory
Memory
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
UnionUnion
u1.dval = 100.0 ;u1.dval = 100.0 ;cout << u1.ival ; cout << u1.ival ; incorrect incorrect
valuevalue
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 ;;
::
Example 5Example 5 x = x +’b’ ;x = x +’b’ ; cout << x ;cout << x ;
Example 5Example 5
Union iandcUnion iandc
{{
char c [ 4 ] ;char c [ 4 ] ;
int x ;int x ;
} ;} ;
Today we Today we studiedstudied
StructuresStructures UnionsUnions