1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.

Slides:



Advertisements
Similar presentations
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Advertisements

1 A pointer variable is a variable whose value is the address of a location in memory int x; x = 5; int* ptr1; ptr1 = &x; int* ptr2; ptr2 = ptr1; *ptr1.
Inheritance and Composition (aka containment) Textbook Chapter –
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 C++ Plus Data Structures Nell Dale Chapter 2 Data Design and Implementation Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
1 Chapter 6 Object-Oriented Software Development.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 11: Classes and Data Abstraction
A function (procedure) code to perform a task (carry out an algorithm) Return_type Func(parameters) ---- Func (arguments) --- Return value.
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
1 Chapter 14-2 Object- Oriented Software Development Dale/Weems.
1 Structured Types, Data Abstraction and Classes.
1 Chapter 14 Object- Oriented Software Development Dale/Weems.
1 Chapter 14 Object-Oriented Software Development Dale/Weems/Headington.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
1 Chapter 14 Object-Oriented Software Development Dale/Weems.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Data Structures and Algorithms Week 3 Data Abstraction: Part II Structured Types, and Classes.
Classes Structured Programming 256 Chapter 8 Classes - Part I OOP & Class Object Terminology File Management.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
1 Structural Types, Data Abstractions and Classes.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
1 Final Exam Tues 3/16/10 (2-3:50) (Same classroom) Old Textbook - Chapters 11-16, 18 Focus is on 15, 16 and 18 Final Exam Tues 3/16/10 (2-3:50) (Same.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
1 Review: C++ class represents an ADT 2 kinds of class members: data members and function members Class members are private by default Data members are.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 12: Classes and Data Abstraction.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 11: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
1 Structured Types, Data Abstraction and Classes.
Chapter 12 Classes and Abstraction
Procedural and Object-Oriented Programming
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Dale/Weems/Headington
C++ Data Types and Data Abstractions
Review: Two Programming Paradigms
About the Presentations
Introduction to Classes
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter Structured Types, Data Abstraction and Classes
C++ Data Types and Data Abstractions
Introduction to Structured Data Types and Classes
CS148 Introduction to Programming II
Introduction to Classes
Classes and Data Abstraction
Chapter 14 Object-Oriented Software Development
Review: C++ class represents an ADT
Presentation transcript:

1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington

2 Chapter 11 Topics l Meaning of a Structured Data Type Declaring and Using a struct Data Type C++ union Data Type l Meaning of an Abstract Data Type Declaring and Using a class Data Type l Using Separate Specification and Implementation Files Invoking class Member Functions in Client Code C++ class Constructors

3 Abstraction l is the separation of the essential qualities of an object from the details of how it works or is composed l focuses on what, not how l is necessary for managing large, complex software projects

4 Control Abstraction l separates the logical properties of an action from its implementation. Search (list, item, length, where, found);. l the function call depends on the function’s specification (description), not its implementation (algorithm)

5 Data Abstraction l separates the logical properties of a data type from its implementation LOGICAL PROPERTIESIMPLEMENTATION What are the possible values?How can this be done in C++? What operations will be needed?How can data types be used?

6 Data Type set of values (domain) allowable operations on those values FOR EXAMPLE, data type int has domain operations +, -, *, /, %, >>, <<

7 Abstract Data Type (ADT) l a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how) FOR EXAMPLE...

8 ADT Specification Example TYPE TimeType DOMAIN Each TimeType 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

9 Another ADT Specification TYPE ComplexNumberType 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

10 ADT Implementation means l choosing a specific data representation for the abstract data using data types that already exist (built-in or programmer- defined) l writing functions for each allowable operation

Several Possible Representations of TimeType 3 int variables 3 strings 3-element int array l actual choice of representation depends on time, space, and algorithms needed to implement operations “10” “45” “27”

12 Some Possible Representations of ComplexNumberType struct with 2 float members 2-element float array real.imag

13 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long double

14 class TimeType Specification // SPECIFICATION FILE( timetype.h ) class TimeType// declares a class data type {// does not allocate memory public : // 5 public function members void Set ( int hours, int mins, int secs ) ; void Increment ( ) ; void Write ( ) const ; bool Equal ( TimeType otherTime ) const ; bool LessThan ( TimeType otherTime ) const ; private :// 3 private data members int hrs ; int mins ; int secs ; } ; 14

15 Use of C++ data Type class l facilitates re-use of C++ code for an ADT l software that uses the class is called a client l variables of the class type are called class objects or class instances l client code uses public member functions to handle its class objects

16 Client Code Using TimeType #include “timetype.h” // includes specification of the class using namespace std ; int main ( ) { TimeType currentTime ; // declares 2 objects of TimeType TimeType endTime ; bool done = false ; currentTime.Set ( 5, 30, 0 ) ; endTime.Set ( 18, 30, 0 ) ; while ( ! done ) {... currentTime.Increment ( ) ; if ( currentTime.Equal ( endTime ) ) done = true ; } ; } 16

17 class type Declaration The class declaration creates a data type and names the members of the class. It does not allocate memory for any variables of that type! Client code still needs to declare class variables.

18 C++ Data Type class represents an ADT l 2 kinds of class members: data members and function members l class members are private by default l data members are generally private l function members are generally declared public l private class members can be accessed only by the class member functions (and friend functions), not by client code.

19 Aggregate class Operations l built-in operations valid on class objects are: 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 l other operations can be defined as class member functions

20 2 separate files Generally Used for class Type // SPECIFICATION FILE ( timetype.h ) // Specifies the data and function members. class TimeType { public:... private:... } ; // IMPLEMENTATION FILE ( timetype.cpp ) // Implements the TimeType member functions....

21 Implementation File for TimeType // IMPLEMENTATION FILE ( timetype.cpp ) // Implements the TimeType member functions. #include “ timetype.h”// also must appear in client code #include... bool TimeType :: Equal ( /* in */ TimeType otherTime ) const // Postcondition: // Function value == true, if this time equals otherTime // == false, otherwise { return ( (hrs == otherTime.hrs) && (mins == otherTime.mins) && (secs == otherTime.secs) ) ; }...

22 Familiar Class Instances and Function Members l the member selection operator (. ) selects either data members or function members l header files iostream and fstream declare the istream, ostream,and ifstream, ofstream I/O classes l both cin and cout are class objects and get and ignore are function members cin.get (someChar) ; cin.ignore (100, ‘\n’) ; l these statements declare myInfile as an instance of class ifstream and invoke function member open ifstream myInfile ; myInfile.open ( “A:\\mydata.dat” ) ;

23 Information Hiding Class implementation details are hidden from the client’s view. This is called information hiding. Public functions of a class provide the interface between the client code and the class objects. client code specificationimplementation abstraction barrier

24 Scope Resolution Operator ( :: ) l C++ programs typically use several class types l different classes can have member functions with the same identifier, like Write( ) l member selection operator is used to determine the class whose member function Write( ) is invoked currentTime.Write( ) ;// class TimeType numberZ.Write( ) ;// class ComplexNumberType l in the implementation file, the scope resolution operator is used in the heading before the function member’s name to specify its class void TimeType :: Write ( ) const {... }

25 TimeType Class Instance Diagrams Private data: hrs mins secs Set Increment Write LessThan Equal Private data: hrs mins secs Set Increment Write LessThan Equal currentTime endTime

26 Use of const with Member Functions when a member function does not modify the private data members, use const in both the function prototype (in specification file) and the heading of the function definition (in implementation file)

27 Example Using const with a Member Function void TimeType :: Write ( ) const // Postcondition: Time has been output in form HH:MM:SS { if ( hrs < 10 ) cout << ‘0’ ; cout << hrs << ‘:’ ; if ( mins < 10 ) cout << ‘0’ ; cout << mins << ‘:’ ; if ( secs < 10 ) cout << ‘0’ ; cout << secs ; } 27

28 Separate Compilation and Linking of Files timetype.h client.cpptimetype.cpp client.obj client.exetimetype.obj Compiler Linker #include “timetype.h” implementation file specification file main program

29 l often several program files use the same header file containing typedef statements, constants, or class type declarations--but, it is a compile-time error to define the same identifier twice l this preprocessor directive syntax is used to avoid the compilation error that would otherwise occur from multiple uses of #include for the same header file #ifndef Preprocessor_Identifier #define Preprocessor_Identifier. #endif Avoiding Multiple Inclusion of Header Files

30 Example Using Preprocessor Directive #ifndef // timetype.h FOR COMPILATION THE CLASS DECLARATION IN // SPECIFICATION FILE FILE timetype.h WILL BE INCLUDED ONLY ONCE #ifndef TIME_H #define TIME_H // timetype.cpp // client.cpp // IMPLEMENTATION FILE // Appointment program class TimeType { #include “timetype.h” #include “timetype.h” public: int main ( void ) { private: } } ; #endif

31 Class Constructors l a class constructor is a member function whose purpose is to initialize the private data members of a class object l the name of a constructor is always the name of the class, and there is no return type for the constructor l a class may have several constructors with different parameter lists. A constructor with no parameters is the default constructor l a constructor is implicitly invoked when a class object is declared--if there are parameters, their values are listed in parentheses in the declaration

32 Specification of TimeType Class Constructors class TimeType// timetype.h { public : // 7 function members void Set ( int hours, int minutes, int seconds ) ; void Increment ( ) ; void Write ( ) const ; bool Equal ( TimeType otherTime ) const ; bool LessThan ( TimeType otherTime ) const ; TimeType ( int initHrs, int initMins, int initSecs ) ; // constructor TimeType ( ) ; // default constructor private :// 3 data members int hrs ; int mins ; int secs ; } ; 32

33 Implementation of TimeType Default Constructor TimeType :: TimeType ( ) // Default Constructor // Postcondition: // hrs == 0 && mins == 0 && secs == 0 { hrs = 0 ; mins = 0 ; secs = 0 ; } 33

34 Implementation of Another TimeType Class Constructor TimeType :: TimeType ( /* in */ int initHrs, /* in */ int initMins, /* in */ int initSecs ) // Constructor // Precondition: 0 <= initHrs <= 23 && 0 <= initMins <= 59 // 0 <= initSecs <= 59 // Postcondition: //hrs == initHrs && mins == initMins && secs == initSecs { hrs = initHrs ; mins = initMins ; secs = initSecs ; } 34

35 Automatic invocation of constructors occurs TimeType departureTime ; // default constructor invoked TimeType movieTime (19, 30, 0 ) ; // parameterized constructor departureTime movieTime Private data: hrs mins secs Set Increment Write LessThan Equal Private data: hrs mins secs Set Increment Write LessThan Equal