Download presentation
Presentation is loading. Please wait.
1
CS148 Introduction to Programming II
Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 14: 3/17/2003 Lecture 14: 3/17/2003 CS148 Spring 2003
2
Outline Abstract Data Types Introduction to C++ Classes
Lecture 14: 3/17/2003 CS148 Spring 2003
3
Abstract Data Types 1/3 Abstraction Control abstraction
The act of separating the essential qualities of an object from the details of how it works or how it is composed Control abstraction Separation of logical properties of an action from its implementation When we invoke a library function, we use the function specification, without knowing the details of the function’s implementation, e.g., sqrt function in math library Data Abstraction Every data type consists of a set of values along with a collection of allowable operations on those values Lecture 14: 3/17/2003 CS148 Spring 2003
4
Abstract Data Types 2/3 Data abstraction comes into play when we declare a data type that is not built into the programming language The new data type can be defined as an Abstract Data Type (ADT) An ADT is a data type whose properties (domain and operations) are specified independently of any particular implementation An ADT has a specification (What?) and an implementation (How?) The specification describes the characteristics of the data values as well as the behavior of each of the operations on those values The user of an ADT needs to understand only the specification, not the implementation in order to use it Lecture 14: 3/17/2003 CS148 Spring 2003
5
Abstract Data Types 3/3 An ADT for representing Time TYPE OPERATIONS
DOMAIN Each TimeType value if a time of day in the form of hours, minutes, and seconds OPERATIONS Set the time Print the time Increment the time by one second Compare two times for equality Determine if one time comes before another Lecture 14: 3/17/2003 CS148 Spring 2003
6
C++ Classes 1/3 C++ supports representing ADTs by providing a built-in structured type known as class Four structured data types available in C++: Array, struct, union, and class A class is similar to a struct but its components (class members) include not only data, but also functions that manipulate that data A C++ class declaration corresponding to TimeType ADT class TimeType { public: void set (int, int, int); void Increment(); void Write(); bool Equal (TimeType); bool LessThan (TimeType); private: int hrs; int mins; int secs; }; TYPE: TimeType DOMAIN: Each TimeType value if a time of day in the form of hours, minutes, and seconds OPERATIONS Set the time Print the time Increment the time by one second Compare two times for equality Determine if one time comes before another Lecture 14: 3/17/2003 CS148 Spring 2003
7
C++ Classes 2/3 The declaration of a class defines a data type but does not create variables of the type (a class variable is called a class object or class instance) Class objects created using variable declarations TimeType startTime; Any software that declares and manipulates TimeType objects is called a client of the class Data and/or functions declared as public constitute the public interface, which means clients can access these class members directly Class members declared as private are considered as private information and are inaccessible to clients. In C++, class members are by default private Built-in operations on classes: member selection and assignment startTime.Increment(); //using dot notation Lecture 14: 3/17/2003 CS148 Spring 2003
8
C++ Classes 3/3 TimeType time1; TimeType time2;
int inputHrs,inputMins,inputSecs; time1.Set(5,20,0); cin >> inputHrs >>inputMins >>inputSecs; time2.Set(inputHrs,inputMins,inputSecs); if (time1.LessThan(time2)) … time2 = time1; time2.Write(); class TimeType { public: void set (int, int, int); void Increment(); void Write(); bool Equal (TimeType); bool LessThan (TimeType); private: int hrs; int mins; int secs; }; Lecture 14: 3/17/2003 CS148 Spring 2003
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.