0% found this document useful (0 votes)
37 views

Chapter 4 Structure

Uploaded by

dillasemera2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Chapter 4 Structure

Uploaded by

dillasemera2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Chapter four

Structures
Records
• Aggregates of several items of possibly different
types that represent related information called
RECORD
• E.g
– Student Record:
• Name a string
• HW Grades an array of 3 doubles
• Test Grades an array of 2 doubles
• Final Average a double
– Bank Record:
• Customer Name
• Account Balance
• Amount
Records
– Arrays
• Recall that elements of arrays must all be of the
same type

scores
scores:: 85
85 79
79 92
92 57
57 68
68 80
80 ......
0 1 2 3 4 5 98 99
– In some situations, we wish to group elements
of different types

employee
employee R.
R.Jones
Jones 123
123Elm
Elm 6/12/55
6/12/55 $14.75
$14.75
Structures
• A Structure is a container, it can hold a bunch of
things.
– These things can be of any type.
• Structures are used to organize related data
(variables) in to a nice neat package.
• 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
struct in C++
• Before creating a structure variable you must
create a structure definition
tag
• Syntax
struct structname
{
datatype1 variable1; Members
datatype2 variable2;
};
• 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.
• Types can be any defined type
• 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
Declaring and using structs
• 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;
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
Accessing Members
• You can treat the members of a struct 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 '.'
• E.g
cout << stu.name << endl;
stu.id = 100;
stu.name= “dawit”;
StudentRecord s1,s2;
s1.name = "Joe Student";

s2 = s1;
Copies the entire structure
Array of structs
• What if we have a record of 10 ,20,100
students
– Array of records
• First declare a struct (such as student)
• Then specify an array of that type
– student stud1[50];
• Declares an array of 50 student data type
• We can declare an array of structs
#include<iostream> cout<<"\n Displaying student
#include<conio.h> Info";
Using namespace std; cout<<"\nStudent Id \t
struct student { Student Name";
cout<<"\
int id;
n======================
char name[15]; ======";
}; for( i = 0; i < 5; i++)
int main()
{ cout<<endl<<s[i].id<<"\t\t\
t"<<s[i].name;
//creating 10 student using an array getch(); }
student s[10];
int i;
for(i=0; i < 10; i ++)
{
cout<<"\n Enter Student Id";
cin>>s[i].id;
cout<<"\nEnter Name";
cin>>s[i].name;
}
Declaring a struct as a member of
struct
• E.g.
– struct employee {
string name;
string id;
date birthdate;
}emp1,emp2;
– struct date{
int day,month,year;
};
• To access the year that emp1 born
– emp1.birtdate.year
– emp2.birthdate.month // access the month
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;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy