C++ Chapter3
C++ Chapter3
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
old_part
old_part == s1
s1 ++ s2;
s2;
– no comparisons
if
if (s1
(s1 << s1)
s1)
cout
cout <<
<< ...;
...;
Aggregate Operations with
Structures
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;
stu.print_ave();