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

C++ Chapter3

Uploaded by

aychewchernet
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)
32 views

C++ Chapter3

Uploaded by

aychewchernet
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/ 20

Chapter Three

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 '.'
(pronounced "dot"):
• 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
#include<iostream.h> cin>>s2.id;
#include<conio.h> cout<<"\nEnter Name";
struct student cin>>s2.name;
{ cout<<"\nStudents
Information";
int id;
cout<<"\n Student id\t
char name[15]; Student Name";
}; cout<<endl<<s1.id<<"\
void main() t"<<s1.name;
{ cout<<endl<<s2.id<<"\
//creating three student 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";
Aggregate Operations with
Structures

• Limitations on aggregate operations


– no I/O cout
cout <<
<< s1;
s1;
cin
cin >>
>> s1;
s1;
– no arithmetic operations

old_part
old_part == s1
s1 ++ s2;
s2;

– no comparisons
if
if (s1
(s1 << s1)
s1)
cout
cout <<
<< ...;
...;
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) ...
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.
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.h> cout<<"\n Displaying student
#include<conio.h> Info";
struct student { cout<<"\nStudent Id \t
int id; Student Name";
cout<<"\
char name[15];
n======================
}; ======";
void main() for( i = 0; i < 5; i++)
{
clrscr(); cout<<endl<<s[i].id<<"\t\t\
//creating 10 student using an array t"<<s[i].name;
student s[10]; getch(); }
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;
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;
Quick Example
struct StudentRecord {
char *name; // student name
double hw[3]; // homework
grades
double test[2]; // test grades
double ave; // final average

void print_ave() {
cout << "Name: " << name << endl;
cout << "Average: " << ave << endl;
}
};
Using the member function
doubleStudentRecord stu;

… // set values in the structure

stu.print_ave();

You might also like