Docsity

Structures in Object-Oriented Programming using C++: Dynamic Allocation and Linked Lists, Slides of Object Oriented Programming

Partial preview of the text

Download Structures in Object-Oriented Programming using C++: Dynamic Allocation and Linked Lists and more Slides Object Oriented Programming in PDF only on Docsity!

Object Oriented

Programming using C++

Lecture – 4

More on Structures

Structures

„ We need structures to organize/store data related

to a single entity in one place

„ We need structures to create user defined data

types

E.g. I am recognized by: First Name Last Name NIC / driving license Number My height etc.

First Name Last Name NIC Number Height

Usman Younis

„ So far we have learned

definition of structures

when the memory is

allocated statically

void main()

Record person1;

E.g., struct NIC { int field1; int field2; int field3; };

struct Record { char first_name[10]; char last_name[10]; NIC Identification_number; int height; };

Structures (contd..)

„ We can also create arrays of structures

E.g.,

Record village[500];

//An array to store data of 500 people of a village

„ However, this might be a limitation, when we don’t know how many people are there in a village

‰ Could be less than 500, or more

Usman Younis

int temp_value = 0;

while(option != ‘n’) //Initialize the List { cout<<“Enter new item value”<<endl; cin>>temp_value; List* newLink = new List;

newLink->value = temp_value; newLink->next = FIRST;

FIRST = newLink; cout<<“Add another (y/n)? ”; cin>>option; }

Structures (contd..)

List* Temp = FIRST;

while(Temp != NULL) //Display the List { cout<<“Item’s value is”<<Temp->value<<endl; Temp = Temp->next; }

Temp = FIRST;

while(Temp != NULL) //Free the Memory { FIRST = Temp->next; delete Temp; Temp = FIRST; } }