Lect17Structs.ppt

advertisement
Lec17 Structs
1
Review of Data Types
float: floating point numbers
int : integer numbers
char: single ASCII character
string: a class representing a sequence of
characters
ifstream, ofstream: classes representing
pathways to files
2
These are great but somewhat
limited
How would you represent an automobile in
a computer?
What about an ATM bank account?
An employee entry in a database?
And so on
We need a type that goes beyond simple one
type data.
Enter…the struct!!
3
Structured Data Types
A structured data type is one in which each
value is a collection of components and
whose organization is characterized by the
method used to access individual
components (i.e. the dot ‘.’ operator)
4
Structs
A record or a struct in C++, is a structured
data type with a fixed number of
components that are accessed by name. The
components may be heterogeneous (mixed
types).
A field or component is a piece of the
record.
5
Syntax of struct definition
struct TypeName
{
DataType MemberName;
DataType MemberName;
…
};
6
Example: StudentRec
struct StudentRec
{
string firstName;
string lastName;
float GPA;
int ID;
};
This would go above main()
7
Creating StudentRec variables
Once you declare a struct StudentRec, it
becomes a new data type you can use
StudentRec Bill, Tamara;
Bill
Tamara
firstName
ID
lastName
GPA
firstName
ID
lastName
GPA
8
Accessing a field
To assign a value to one of the fields, you use the
member selector operator ‘.’
StudentRec Bill; // create a variable of type StudentRec
Bill.firstName = “William”;
Bill.lastName = “Borroughs”;
Bill
Bill.ID=333;
ID
firstName
Bill.GPA=3.87;
William
333
cout<<Bill.ID;
GPA
lastName
3.87
cin>>Bill.lastName;
Borroughs
9
Your turn
Declare a student record for a variable x
Make x store the data for Kalina Noori, who’s ID
is 456 and GPA is 3.97
Display the first and last names of x on the same
line
x
Input the ID and GPA of x
ID
firstName
lastName
GPA
10
Aggregate Operations (performed on a
struct as a whole)
1.
2.
3.
4.
5.
6.
Aggregate Operation
I/O
Assignment
Arithmetic
Comparison
Passing Arguments
Return from a
function
Allowed on Structs?
No
Yes
No
No
Yes, value and reference
Yes
Operations labelled ‘no’ can still be performed
on a field by field basis (see next slide)
11
Example How to do the operations
on StudentRec x, y;
Operation
1. I/O
Correct Example
cin>>x.firstName>>x.lastName
>>x.ID>>x.GPA; (Wrong: cin>>x)
2. Assignment
3. Arithmetic
y=x; (copies all fields) or x.ID=y.ID
y.GPA=x.GPA+y.GPA; (Wrong: x=x+y)
4. Comparison
if (x.ID < y.ID)
5. Passing to Fn Display(x);
6. Function return return x;
(Wrong: if (x<y))
OR y.GPA=sqrt(x.GPA)
OR return x.GPA
12
Initializing Structs
When a struct is created you can initialize it with a list of
values (initialization list):
StudentRec s={“Isa”, “Corwin”,3.5, 555};
The sequence must match the order of declaration in the
struct (see slide 7):
NO:
StudentRec s={“Isa”,“Corwin”,555,3.5};OOPS
You can only do this on declaring
NO:
StudentRec t;
t={“Bert”,“Lance”,3.5, 555};
ERROR
13
Data Abstraction
Data abstraction is the separation of a data’s logical
properties from its implementation, for example:
Logical Properties of a real number:
addition, multiplication, positive, negative
Implementation of a real number:
Hardware, limited bits, mantissa, exponent
how these can be used to provide adding, multiplication
An Abstract Data Type is a data type whose
properties are specified independently of any particular
implementation. How we represent real things on a
computer
This leads us to Classes (2 weeks!)
14
TRY IT NOW!!
Prepare for Project 2, Part 2
Practice basic concepts of structs by
completing Assignment 11, problems 1-7
15
Download