Download presentation
Presentation is loading. Please wait.
Published byBenedict Chase Modified over 9 years ago
1
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types
2
ADT Basics All about designing a data type that others can use easily! Two parts A set of data (struct) Operations on that data (methods) Separation between use and implementation We’re concerned with the implementation of ADTs For Java programmers, very similar to classes Small aside, OOP design principles are not enforced by the C language, we must enforce them ourselves Goffin – ENEE150
3
Why use ADTs? The separation of use and implementation increases portability of code Example: Complex Number ADT One person will write the data type, but ANY other programmers can use the data type in their code! Simple as a declaration of any other primitive (int, float, char, double) data type Goffin – ENEE150
4
ADT Implementation Use a struct to group together the data stored by the ADT typedef struct complex_type{ float real; float imag; } complex; Declare “operations” using function prototypes Goffin – ENEE150
5
ADT Operations Public operations/functions can be called by the user of the ADT Such as constructor and destructor Private operations/functions can only be used in the implementation of an ADT The compiler should not let the user call these functions Goffin – ENEE150
6
Encapsulation Place struct and public function prototypes in header file Public function implementation and private functions go into.c file of same name as header You can explicitly make private functions private with the “static” keyword – static functions cannot be called in another.c file Ex: static int foo(); Same goes with global variables Goffin – ENEE150
7
Polymorphism If you want to treat the type of an ADT’s data as a variable, you need a slightly different approach. Two (main) approaches in C: Unions Generic Pointers Goffin – ENEE150
8
Unions Single variable which can take on a fixed number of data types union val{ int ival; float fval; char cval; } x; Stores only one value, but can be any of the declared data types Access each particular data type with ‘.’ syntax, like structs Goffin – ENEE150
9
Generic Pointers Pointers that can point to any kind of data declared with “void *” Can be set to an endless number of types that don’t have to be specified by the implementation More powerful than unions in this sense That said, you need to cast the relevant information to a “void *” explicitly
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.