Difference between revisions of "C++ programming/Chapter 12"

From Teknologisk videncenter
Jump to: navigation, search
m (ADT Specification)
m (Class)
Line 44: Line 44:
 
: A Data Type that is used to represent a ADT - Abstract Data Type.
 
: A Data Type that is used to represent a ADT - Abstract Data Type.
  
[[CategoryProgramming]][[Category:C++]]
+
[[Category:Programming]][[Category:C++]]

Revision as of 09:28, 16 January 2011

Classes and Abstraction

Knowledge goals

  • To understand the difference between specification and implementation of an abstract data type.
  • To understand the concepts of encapsulation and abstraction.
  • To understand how control and data abstraction facilitate modifiability and reuse.
  • To understand the basic class design principles.
  • To understand how encapsulation and information hiding are enforced by the C++ compiler.

To be able to

  • Declare a C++ class type.
  • Declare class objects, given the declaration of a class type.
  • Write client code that invokes class member functions.
  • Implement class member functions.
  • Organize the code for a C++ class into two files: the specification (.h) file and the implementation file.
  • Write a C++ class constructor.

ADT - Abstract Data Type

An Abstract Data Type is a constructed datatype with its functions. Often gathered in a Class. With Object Oriented Programming it is important to be consequent about the abstraction layers. That will make it easy to understand even very huge and complicated programming projects.

Why ADT

The main purpose of ADT is to hide the implementation details, and allow a user of the implementation to abstract from the inner details of the implementation. The black-box theory. Changes in the implementation will not affect the use of the ADT.

ADT Specification

The Type specification below of TimeOfDay hides the implementation details and lists the operations that can be performed on the TimeOfDay objects. (Objects are the TimeOfDay instances)

TYPE
TimeOfDay
DOMAIN
Each TimeOfDay value is a time of day in the form of hours, minutes, and seconds.
OPERATIONS
Create a time object.
Print (or write) the time.
Return an object containing the time, incremented by one second.
Compare two times for equality.
Determine if one time is "less than" (comes before) another.

Buzzwords of Object Oriented Programming

Constructor
An operation that initializes a new instance (variable) of an ADT.
Transformer
An operation that changes the value of the ADT; also known as a mutator.
Observer
An operation that allows us to observe the state of an instance of an ADT without changing it; also known as an accessor.
Destructor
An operation that cleans up the state of an ADT instance just prior to releasing its storage for reuse.
Iterator
An operation that allows us to process—one at a time—all the components in an instance of an ADT.

Class

Class
A Data Type that is used to represent a ADT - Abstract Data Type.