structure 1 (2)
structure 1 (2)
Structures
By Abdela A. 1
Introduction
• Topic to be covered:
• Introduction to structure
• Structure vs. record
• Array vs. structure
• Defining and initialization of structure
• Array of structure
• Pointer to structure
By Abdela A. 2
Records
• Aggregates of several items of possibly different types
that represent related information called RECORD
• E.g
– Student Record:
• Name
• Grades
• Final Average
– Bank Record:
• Customer Name
• Account Balance
• Amount
By Abdela A. 3
Records
– Arrays
• Recall that elements of arrays must all be of the
same: type(Homogenous)
scores
scores : 85
85 79
79 92
92 57
57 68
68 80
80 ......
0 1 2 3 4 5 98 99
•
• Structure
–employee
In someR.
employee situations,
R.Jones
Jones 123 we 6/12/55
123Elm
Elm wish to$14.75
6/12/55 group e
$14.75
– Elements of different types( Hetrogenous)
By Abdela A. 4
Structures
• A Structure is a container, it can hold a group of
things.
(These things can be of any type).
• C++ provides a way to collect similar variables
into a single structure
– using C++ ‘struct’ keyword
• A structure definition is a user-defined variable
type
• you can create variables from it just like you
would any other type
By Abdela A. 5
Structures
• The term structure in C++ means both a
user-defined type which is a grouping of
variables as well as meaning a variable
based on a user-defined structure type
• Its heterogeneous collection of item or
element
By Abdela A. 6
Syntax of structure in C++
• Before creating a structure variable you must create a
structure definition
• Syntax Tag
– struct structname
– {
– datatype1 variable1; Members
– datatype2 variable2; function
– };
• This is simply a data blue print
• Each thing in a structure is called member.
• Each member has a name, a type - Names follow the
rules for variable names.
By Abdela A. 7
Structures
Example struct student
{
int id;
char name[15];
};
You can’t initialize members in struct definition
• E.g.
– struct date{
int day = 24, month = 10, year = 2001;
}; //this is wrong
By Abdela A. 8
Declaring and using structure
• By defining a structure you create a new
data type.
• Once you have defined a structure you
can create a variable from it just as you
would any other variable
• E.g:
• student std1;
• date birthday;
By Abdela A. 9
Initializing a structure
• Student std1={"Ababe","Scr/2222/22"};
– you can assign fewer values than there are
member variables
– Try to assign more values than are member
variables, you will get a compiler error
By Abdela A. 10
Accessing Members
• You can treat the members of a structure just like variables.
• Use the name of the record
the name of the member
separated by a dot
• You need to use the member access operator '.' (pronounced
"dot"):
• E.g
cout << stu.name << endl;
stu.id = 100;
stu.name= “dawit”;
StudentRecord s1,s2;
s1.name = "Joe Student";
By Abdela A. 11
#include<iostream.h>
#include<conio.h> cin>>s2.id;
cout<<"\nEnter Name";
struct student
cin>>s2.name;
{
cout<<"\nStudents
int id; Information";
char name[15]; cout<<"\n Student id\t
}; Student Name";
void main() cout<<endl<<s1.id<<"\
{ t"<<s1.name;
//creating two student cout<<endl<<s2.id<<"\
t"<<s2.name;
variables
getch();
student s1,s2;
}
cout<<"\n Enter Student Id";
cin>>s1.id;
cout<<"\nEnter Name";
cin>>s1.name;
cout<<"\n Enter Student Id";
By Abdela A. 12
Aggregate
• Limitations Operations with
on aggregate operations
– no I/O Structures
– no arithmetic operations
cout
cout <<
<< s1;
s1;
cin
cin >>
>> s1;
s1;
– no comparisons
old_part
old_part == s1
s1 ++
s2;
s2;
if
if (s1
(s1 << s1)
s1)
cout
cout <<
<< ...;
...;
By Abdela A. 13
Aggregate Operations with
Structures
• struct variables must be compared member-
wise.
To compare the values of student and
newStudent, you must compare them member-
wise, as follows:
if(student.firstName == newStudent.firstName &&
student.lastName == newStudent.lastName) ...
By Abdela A. 14
Input/Output
• There are no aggregate input/output
operations on struct.
• Data in a struct variable must be read one
member at a time.
• Contents of a struct must be written one
member at a time.
By Abdela A. 15
Array of structure
• What if we have a record of 10 ,20,100 students
– Array of records
– student stud1[50];
By Abdela A. 18
Defining a structure in a
structure
• struct employee {
– string name;
– string id;
• struct date{
– int day,month,year;
– }; birthdate;
– }emp1,emp2;
– emp1.birtdate.year;
– emp2.birthdate.month;
By Abdela A. 19
Pointers to Structures
• Pointers to structures are used often.
• it affects the way you access the structure variable’s
members
date birth;
date *p = &birth;
*p.year = 1979; =>*(p.year) =1979
• There is another member access operator used with
pointers: ->
student *sptr;
…
cout << "Name is" << sptr->name;
cout << “ID is " << sptr->id;
By Abdela A. 20
Group assignment
1. Write a program using structure which accepts information on at
least five books. The inf. includes title of the book, and publication
year. There is another information about the book, called status,
status will automatically be given by the program based on the
publication year entered by the user, if year of put publication is
before 1990, the status is a “Out dated”; if the book is published
between 1991-2000, the status will automatically be “medium”, for
books published after the year 2000, status will be “latest”, the
program should output what is entered by the user in the following
ways as an example.
Title:
Publication year:
Status:
By Abdela A. 21
Group assignment
2. Write a program using structure which accepts information of 4 employees.
the information of employee in organization include: name, id,salary,location
You have to get or read data from keyboard or users and show or display
data in the following format: use array of structure
No name sex id salary location
1 kumar M 001 35000 MWU
2 peaven M 002 4600 JU
3 genet F 003 4000 MU
4 semira F 004 7000 WU
By Abdela A. 22
Group assignment
3. Write a C++ program to keep records and perform statistical analysis for
student information system for Mada Walabu University. The program will
prompt the user to choose the operation of records from a menu as shown
below:
=============================================
MENU
============================================
• Add student records
• Delete student records
• Update student records
• View all student records
• Find student by ID(search)
• Note: The program also asks the user to decide whether he/she wants to
continue the operation. If he/she input ‘y’, the program will prompt the user
to choose the operation gain. Otherwise, the program will terminate.
• Note: All students records store in an array of structure
By Abdela A. 23
Group assignment
• Title
• Student information system
• Dormitory information system
• Store management system
• Hospital(clinic) management system
• Bank management system
• Library management system
• Colleague of computing information management
system
• Cafe management system
By Abdela A. 24