cs1123 12 structures

30
Structures (CS1123) By Dr. Muhammad Aleem, Department of Computer Science, Mohammad Ali Jinnah University, Islamabad Fall Semester 2013

Upload: talha-malik

Post on 29-Jun-2015

46 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Cs1123 12 structures

Structures(CS1123)

By

Dr. Muhammad Aleem,

Department of Computer Science, Mohammad Ali Jinnah University, Islamabad

Fall Semester 2013

Page 2: Cs1123 12 structures

Introducing Structures A structure is a collection of multiple data types that

can be referenced with single name.

The data items in structure are called structure members, elements, or fields.

The difference between array and structure: is that array must consists of a set of values of same data type but on the other hand, structure may consists of different data types.

Page 3: Cs1123 12 structures

Defining the Structure

• Structure definition tells how the structure is organized: it specifies what members the structure will contain.

Syntax & Example

Syntax:

struct StructName{

DataType1 Identifier1;DataType2 Identifier2;...

};

Example:

struct Product{

int ID;string name;float price;

};

Page 4: Cs1123 12 structures

Structure Definition Syntax

Page 5: Cs1123 12 structures

Declaring a Structure variable• Structure variable can be defined after the definition of a

structure. The syntax of the declaration is:

StructName Identifier;

Example: part processor; part keyboard;

• This declaration tells the compiler to allocate memory space according to the elements of the structure.

• The structure variable processor and keyboard occupies 12 bytes in the memory. 4 bytes for modelnumber, 4 bytes for partnumber, 4 bytes for cost

Page 6: Cs1123 12 structures

Structure members in memory

struct part{

int modelnumber;

int partnumber;float cost;

};

modelnumber

partnumber

cost

4 Bytes

4 Bytes

4 Bytes

Page 7: Cs1123 12 structures

Another way of Declaring a Structure variable• You can also declare struct variables when you

define the struct. For example:

struct part { int modelnumber; int partnumber;

float cost;} part1;

These statements define the struct named part and also declare part1 to be a variable of type part.

Page 8: Cs1123 12 structures

Examples

struct employee{

string firstName;string lastName;string address;double salary;int deptID;

};

employee e1;

struct student{

string firstName;string lastName;char courseGrade;int Score;

double CGPA;

} s1, s2;

Page 9: Cs1123 12 structures

Initializing Structure Variables• The syntax of initializing structure is:

StructName struct_identifier = {Value1, Value2, …};

Page 10: Cs1123 12 structures

Structure Variable Initialization with Declaration

Note: Values should be written in the same sequence in which they are specified in structure definition.

struct student{ string firstName; string lastName; char courseGrade; int marks;};

void main( ){ student BC022010= {“M”, “Umar”, ‘A’, 94} ;}

Page 11: Cs1123 12 structures

Assigning Values to Structure Variables• After creating structure variable, values to structure

members can be assigned using dot (.) operator

• The syntax is as follows:

student BC111017;

BC111017.firstName = “Muhammad”; BC111017.lastName = “Umar”; BC111017.courseGrade = ‘A’; BC111017.marks = 93;

Page 12: Cs1123 12 structures

Assigning Values to Structure Variables• After creating structure variable, values to structure

members can be assigned using cin.

• Output to screen using cout

student BC012311;

cin>>BC012311.firstName; cin>>BC012311.lastName; cin>>BC012311.courseGrade; cin>>BC012311.marks ;

cout<<BC012311.firstName<<BC012311.lastName;

Page 13: Cs1123 12 structures

Assigning one Structure Variable to another• A structure variable can be assigned to another

structure variable only if both are of same type

• A structure variable can be initialized by assigning another structure variable to it by using the assignment operator as follows:

Example: studentType newStudent = {“John”, “Lee”, ‘A’, 99} ;

studentType student2 = newStudent;

Page 14: Cs1123 12 structures

Array of Structures• An array can also be created of user-defined type such as: structure.

• An array of structure is a type of array in which each element contains a complete structure.

struct Book{ int ID; int Pages; float Price;};

Book MAJULibrary[100]; // declaration of array of structures

MAJULibrary[0]ID Pages Price

…MAJULibrary[1]ID Pages Price

MAJULibrary[99]ID Pages Price

Page 15: Cs1123 12 structures

Initialization of Array of Structuresstruct Book{ int ID; int Pages; float Price;};Book b[3]; // declaration of array of structures

• Initializing can be at the time of declaration

Book b[3] = {{1,275,70},{2,600,90},{3,786,100}};

• Or can be assigned values using cin: cin>>b[0].ID ;

cin>>b[0].Pages; cin>>b[0].Price;

Page 16: Cs1123 12 structures

Array as Member of Structures• A structure may also contain arrays as members.

struct Student {

int RollNo; float Marks[3]; };

• Initialization can be done at time of declaration:

Student S = {1, {70.0, 90.0, 97.0} };

Page 17: Cs1123 12 structures

Array as Member of Structures• Or it can be assigned values later in the program: Student S; S.RollNo = 1; S.Marks[0] = 70.0; S.Marks[1] = 90.0; S.Marks[2] = 97.0;

• Or user can use cin to get input directly:cin>>S.RollNo; cin>>S.Marks[0]; cin>>S.Marks[1]; cin>>S.Marks[2];

Page 18: Cs1123 12 structures

Nested Structure• A structure can be a member of another structure:

called nesting of structurestruct A{ int x; double y;};

struct B {

char ch; A v1;};

B record;

record v1

xch y

Page 19: Cs1123 12 structures

Initializing/Assigning to Nested Structurestruct A{ int x; float y;};

struct B{char ch;A v2;

};

void main(){ B record; cin>>record.ch; cin>>record.v2.x; cin>>record.v2.y;}

void main(){ B record = {‘S’, {100, 3.6} };}

void main(){ B record; record.ch = ‘S’; record.v2.x = 100; record.v2.y = 3.6;}

Page 20: Cs1123 12 structures

Lecture: 06/01/2014

Page 21: Cs1123 12 structures

Accessing Structures with Pointers• Pointer variables can be used to point to structure

type variables too.• The pointer variable should be of same type, for

example: structure type struct Rectangle { int width; int height;};

void main( ){

Rectangle rect1={22,33}; Rectangle* rect1Ptr = &rect1;

}

Page 22: Cs1123 12 structures

Accessing Structures with Pointers• How to access the structure members (using

pointer)? – Use dereferencing operator (*) with dot (.) operator

struct Rectangle { int width; int height;};

void main( ){

Rectangle rect1={22,33}; Rectangle* rectPtr = &rect1; cout<< (*rectPrt).width<<(*rectPrt).height;

}

Page 23: Cs1123 12 structures

Accessing Structures with Pointers• Is there some easier way also?– Use arrow operator ( -> )

struct Rectangle { int width; int height;};

void main( ){

Rectangle rect1={22,33}; Rectangle* rectPtr = &rect1; cout<< rectPrt->width<<rectPrt->height;

}

Page 24: Cs1123 12 structures

Class Exercises(1) – Find Errors

• Find errors:struct {

int x;float y;

};

struct values{

char name[20];int age;

}

Page 25: Cs1123 12 structures

Class Exercises(2) – Find Errors

• Find errors:struct TwoVals{

int a,b;};

void main(){ TwoVals.a=10; TwoVals.b=20;}

Page 26: Cs1123 12 structures

Class Exercises(3) – Find Errors

• Find errors:struct ThreeVals{

int a,b,c;};

int main(){ ThreeVals vals={1,2,3}; cout<<vals<<endl; return 0;}

Page 27: Cs1123 12 structures

Class Exercises(4) – Find Errors• Find errors:

struct names{

char first[20]; char last[20];};

int main(){ names customer = {“Muhammad”, “Ali”}; cout<<names.first<<endl; cout<<names.last<<endl; return 0;}

Page 28: Cs1123 12 structures

Class Exercises(5) – Find Errors• Find errors:

struct TwoVals{

int a=5; int b=10;};

int main(){ TwoVals v; cout<<v.a<<“ “<<v.b; return 0;}

Page 29: Cs1123 12 structures

Class Exercise-6• Define a structure called “car”. The member elements

of the car structure are:• string Model;• int Year;• float Price

Create an array of 30 cars. Get input for all 30 carsfrom the user. Then the program should displaycomplete information (Model, Year, Price) of those cars only which are above 500000 in price.

Page 30: Cs1123 12 structures

Class Exercise-7• Write a program that implements the following using C++

struct. The program should finally displays the values stored in a phone directory (for 10 people)

PhoneNoName

City Country

Address

PhoneDirectory