Lecture 2 – Abstract Data Type (ADT)

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Classes. COMP104 Lecture 25 / Slide 2 Motivation  Types such as int, double, and char are simple objects. * They can only answer one question: “What.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Software Testing and Quality Assurance
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
CS 201 Functions Debzani Deb.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
1 Building Classes (the "++" in C++) (Chapter 14) Representing More Complex Objects.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Unit 1 - Introducing Abstract Data Type (ADT) Part 1.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Class Operations Creating New Types. Review Last time, we began building, a class to allow us to model temperatures: Last time, we began building Temperature,
Unit 1 - Introducing Abstract Data Type (ADT) Part 3 – Slides 24 to end.
Prof. I. J. Chung Data Structure #1 Professor I. J. Chung.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
1 More About Classes – Instance Methods Chap.6 Study Sections 6.1 – 6.4 Representing More Complex Objects.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Chapter 12 Classes and Abstraction
Andrew(amwallis) Classes!
More Sophisticated Behavior
Topic: Programming Languages and their Evolution + Intro to Scratch
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Chapter 7: Introduction to Classes and Objects
Data Abstraction: The Walls
Review: Two Programming Paradigms
CO1401 Programming Design and Implementation
Topic: Functions – Part 2
Introduction to Classes
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
CS212: Object Oriented Analysis and Design
Introduction to Classes
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Object-Oriented Programming
Object-Oriented Programming Using C++ Second Edition
Chapter 9 Objects and Classes
Introduction to Classes and Objects
Object-Oriented Programming
Classes and OOP.
Object-Oriented Programming
CS410 – Software Engineering Lecture #5: C++ Basics III
Review of Previous Lesson
CS2013 Lecture 7 John Hurley Cal State LA.
Data Structures and ADTs
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
Lecture 9 Concepts of Programming Languages
CMPT 225 Lecture 5 – linked list.
Lecture 3 – Data collection List ADT
Lecture 4 – Data collection List ADT
CMPT 225 Lecture 7 – Stack.
Introduction to Classes and Objects
Presentation transcript:

Lecture 2 – Abstract Data Type (ADT) CMPT 225 Lecture 2 – Abstract Data Type (ADT)

Learning Outcomes At the end of this lecture, a student will be able to: Define abstraction, information hiding and "abstract data type“ (ADT) Write C++ code Encapsulate methods and variables for an ADT into a C++ class Differentiate between a class that has been designed as an ADT and a class that has not Compare and contrast them

Today’s menu Abstract Data Type (ADT) Example: Temperature class Definition + “Wall” metaphor How to design an ADT How to implement an ADT in C++ How to test an ADT Example: Temperature class Implemented as an ADT Implemented as a non-ADT Compare both implementations

Let’s start with a problem Problem Statement Create a temperature conversion application Step 1 – Understanding the problem What do we do in this step? Step 2 – Design Solution Step 3 – Implement Solution Step 4 – Testing solution

Why is Step 1 is important?

Step 2 – Design solution UML Class Diagram Temperature degrees scale Class Name degrees scale Attributes Operations inFahrenheit( ) inCelsius( ) raise( toWhat ) Exclude constructors, destructors, getters/setters. Why? Because we assume that we will develop these methods for our classes, if they are needed.

Step 3 – Implement solution /* Header Comment Block */ /* Class Definition */ class Temperature { private: double myDegrees; // >= ABSOLUTE_ZERO for myScale char myScale; // 'F' or 'C' bool isValidTemperature( const double degrees, const char scale ); public: }; Header file: Temperature.h Preprocessor and using directives See next slide

Step 3 – Implement solution – cont’d ... public: // Constructors Temperature(); Temperature(double degrees, char scale ); // Getters double getDegrees( ) const; char getScale( ) const; // Setters void setDegrees( const double degrees ); // Application-related methods Temperature inFahrenheit( ) const; Temperature inCelsius( ) const; void raise( const double amount ); }; Constants

Step 3 – Implement solution /* Header Comment Block */ Temperature::Temperature():myDegrees(0.0),myScale('C'){} ... Temperature Temperature::inFahrenheit( ) const { Temperature result; if ( myScale == 'F' ) result = Temperature( myDegrees, 'F' ); else if ( myScale == 'C' ) result = Temperature(myDegrees * 1.8 + 32.0, 'F'); return result; } ... Implementation file: Temperature.cpp Preprocessor and using directives

Step 3 – Implement solution /* Header Comment Block */ int main() { // Display menu // 1. Convert Celsius temperature to Fahrenheit // 2. Convert Fahrenheit temperature to Celsius // Read user choice // Ask user for and read temperature degrees // Create a temperature object of required scale // Convert this temperature object to // temperature object of other scale // Display resulted conversion // Repeat the above until user quits return 0; } TemperatureConverter.cpp Preprocessor and using directives

Step 4 – Testing Temperature class /* Header Comment Block */ int main() { // Create a valid Celsius temperature // Create an invalid Celsius temperature // Create a valid Fahrenheit temperature // Create an invalid Fahrenheit temperature // Converting a valid Celsius temperature // to a Fahrenheit temperature // Raising a valid Celsius temperature to // an invalid amount of degrees ... return 0; } TemperatureTestDriver.cpp Preprocessor and using directives Goals of Test Driver: 1. To test each method of Temperature class by calling it at least once 2. To “break” the code, i.e., calling each method with valid and invalid parameters.

What makes this class an ADT? So, what is an ADT (abstract data type)?

Abstraction – in the real world From the Latin abs, meaning away from  and trahere, meaning to draw “Process of taking away or removing characteristics from something in order to reduce it to a set of essential characteristics” Examples: Car Map Source: https://whatis.techtarget.com/definition/abstraction

Abstraction and information hiding – in the software world We hide the attributes (data members) by listing them as We achieve abstraction by hiding information from “public view” -> from other classes (i.e., client code, test drivers) We separate the purpose (what) of a class from its implementation (how) and hide the latter (i.e., class’ data members and the implementation of its methods) This way, client code can use the class without knowing its implementation (only knowing the class’ interface) This reduces complexity and allows for easy modification And this is how we construct an ADT private within the header: .h file We hide the implementation of its methods by putting them in a separate file: .cpp file public

When Temperature class implemented as an ADT Temperature’s “private” section hidden behind the wall Client code must use Temperature’s public methods (slits in the wall) to access Temperature’s private section Temperature Converter.cpp (Client Code) Temperature class

Why should we hide a class’ implementation? /* Header Comment Block */ /* Class Definition */ class Temperature { public: double myDegrees; // >= ABSOLUTE_ZERO for myScale char myScale; // 'F' or 'C' ... Header file: Temperature.h Preprocessor and using directives Constants

Why should we hide a class’ implementation? cont’d ... // Constructors Temperature(); Temperature(double degrees, char scale ); // Application-related methods Temperature inFahrenheit( ) const; Temperature inCelsius( ) const; void raise( const double amount ); bool isValidTemperature( const double degrees, const char scale ); };

Step 4 – Test solution - Demo Create a valid Fahrenheit temperature -> testing Temperature( 32.0, 'F' ) Actual Result: 'tempFahr' -> 32 degree F Changing the amount of degrees of 'tempFahr' to -976.02F by setting 'myDegrees' to -976.02 directly. Actual Result: 'tempFahr' -> -976.02 degree F

When Temperature class not implemented as an ADT Client code can tamper with the class’ attributes (data members), hence breaking the class invariant In our Temperature class example, client code can break Temperature class’ invariant: Actual Result: 'tempFahr' -> -976.02 degree F Temperature Converter.cpp (Client Code) Temperature class myDegrees myScale

Advantages and disadvantages of ADT Preserve/control the integrity of a class’ data (expressed as invariant of a class) Disadvantages: More code to write: must have getters/setters methods

√ Learning Check We can now … describe what happens in the 4 steps of the software development process: Step 1 - Problem statement Step 2 – Design Step 3 – Implementation Step 4 - Testing construct an ADT class in C++ define abstract data type (ADT), abstraction and information hiding differentiate between a class that has been designed/implemented as an ADT and a class that has not list some of the advantages and disadvantages of ADT

Next Lecture Introduce our first data structure: List Design a List class as an ADT Implement it using an array