Difference between revisions of "C++ programming/Chapter 12"
m (New page: =Classes and Abstraction=) |
m (→Example) |
||
(9 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Classes and Abstraction= | =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 before another time. | ||
+ | : Remove timeobject | ||
+ | |||
+ | =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. | ||
+ | ==Object== | ||
+ | An '''instantiated''' example of a class | ||
+ | =Syntax= | ||
+ | ==Example== | ||
+ | *[[/TimeOfDay example|TimeOfDay Example]] | ||
+ | <Source lang="cpp"> | ||
+ | class TimeOfDay | ||
+ | { | ||
+ | public: | ||
+ | TimeOfDay() ; | ||
+ | // Constructor: Creates a zero TimeOfDay object | ||
+ | TimeOfDay(int hours, int minutes, int seconds) ; | ||
+ | // Constructor: Creates a TimeOfDay object with the given time | ||
+ | Void Increment() const; | ||
+ | // Transformer: Increment TimeOfDay by one second | ||
+ | TimeOfDay getTime() const; | ||
+ | // Observer: returns the TimeOfDay object to cout | ||
+ | bool Equal(TimeOfDay otherTime) const; | ||
+ | // Observer: Returns true if this TimeOfDay object equals otherTime | ||
+ | bool LessThan(TimeOfDay otherTime) const; | ||
+ | // Observer: Returns true if thi s TimeOfDay object is earlier than otherTime | ||
+ | private: | ||
+ | int hours; | ||
+ | int minutes; | ||
+ | int seconds; | ||
+ | } ; | ||
+ | </source> | ||
+ | [[Category:Programming]][[Category:C++]] |
Latest revision as of 06:09, 17 January 2011
Contents
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 before another time.
- Remove timeobject
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.
Object
An instantiated example of a class
Syntax
Example
class TimeOfDay
{
public:
TimeOfDay() ;
// Constructor: Creates a zero TimeOfDay object
TimeOfDay(int hours, int minutes, int seconds) ;
// Constructor: Creates a TimeOfDay object with the given time
Void Increment() const;
// Transformer: Increment TimeOfDay by one second
TimeOfDay getTime() const;
// Observer: returns the TimeOfDay object to cout
bool Equal(TimeOfDay otherTime) const;
// Observer: Returns true if this TimeOfDay object equals otherTime
bool LessThan(TimeOfDay otherTime) const;
// Observer: Returns true if thi s TimeOfDay object is earlier than otherTime
private:
int hours;
int minutes;
int seconds;
} ;