oop lec 3(structures)

25
Structures Lecture No 3 COMSATS Institute of Information & Technology

Upload: asfand-hassan

Post on 20-Jan-2015

218 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Oop lec 3(structures)

StructuresLecture No 3COMSATS Institute of Information & Technology

Page 2: Oop lec 3(structures)

Structures Ordinary variables can hold one piece of

information. Arrays can hold a number of pieces of

information of the same data type. Quite often we deal with entities that are

collection of dissimilar data types. Want to store data about a book. Want to store its name (a string), its price (a

float) and number of pages in it (an int).

2

Object Oriented Programming

Page 3: Oop lec 3(structures)

Structures C and C++ support data structures that

can store combinations of character, integer floating point and enumerated type data. They are called structs.

In general, we can say a structure is a collection of different types of data.

3

Object Oriented Programming

Page 4: Oop lec 3(structures)

‘C++’ implementation of Structure The keyword ‘struct’ is used for creating a

structure. Syntax: struct structure-name { datatype1 varname1; datatype1 varname2; datatype1 varname3; };creating the object of structure: Struct structure-name var1, var2, var3;

4

Object Oriented Programming

Page 5: Oop lec 3(structures)

Declaration struct list { int roll; char name[10]; float marks; }; struct list a , b , c;

It is equivalent to: struct list { int roll; char name[10]; float marks; }a, b, c;

5

Object Oriented Programming

Page 6: Oop lec 3(structures)

Declaring Structures (struct) Reserves Space

struct my_example{

int label;char letter;char name[20];

} mystruct ;

Does Not Reserve Space

struct my_example

{

int label;

char letter;

char name[20];

} ;

/* The name "my_example" is

called a structure tag */

6

Object Oriented Programming

Page 7: Oop lec 3(structures)

Accessing Struct Members Individual members of a struct variable may be

accessed using the structure member operator (the dot, “.” member access operator)

  mystruct.letter ; 

Or , if a pointer to the struct has been declared and initialized

  Some_name *myptr = &mystruct ;  by using the structure pointer operator (the “->“):  myptr -> letter ;   which could also be written as:  (*myptr).letter ;

7

Object Oriented Programming

Page 8: Oop lec 3(structures)

Accessing structure elements . (dot operator) is used to access individual structure element e.g. struct list { int roll; char name[10]; float marks; };

struct list a , b , c; a.roll––is the integer element of structure a. a.name––is char array element of structure a. b.marks––is a float element of structure b. a.marks––is a float element of structure b. scanf( “%d”, &b.roll); this statement can accept an integer roll of

structure from user. This is applied to all the elements of a structure.

8

Object Oriented Programming

Page 9: Oop lec 3(structures)

Things to remember: The closing brace in the structure type declaration must

be followed by a semicolon.

Structure type declaration does not tell the compiler to reserve any space in memory. All a structure declaration does is, it defines the ‘form’ of the structure.

Usually structure type declaration appears at the top of the source code file, before any variables or functions are defined. In very large programs they are usually put in a separate header file, and the file is included (using the preprocessor directive #include) in whichever program we want to use this structure type.

9

Object Oriented Programming

Page 10: Oop lec 3(structures)

How Structure Elements are Stored :

Whatever be the elements of a structure, they are always stored in contiguous memory locations.

/*Memory map of structure elements*/ main() { struct book { char name; float price; int pages; }; struct book b1 = {‘B’, 130.00, 550};

10

Object Oriented Programming

Page 11: Oop lec 3(structures)

Output:

11

Object Oriented Programming

Page 12: Oop lec 3(structures)

Array of Structures: To store data of 100 books we would be

required to use 100 different structure variables from b1 to b100,

A better approach would be to use an array of structures.

12

Object Oriented Programming

Page 13: Oop lec 3(structures)

/* Usage of an array of structures */main( ){struct book

{char name ;float price ;int pages ;} ;

struct book b[100] ;int i ;

Object Oriented Programming

13

Page 14: Oop lec 3(structures)

for ( i = 0 ; i <= 99 ; i++ ){cout << "\nEnter name, price and pages " ;cin >> b[i].name, b[i].price, b[i].pages;}for ( i = 0 ; i <= 99 ; i++ )cout << b[i].name, b[i].price, b[i].pages <<endl;}

Object Oriented Programming

14

Page 15: Oop lec 3(structures)

Passing structure to a function:

15

Object Oriented Programming

Struct Test{ int marks;Char grade; };

void show(Test p);void main(){

Test t;cout <<“Enter marks:”cin>>t.marks;cout <<“Enter grade:”cin>>t.grade;show(t);getch();

}

Page 16: Oop lec 3(structures)

16

Object Oriented Programming

void show(Test p){cout<<“Marks:”<<p.marks<<endl;cout<<“Grade:”<<p.grade<<endl;}

Page 17: Oop lec 3(structures)

Structure Pointers:

The way we can have a pointer pointing to an int, or a pointer pointing to a char, similarly we can have a pointer pointing to a struct.

17

Object Oriented Programming

Page 18: Oop lec 3(structures)

18

Object Oriented Programming

struct Book{char author[30];Int pages;Int price}Void main(){Book rec, *ptr;ptr = &rec;cout<<“Enter author name:”;cin.get(ptr->author, 30);

Page 19: Oop lec 3(structures)

cout<<“Enter pages:”;cin>>ptr->pages;

cout<<“Author:”<<ptr->author<<endl;cout<<“Pages:”<<ptr->pages<<endl;}

Object Oriented Programming

19

Page 20: Oop lec 3(structures)

Structures within Structures struct date

{ int day, month, year;};struct employrec{ char name[20[; char id[20]; float salary; struct date hiredate; };struct employrec employees;

To access the field day of structure type date, employees.hiredate.day = 10;

20

Object Oriented Programming

Page 21: Oop lec 3(structures)

User Defined Data Types (typedef) The C language provides a facility called typedef

for creating synonyms for previously defined data type names. For example, the declaration:

 

typedef int Length;

makes the name Length a synonym (or alias) for the data type int.

The data “type” name Length can now be used in declarations in exactly the same way that the data type int can be used:

Length a, b, len ;Length numbers[10] ;

21

Object Oriented Programming

Page 22: Oop lec 3(structures)

Typedef & Struct Often, typedef is used in combination with struct to

declare a synonym (or an alias) for a structure: 

typedef struct /* Define a structure */{ int label ; char letter; char name[20] ;} Some_name ; /* The "alias" is Some_name */

 

Some_name mystruct ; /* Create a struct variable */

22

Object Oriented Programming

Page 23: Oop lec 3(structures)

Enumeration Enumeration is essentially a method of creating a

numbered list. It allows the user to assign names to numbers which can then be used as indices in an array

Enumeration is a user-defined data type. It is defined using the keyword enum and the syntax is:

enum tag_name {name_0, …, name_n} ;

The tag_name is not used directly. The names in the braces are symbolic constants that take on integer values from zero through n. As an example, the statement:

enum colors { red, yellow, green } ; creates three constants. red is assigned the value 0,

yellow is assigned 1 and green is assigned 2.

23

Object Oriented Programming

Page 24: Oop lec 3(structures)

Enumeration/* This program uses enumerated data types to access the elements of an array */#include <stdio.h>int main( ){

int March[5][7]={{0,0,1,2,3,4,5},{6,7,8,9,10,11,12},{13,14,15,16,17,18,19},{20,21,22,23,24,25,26},{27,28,29,30,31,0,0}};enum days {Sunday, Monday, Tuesday,

Wednesday, Thursday, Friday, Saturday};

24

Object Oriented Programming

Page 25: Oop lec 3(structures)

Enumerationenum week {week_one, week_two, week_three,

week_four, week_five};

printf ("Monday the third week " "of March is March %d\n",

March [week_three] [Monday] ); }

25

Object Oriented Programming