Docsity

Understanding Structures in Object-Oriented Programming using C++, Slides of Object Oriented Programming

Toggle sidebar
bg1
2/15/2011
1
Object Oriented
Programming using C++
Lecture โ€“ 3
Structures
Why Structures
๎˜Data types, e.g., float, int, char, etc., are used to
store single piece of information or value in a variable
๎˜However; in reality we need the data to be organized
in sets or a piece of collection
๎˜‚E.g.
I am recognized by:
First Name
Last Name
NIC / driving license Number
My height
etc.
๎˜‚All this information represents an entity, i.e., me, and it needs
to be recorded as a set
First Name Last Name
NIC Number
Height
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Understanding Structures in Object-Oriented Programming using C++ and more Slides Object Oriented Programming in PDF only on Docsity!

Object Oriented

Programming using C++

Lecture โ€“ 3

Structures

Why Structures

 Data types, e.g., float, int, char, etc., are used to

store single piece of information or value in a variable

 However; in reality we need the data to be organized

in sets or a piece of collection

 E.g. I am recognized by: First Name Last Name NIC / driving license Number My height etc.  All this information represents an entity, i.e., me, and it needs to be recorded as a set

First Name Last Name NIC Number Height

Structure design

 Different data-types are used to identify individual

item

 String or char[] for first name  String or char[] for last name  int can be used for NIC ??  Or you can make a sub-structure with three fields to identify each section of your NIC  int for height

Its all about how you design it!

First Name Last Name NIC Number Height

This group or record is called a

structure , and individual fields

are its members

Usman Younis

Structure

 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; };

First Name

NIC Number

NIC

Char[]

Last Name

Height

Char[] NIC int

Char[] Char[] int

Person 1 Person 2

Structure Assignment

 Structures can be used as variables, such that the

value is assigned to another variable of the same

type.

 E.g.

Record Person1 = {โ€œusmanโ€, โ€œyounisโ€, {123, 123, 1}, 123};

Record Person2;

Person2 = Person1;

usman younis 123, 123, 1 123

usman younis 123, 123, 1 123

Person1 Person

Structure Assignment (contd..)

 However; you cannot perform arithmetic,

comparison, etc., operations on these variables

Person1 + Person

If(Person1 < Person2)

 You have to use โ€œ.โ€ (DOT) operator to access the

members. No direct access

cout<<Person1;

Structures as an Array

 We can create a list of records using structure

arrays

Record Family[6]; //a family of 6 persons

 Like an ordinary array, the data will be stored

sequentially

int

[0]

int

[1]

int

[2]

int

[3]

int

[4]

[0] [1] [2] [3] [4]

An array of

Integers

An array of

Structures

Structures as an Array (contd..)

 The members are accessed the similar way, i.e.,

using the โ€œ.โ€ (DOT) operator

 E.g.

for( int i = 0; i < 6; i++)

cout<<Family[i].first_name;