Download Understanding Classes, Data Abstraction, and Time ADT in OOP and more Slides Programming for Engineers in PDF only on Docsity!
Chapter 9: Classes and
Data Abstraction
Outline
6.1 Introduction
6.5 Implementing a Time Abstract Data Type with a class
6.6 Class Scope and Accessing Class Members
6.7 Separating Interface from Implementation
6.8 Controlling Access to Members
6.9 Access Functions and Utility Functions
6.10 Initializing Class Objects: Constructors
6.11 Using Default Arguments with Constructors
6.12 Destructors
6.13 When Constructors and Destructors Are Called
6.14 Using Set and Get Functions
6.15 Subtle Trap: Returning a Reference to a
private Data Member
6.16 Default Memberwise Assignment
6.17 Software Reusability
6.1 Introduction
- Object-oriented programming (OOP)
- Encapsulates data (attributes) and functions (behavior) into packages called classes
- Information hiding
- Implementation details hidden within classes themselves
- User-defined (programmer-defined) types: classes
- Data (data members)
- Functions (member functions or methods)
- Similar to blueprints – reusable
- Class instance: object
6.5 Implementing a Time Abstract Data Type with a class
- Constructor function
- Special member function
- Initializes data members
- Same name as class
- Called when object instantiated
- Several constructors
- No return type
Class Time definition (1 of 1) 1 class Time { 2 3 public: 4 Time(); // constructor 5 void setTime( int, int, int ); // set hour, minute, second 6 void printUniversal(); // print universal-time format 7 void printStandard(); // print standard-time format 8 9 private: 10 int hour; // 0 - 23 (24-hour clock format) 11 int minute; // 0 - 59 12 int second; // 0 - 59 13 14 }; // end class Time
Member access specifiers.
Definition of class begins
with keyword class.
Class body starts with left
brace.
Class body ends with right
brace.
Definition terminates with
semicolon.
Function prototypes for
public member functions.
private data members
accessible only to member
functions.
Constructor has same name as
class, Time , and no return
type.
6.5 Implementing a Time Abstract Data Type with a class
- Objects of class
- After class definition
- Class name new type specifier
- Object, array, pointer and reference declarations
- Example:
Time sunset; // object of type Time
Class name becomes new
type specifier.
fig06_03.cpp (1 of 5) 1 // Fig. 6.3: fig06_03.cpp 2 // Time class. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setfill; 11 using std::setw; 12 13 // Time abstract data type (ADT) definition 14 class Time { 15 16 public: 17 Time(); // constructor 18 void setTime( int, int, int ); // set hour, minute, second 19 void printUniversal(); // print universal-time format 20 void printStandard(); // print standard-time format 21
Define class Time.
fig06_03.cpp (2 of 5) 22 private: 23 int hour; // 0 - 23 (24-hour clock format) 24 int minute; // 0 - 59 25 int second; // 0 - 59 26 27 }; // end class Time 28 29 // Time constructor initializes each data member to zero and 30 // ensures all Time objects start in a consistent state 31 Time::Time() 32 { 33 hour = minute = second = 0; 34 35 } // end Time constructor 36 37 // set new Time value using universal time, perform validity 38 // checks on the data values and set invalid values to zero 39 void Time::setTime( int h, int m, int s ) 40 { 41 hour = ( h >= 0 && h < 24 )? h : 0; 42 minute = ( m >= 0 && m < 60 )? m : 0; 43 second = ( s >= 0 && s < 60 )? s : 0; 44 45 } // end function setTime 46
Constructor initializes
private data members
to 0.
public member
function checks
parameter values for
validity before setting
private data
members.
fig06_03.cpp
(4 of 5)
70 // output Time object t's initial values 71 cout << "The initial universal time is "; 72 t.printUniversal(); // 00:00: 73 74 cout << "\nThe initial standard time is "; 75 t.printStandard(); // 12:00:00 AM 76 77 t.setTime( 13, 27, 6 ); // change time 78 79 // output Time object t's new values 80 cout << "\n\nUniversal time after setTime is "; 81 t.printUniversal(); // 13:27: 82 83 cout << "\nStandard time after setTime is "; 84 t.printStandard(); // 1:27:06 PM 85 86 t.setTime( 99, 99, 99 ); // attempt invalid settings 87 88 // output t's values after specifying invalid values 89 cout << "\n\nAfter attempting invalid settings:" 90 << "\nUniversal time: "; 91 t.printUniversal(); // 00:00: 92
Invoke public member
functions to print time.
Set data members using
public member function.
Attempt to set data members
to invalid values using
public member function.
93 cout << "\nStandard time: "; 94 t.printStandard(); // 12:00:00 AM 95 cout << endl; 96 97 return 0; 98 99 } // end main
6.5 Implementing a Time Abstract Data Type with a class
- Destructors
- Same name as class
- Preceded with tilde ( ~ )
- No arguments
- Cannot be overloaded
- Performs “termination housekeeping”
- For example, if your constructor uses new to allocate memory, the destructor should use delete to free that memory
6.5 Implementing a Time Abstract Data Type with a class
- Advantages of using classes
- Simplify programming
- Software reuse
- Class objects included as members of other classes
- Inheritance
- New classes derived from old
6.6 Class Scope and Accessing Class Members
- Function scope
- Variables declared in member function
- Only known to function
- Variables with same name as class-scope variables
- Class-scope variable “hidden”
- Access with scope resolution operator ( :: ) ClassName::classVariableName
- Variables only known to function they are defined in
Separating Interface from Implementation
- We began by including a class's definition and member- function definitions in one file.
- We then demonstrated separating this code into two files a header file for the class definition (i.e., the class's interface) and a source code file for the class's member function definitions (i.e., the class's implementation).
- Recall that this makes it easier to modify programs.