05 Abstract Data Type
05 Abstract Data Type
Mostafa S. Ibrahim
Teaching, Training and Coaching since more than a decade!
Img Src
What vs How
● Do you care how:
○ a TV/Car work? Google really searches and find results? Browser access the internet?
○ C++ computes pow(2.0, 3.2)
○ C++ handles OS to read/write from files using fstream?
● Most of the time, the user care about WHAT not HOW
○ What = Function takes and return
○ How = it is implemented. But
■ Some implementation can be slow (loop to sum 1 to n) or fast (sum = n * n+1 / 2)
■ Some might be buggy or stable (internet explorer vs Firefox)
■ Some might takes more memory (chrome vs Firefox)
○ We can change the internal implementation of the class independently without affecting the
user.
■ User depends on limited visible functionalities of specific WHAT details
Data Types
● Primitive Data Type
○ E.g. int, float
○ Supported Operations: e.g. x + 2 * y
● User-Defined Data Type
○ E.g. Our vector
○ Supported Operations: push_back, find, get(idx)
● What is an Abstract Data Type?
○ It is like a user defined data type
○ But we focus on the what: e.g. push_back
○ But we don’t care about how (not specified yet)
■ is it slow push_back or push_back with capacity trick?
○ There is only ONE what, but MANY how
What is ADT?
● ADTs are a theoretical concept. More like logical/mathematical view
○ We specify the what part and also potentially the expected performance
○ It is independent of a programming language and how it will be implemented
● Data structures are concrete. They are implementing the ADT
○ E.g. providing a push_back functionality with capacity enhancement
● The word abstraction?!
○ Abstraction is about hiding unwanted details while showing most essential in a given context
○ So we show the expected ‘what’ is supported and hide the how
○ Abstraction = High-level
■ Tip: Senior managers have high abstraction skills
■ They focus on the big picture and let the technical details for the engineers
Why ADT?
● Recall when you learned STL vector or stack, did u care how implemented?
○ Similarly, when you first learned driving, you never care of the inner details
● ADT are acting like an interface
○ We as clients: use it based on the agreed provided functionality (interface)
○ The implementer: follow the agreed design (interface)
● In industry
○ You discuss with your team lead the proper interface (provided functionalities / logic)
○ Then implement it
Vector ADT
● get-size()
● get-front()
● get-back()
● get(idx)
● set(idx, val)
● print()
● find(value): Return the position of the given value or None if not found
○ In C++, the position will be 0-based index. We can return -1 if not found
● push-back(value): Expected to add an element to the end of the vector
C++: Headers for interface separation
“Acquire knowledge and impart it to the people.”