Abstraction is the separation of essential qualities from implementation details.
Focuses on what, not how.
Crucial for managing large, complex software projects.
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).
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.
A set of values (domain) and allowed operations on those values.
Example: int
has domain −32768…32767$$-32768 … 32767$$ and operations ,+,−,∗,/,$$, +, -, *, /, %, >>, <<$$.
An ADT is a data type defined by its properties (domain and operations) independently of any specific implementation.
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
TYPE ComplexNumber
DOMAIN: Each value is an ordered pair of real numbers (a,b)$$(a, b)$$ representing a+bi$$a + bi$$
OPERATIONS:
Initialize the complex number
Write the complex number
Add
Subtract
Multiply
Divide
Determine the absolute value of a complex number
Choose a specific data representation using existing data types (built-in or programmer-defined).
Write functions for each allowable operation.
3 int variables
3 strings
3-element int array
Example:
10$$10$$, 45$$45$$, 27$$27$$
“10”$$“10”$$, “45”$$“45”$$, “27”$$“27”$$
Choice depends on time, space, and algorithms needed to implement operations.
struct with 2 float members
2-element float array
Example:
−16.2$$-16.2$$, 5.8$$5.8$$
Simple:
integral: char
, short
, int
, long
, bool
, enum
floating: float
, double
, long double
structured: array
, struct
, union
, class
address: pointer
, reference
In Time.h
(specification file).
Declares a class data type (does not allocate memory).
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;
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.
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.
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.
Two kinds: data members and function members.
Class members are private by default.
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.
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.
Specification file (time.h
): specifies data and function members.
Implementation file (time.cpp
): implements the Time member functions.
Includes the specification file: #include “ time.h”
.
Uses scope resolution operator to associate functions with the class: bool Time::Equal(/* in */ Time otherTime) const
The member selection operator (.)
selects either data members or function members.
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.
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 { . . . }
Shows currentTime
and endTime
objects with private data (hrs
, mins
, secs
) and public functions (Set
, Increment
, Write
, LessThan
, Equal
).
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).
void Time::Write () const
The const
keyword indicates that this member function does not modify the object's state.
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
#ifndef TIME_H
#define TIME_H
Wrap the class declaration in time.h
within these directives to ensure it's included only once.
#endif
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.
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.
Time (int initHrs, int initMins, int initSecs);
(Parameterized constructor)
Time();
(Default constructor)
Time::Time ()
Postcondition: hrs == 0 && mins == 0 && secs == 0
Time::Time(int initHrs, int initMins, int initSecs)
Precondition:
0<=initHrs<=23$$0 <= initHrs <= 23$$
0<=initMins<=59$$0 <= initMins <= 59$$
0<=initSecs<=59$$0 <= initSecs <= 59$$
Postcondition:
hrs == initHrs && mins == initMins && secs == initSecs
Time departureTime;
// Default constructor invoked
Time movieTime (19, 30, 0);
// Parameterized constructor invoked
Classes and Abstraction
Search(list, item, length, where, found);
.int
has domain −32768…32767 and operations ,+,−,∗,/,.char
, short
, int
, long
, bool
, enum
float
, double
, long double
array
, struct
, union
, class
pointer
, reference
Time.h
(specification file).void Set (int hours , int mins , int secs);
void Increment ();
void Write () const;
bool Equal (Time otherTime) const;
bool LessThan (Time otherTime) const;
int hrs;
int mins;
int secs;
#include “time.h”
.Time currentTime;
, Time endTime;
currentTime.Set (5, 30, 0);
, currentTime.Increment ();
, etc.time.h
): specifies data and function members.time.cpp
): implements the Time member functions.#include “ time.h”
.bool Time::Equal(/* in */ Time otherTime) const
(.)
selects either data members or function members.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”);
Write()
.currentTime.Write(); // Class Time
, numberZ.Write(); // Class ComplexNumber
void Time::Write () const { . . . }
currentTime
and endTime
objects with private data (hrs
, mins
, secs
) and public functions (Set
, Increment
, Write
, LessThan
, Equal
).const
.const
in both the function prototype (in specification file) and the heading of the function definition (in implementation file).void Time::Write () const
const
keyword indicates that this member function does not modify the object's state.time.h
, client.cpp
, and time.cpp
into object files and then linking them into an executable.#include
statements for the same header file.#ifndef Preprocessor_Identifier
#define Preprocessor_Identifier
...
#endif
#ifndef TIME_H
#define TIME_H
time.h
within these directives to ensure it's included only once.#endif
Time (int initHrs, int initMins, int initSecs);
(Parameterized constructor)Time();
(Default constructor)Time::Time ()
hrs == 0 && mins == 0 && secs == 0
Time::Time(int initHrs, int initMins, int initSecs)
hrs == initHrs && mins == initMins && secs == initSecs
Time departureTime;
// Default constructor invokedTime movieTime (19, 30, 0);
// Parameterized constructor invoked