Abstract Data Types - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Abstract Data Types

Description:

Abstract Data Types ... Examples. struct Point. int x, y; Point p1, p2; ... intuitive notation, but it is only appropriate when the initializers are simple ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 23
Provided by: anni65
Category:

less

Transcript and Presenter's Notes

Title: Abstract Data Types


1
Structures
2
Abstract Data Types
  • a type that is defined by specifying its
    properties and the operations that preserve those
    properties

3
structs
  • the keyword struct allows us to create structured
    data types
  • a structured data type combines the properties or
    attributes of a data representation and the
    behavior of that data into a single data type
  • operations can be defined that operate on the
    data as a whole rather than operating on its
    components

4
Syntax
struct ltstructure namegt ltdata typegt
prop1 ltdata typegt prop2 ltdata typegt
behavior1(ltarg listgt) ltdata typegt behavior1(ltarg
listgt) ltoptional variable to declaregt
5
Examples
struct Point int x, y Point p1,
p2 struct Complex int real,
imag Complex c1, c2 struct Fraction
int num, denom Fraction f1, f2
6
Accessing Attributes
The individual components of a struct are called
attributes or members. Attributes can be
accessed via the . operator when the variable
is an instance of the structured data type. Point
p1 p1.x 2 p1.y 8 Attributes can be
accessed via the -gt operator when the variable
is a pointer to an instance of the structured
data type. Point pptr p1 pptr-gtx
8 pptr-gty 10
7
Operations on structs
  • Most structs should support the following
    operations
  • initialization
  • assignment
  • I/O
  • comparisons
  • Additionally, most structs support custom
    behavior such as mathematical operations.

8
Initialization
Initialization can be accomplished using list
notation. The values are assigned based on the
order of the attribute declarations in the
structure declaration. Point origin
0,0 Fraction f1 2, 3 It can also be
accomplished by initializing it with the value of
an already initialized structure of the same
type. Fraction f2 f1 Or from the value
returned by a function call. Point p2
make_point(0,3)
9
Assignment
Assignment to structures can be accomplished on
the structure as a whole or to the individual
attributes of a structure. Fraction f1
3,4 Fraction f2 4,5 Fraction dup dup
f1 sum fraction_sum(f1, f2) dup.num
f2.num dup.denom f2.denom
10
Structures as Function Parameters
Structures can be passed as parameters to
functions. They can be passed by value or by
referencesame as any other data type. Point
increment(Point pt, int inc) Point temp
temp.x pt.x inc temp.y pt.y inc
return temp
11
Pass by Reference
Point increment(const Point pt, int inc)
Point temp temp.x pt.x inc temp.y
pt.y inc return temp Or void
increment(Point pt, int inc) pt.x inc
pt.y inc What would be an advantage of
passing by reference?
12
I/O
Input and Output of structured data types is not
built in to C. It can be accomplished by
writing specialized functions. void
print_fraction(const Fraction f1) void
read_fraction(Fraction f1) Or using standard
I/O instructions on the attributes cout ltlt
f1.num ltlt / ltlt f1.denom cout ltlt ( ltlt pt.x ltlt
, ltlt pt.y ltlt ) cin gtgt f1.num gtgt
f1.denom cin gtgt pt.x gtgt pt.y
13
Arrays of Structures
Arrays of structures can be declared using the
same notation used for declaring arrays of
built-in types. Point parray25 // array of
25 points An array of structures can be
initialized using list notation. Point parray4
2, 3, 4, 5, 6, 7, 8, 9 This notation may
not be the most intuitive notation, but it is
only appropriate when the initializers are simple
variables or character strings. Additional
braces are required in other cases.
14
The items in the array can also be initialized
individually Point parray4 parray0.x
1 parray0.y 1 parray1.x 2 parray1.y
2 parray2.x 3 parray2.y
3 parray3.x 4 parray3.y 4
15
Structures as Building Blocks
A structured data type can be composed of
instances of other structured data types. struct
Rectangle Point upper_left Point
lower_right To access members of upper_left
and lower_right, the . operator must be applied
multiple times. rect.upper_left.x
12 rect.upper_left.y 12
16
How could you initialize an array of
rectangles? Rectangle rarray2 0,
12, 12, 0 , 0, 15, 15, 0
How could you pass an array of rectangles
to a function? void rect_fun(Rectangle rarray,
int size) for (int index 0 index lt size
index) rarrayindex.upper_left.x
-index - 5 rarrayindex.upper_left.y
index 5 rarrayindex.lower_right.x
index 5 rarrayindex.lower_right.y
-index - 5
17
Defining Behavior
Behavior for structures can be defined by writing
functions void print_point(const Point p)
cout ltlt ( ltlt p.x ltlt , ltlt p.y ltlt ) ltlt
endl int rect_area(const Rectangle r)
int dim1 abs(r.upper_left.x -
r.lower_right.x) int dim2
abs(r.upper_left.y - r.lower_right.y) return
dim1 dim2 Complex complex_add(const
Complex op1, const Complex
op2) Complex sum op1.real op2.real,
op1.imag op2.imag return sum
18
OR by defining member functions. A member
function is any function that is declared within
the scope of the structure declaration. struct
Point void print() struct Rectangle
int area() struct Complex Complex
add(const Complex op2)
19
Defining Member Functions
Use the scope resolution operator to
distinguish a member function from a non-member
function. void Pointprint() cout ltlt (
ltlt x ltlt , ltlt y ltlt ) ltlt endl int
Rectanglearea() return (abs(upper_left.x -
lower_right.x) abs(upper_left.y -
lower_right.y)) Complex Complexadd(const
Complex op2) Complex sum real
op2.real, imag op2.imag return sum
20
Calling Member Functions
Use the . operator to call member functions.
This operator requires an instance of the
structured data type to exist. Point p p.x
3 p.y 6 p.print() Rectangle r1 p1, p2
cout ltlt r1.area() Complex c1 3,
8 Complex c2 2, 4 Complex sum c1.add(c2)
21
File Organization with structs
When working with structures, the declaration of
the structure belongs in a header file, using a
.h extension. The implementation of the
structures belongs in a source file, using a .cpp
extension. Point.h and Point.cpp Complex.h and
Complex.cpp Fraction.h and Fraction.cpp Rectangle.
h and Rectangle.cpp
22
Header files should be wrapped with preprocessor
directives to prevent multiple inclusions //
Point.h ifndef _POINT_H_ define
_POINT_H_ class Point endif //
_POINT_H_ Source files should include header
files using string quotations rather than
brackets // Point.cpp include Point.h
Write a Comment
User Comments (0)
About PowerShow.com