Abstraction
- Abstraction is the separation of essential qualities from implementation details.
- Focuses on what, not how.
- Crucial for managing large, complex software projects.
Control Abstraction
- Separates logical properties of an action from its implementation.
- Example:
Search(list, item, length, where, found);
. - The function call relies on the function's specification (description), not its implementation (algorithm).
Data Abstraction
- Separates logical properties of a data type from its implementation.
- Logical properties encompass possible values and required operations.
- Implementation involves how these are done in C++ and how data types are used.
Data Type
- A set of values (domain) and allowed operations on those values.
- Example:
int
has domain -32768 … 32767 and operations , +, -, *, /, %, >>, <<.
Abstract Data Type (ADT)
- An ADT is a data type defined by its properties (domain and operations) independently of any specific implementation.
ADT Specification Example: Time
- TYPE Time
- DOMAIN: Each Time value is a time in hours, minutes, and seconds.
- OPERATIONS:
- Set the time
- Print the time
- Increment by one second
- Compare 2 times for equality
- Determine if one time is “less than” another
ADT Specification Example: ComplexNumber
- TYPE ComplexNumber
- DOMAIN: Each value is an ordered pair of real numbers (a, b) representing a + bi
- OPERATIONS:
- Initialize the complex number
- Write the complex number
- Add
- Subtract
- Multiply
- Divide
- Determine the absolute value of a complex number
ADT Implementation
- Choose a specific data representation using existing data types (built-in or programmer-defined).
- Write functions for each allowable operation.
Possible Representations of ADT Time
- 3 int variables
- 3 strings
- 3-element int array
- Example:
- 10, 45, 27
- “10”, “45”, “27”
- Choice depends on time, space, and algorithms needed to implement operations.
Possible Representations of ADT ComplexNumber
- struct with 2 float members
- 2-element float array
- Example:
C++ Data Types
- Simple:
- integral:
char
, short
, int
, long
, bool
, enum
- floating:
float
, double
, long double
- structured:
array
, struct
, union
, class
- address:
pointer
, reference
Class Time Specification
- In
Time.h
(specification file). - Declares a class data type (does not allocate memory).
Class Time Specification (Public and Private Members)
- Public:
void Set (int hours , int mins , int secs);
void Increment ();
void Write () const;
bool Equal (Time otherTime) const;
bool LessThan (Time otherTime) const;
- Private:
int hrs;
int mins;
int secs;
C++ classType
- Facilitates re-use of C++ code for an ADT.
- Software using the class is called a client.
- Variables of the class type are called class objects or class instances.
- Client code uses public member functions to manipulate class objects.
Client Code Using Time
- Includes specification of the class using
#include “time.h”
. - Declares objects of Time class:
Time currentTime;
, Time endTime;
- Calls member functions:
currentTime.Set (5, 30, 0);
, currentTime.Increment ();
, etc.
Class Declaration
- Creates a data type and names the members of the class.
- Does not allocate memory for any variables of that type.
- Client code needs to declare class variables.
Class Members
- Two kinds: data members and function members.
- Class members are private by default.
Private vs. Public Members
- Data members are generally private.
- Function members are generally public.
- Private class members can be accessed only by class member functions (and friend functions), not by client code.
Aggregate Class Operations
- Built-in operations valid on class objects:
- Member selection using dot (.) operator
- Assignment to another class variable using (=)
- Pass to a function as argument (by value or by reference)
- Return as value of a function
- Other operations can be defined as class member functions.
Separate Specification and Implementation
- Specification file (
time.h
): specifies data and function members. - Implementation file (
time.cpp
): implements the Time member functions.
Implementation File for Time
- Includes the specification file:
#include “ time.h”
. - Uses scope resolution operator to associate functions with the class:
bool Time::Equal(/* in */ Time otherTime) const
Member Selection Operator
- The member selection operator
(.)
selects either data members or function members.
I/O Classes
- Header files
iostream
and fstream
declare the istream
, ostream
, and ifstream
, ofstream
I/O classes. cin
and cout
are class objects.get
and ignore
are function members: cin.get (someChar);
, cin.ignore (100, ‘
’);
ifstream myInfile;
, myInfile.open (“mydata.dat”);
- Class implementation details are hidden from the client’s view.
- Public functions provide the interface between client code and class objects.
- Abstraction barrier between specification and implementation.
Selection and Resolution
- C++ programs typically use several class types.
- Different classes can have member functions with the same identifier, like
Write()
. - Member selection operator determines which object's member function is called:
currentTime.Write(); // Class Time
, numberZ.Write(); // Class ComplexNumber
- Scope resolution operator in implementation file specifies the class:
void Time::Write () const { . . . }
Time Class Instance Diagrams
- Shows
currentTime
and endTime
objects with private data (hrs
, mins
, secs
) and public functions (Set
, Increment
, Write
, LessThan
, Equal
).
Use of const with Member Functions
- When a member function does not modify the private data members, use
const
. - Use
const
in both the function prototype (in specification file) and the heading of the function definition (in implementation file).
Example Using const with a Member Function
void Time::Write () const
- The
const
keyword indicates that this member function does not modify the object's state.
Separate Compilation and Linking of Files
- Shows the process of compiling
time.h
, client.cpp
, and time.cpp
into object files and then linking them into an executable.
- Use preprocessor directives to prevent compilation errors from multiple
#include
statements for the same header file. - Syntax:
#ifndef Preprocessor_Identifier
#define Preprocessor_Identifier
...
#endif
Example Using Preprocessor Directive
#ifndef TIME_H
#define TIME_H
- Wrap the class declaration in
time.h
within these directives to ensure it's included only once. #endif
Class Constructors
- A member function to initialize the private data members of a class object.
- Name is always the name of the class, and there is no return type.
Class Constructors (Multiple Constructors)
- A class may have several constructors with different parameter lists.
- A constructor with no parameters is the default constructor.
- A constructor is implicitly invoked when a class object is declared.
- If there are parameters, their values are listed in parentheses in the declaration.
Specification of Time Class Constructors
Time (int initHrs, int initMins, int initSecs);
(Parameterized constructor)Time();
(Default constructor)
Implementation of Time Default Constructor
Time::Time ()
- Postcondition:
hrs == 0 && mins == 0 && secs == 0
Parameterized Constructor
Time::Time(int initHrs, int initMins, int initSecs)
- Precondition:
- 0 <= initHrs <= 23
- 0 <= initMins <= 59
- 0 <= initSecs <= 59
- Postcondition:
hrs == initHrs && mins == initMins && secs == initSecs
Automatic Invocation of Constructors
Time departureTime;
// Default constructor invokedTime movieTime (19, 30, 0);
// Parameterized constructor invoked