Data Abstraction. Andrew DeOrio. andrewdeorio.com (3,4,5) (-3,4,5)

Similar documents
VIRTUAL FUNCTIONS Chapter 10

TDDE18 & 726G77. Functions

CS Homework 10 p. 1. CS Homework 10

Object Oriented Software Design II

Object Oriented Software Design II

Problem Solving with C++

Absolute C++ Walter Savitch

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Ch. 12: Operator Overloading

Java Object Oriented Design. CSC207 Fall 2014

What does it mean by information hiding? What are the advantages of it? {5 Marks}

Midterm Exam 5 April 20, 2015

Do not write in this area A.1 A.2 A.3 A.4 A.5 A.6 B.1 B.2 B.3 TOTAL. Maximum possible points:

LECTURE 5 Control Structures Part 2

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

VALLIAMMAI ENGINEERING COLLEGE

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

Instantiation of Template class

Polymorphism. Arizona State University 1

Lecture 5: Inheritance

Polymorphism. VII - Inheritance and Polymorphism

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Data Structures and Other Objects Using C++

Object-Oriented Principles and Practice / C++

Semantic Analysis. Lecture 9. February 7, 2018

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Inheritance, and Polymorphism.

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Polymorphism. Zimmer CSCI 330

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

22. Subtyping, Inheritance and Polymorphism

Data Abstraction. Hwansoo Han

CMSC202 Computer Science II for Majors

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Polymorphism Part 1 1

Formal Methods for Java

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

CS 162, Lecture 25: Exam II Review. 30 May 2018

Object-Oriented Programming

Chapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved

C++ Programming: Polymorphism

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Chapter 16. Templates. Copyright 2010 Pearson Addison-Wesley. All rights reserved

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Programming, numerics and optimization

Object Oriented Programming. Solved MCQs - Part 2

Special Member Functions

CSE 331 Final Exam 3/16/15 Sample Solution

Software Design and Analysis for Engineers

CE221 Programming in C++ Part 1 Introduction

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CS115 - Module 4 - Compound data: structures

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

EL2310 Scientific Programming

CS102 C++ Exception Handling & Namespaces

COMP322 - Introduction to C++

CSCI-1200 Data Structures Fall 2017 Lecture 25 C++ Inheritance and Polymorphism

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

PROGRAMMING IN C AND C++:

G Programming Languages - Fall 2012

Topic 7: Algebraic Data Types

C++ Inheritance and Encapsulation

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

void fun() C::C() // ctor try try try : member( ) catch (const E& e) { catch (const E& e) { catch (const E& e) {

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Testing, Debugging, and Verification

CS 3 Introduction to Software Engineering. 3: Exceptions

Looping and Counting. Lecture 3. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017


Assertions, pre/postconditions

More class design with C++ Starting Savitch Chap. 11

Conformance. Object-Oriented Programming Spring 2015

Classes Ch

C++_ MARKS 40 MIN

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

September 19,

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

1 Short Answer (8 Points Each)

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

Midterm Exam (REGULAR SECTION)

+2 Volume II OBJECT TECHNOLOGY OBJECTIVE QUESTIONS R.Sreenivasan SanThome HSS, Chennai-4. Chapter -1

III. Classes (Chap. 3)

Chapter 4 Defining Classes I

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

CSS 343 Data Structures, Algorithms, and Discrete Math II. Polymorphism. Yusuf Pisan

Unit 7. 'while' Loops

CS 247: Software Engineering Principles. ADT Design

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Transcription:

Data Abstraction Andrew DeOrio awdeorio@umich.edu andrewdeorio.com (3,4,5) 5 (4,5,9) (4,5,3) 3 4 (-3,4,5)

Review: compound types A compound type binds together several other types into one new type In C++, we can create a compound type using a class class Triangle { public: double a, b, c; //edge lengths ; a, b, and c are called member data 4

Review: member functions class Triangle { public: double a, b, c; //edge lengths double area() { //compute area double s = (a+b+c)/2; double a = sqrt(s*(s-a)*(s-b)*(s-c)); return a; ; area = In addition to data, a class can contain member functions Because member functions are within the same scope as member data, they have access to the member data directly Heron s formula s = a + b + c 2 s(s - a)(s - b)(s -c) 5

Review: constructors class Triangle { public: double a, b, c; //edge lengths double area() {/*...*/ Triangle(double a_in, double b_in, double c_in) { a = a_in; b = b_in; c = c_in; ; Member data can be initialized using a constructor 6

Review: using classes class Triangle {/*...*/; int main() { Triangle t(3,4,5); cout << t.area() << "\n"; Users of a class can call member functions $ g++ test.cpp $./a.out area = 6 7

Abstraction in computer programs Procedural abstraction lets us separate what a procedure does from how it is implemented In C++, we use functions to implement procedural abstraction For example: //returns n!, requires that n >= 0 int factorial(int n); int factorial (int n) { if (n == 0) return 1; return n*factorial(n-1); int factorial(int n) { int result = 1; while (n!= 0) { result *= n; n -= 1; return result; 8

Abstraction in computer programs Data abstraction lets us separate what a type is (and what it can do) from how the type is implemented In C++, we use a class to implement data abstraction We can create an Abstract Data Type (ADT) using a class ADTs let us model complex phenomena More complex than built-in data types like int, double, etc. ADTs make programs easier to maintain and modify You can change the implementation and no users of the type can tell 9

Creating our ADT Let s build on our triangle compound data type to make it an Abstract Data Type We will write an abstract description of values and operations What the data type does, but not how Triangle.h Next, we will implement the ADT How the data type works Triangle.cpp Finally we will use our new ADT Graphics.cpp 10

Creating our ADT What if we have two programmers? Alice and Bob agree on an abstraction Triangle.h Alice codes Triangle.cpp Implements ADT Triangle.cpp Bob codes Graphics.cpp Uses ADT Graphics.cpp Cartoon credit: xkcd.com 11

Triangle.h Triangle ADT class Triangle { ; //a geometric representation of a triangle //... Put only the class declaration (no implementations) in the file Triangle.h This file contains the abstraction We will add operations to Create a triangle Print some information (helpful for debugging) Calculate the area 12

Triangle.h Triangle ADT class Triangle { //a geometric representation of a triangle public: ; //Creates a triangle from edge lengths //Requires that a, b, c are non-negative and //form a triangle Triangle(double a_in, double b_in, double c_in); A constructor initializes member variables when a Triangle is created 13

Triangle.h Triangle ADT class Triangle { //a geometric representation of a triangle public: Triangle(double a_in, double b_in, double c_in); //Prints edge lengths void print(); //Returns triangle area double area(); ; Add member functions to print edges and compute area 14

What vs. How We now have an abstract description of values and operations What the data type does Triangle.h Now, we need to implement this ADT How the data type works Member variables go in Triangle.h Member function implementations go in Triangle.cpp Triangle.cpp 15

Triangle.h Triangle ADT class Triangle { //a geometric representation of a triangle public: Triangle(double a_in, double b_in,double c_in); void print(); double area(); //edges are non-negative and form a triangle double a, b, c; ; Add member variables 16

Triangle.cpp Triangle ADT Function implementations go in Triangle.cpp Users of the Triangle ADT do not need to see this file! #include "Triangle.h" tells the compiler to copypaste Triangle.h at the top of this file #include "Triangle.h 17

Triangle.cpp Triangle ADT Triangle::Triangle(double a_in, double b_in, double c_in){ a = a_in; b = b_in; c = c_in; Constructor initializes member variables when a triangle is created :: is the scope resolution operator Tells compiler this is a member function inside the Triangle class s scope 18

Representation invariant Triangle::Triangle(double a_in, double b_in, double c_in){ a = a_in; b = b_in; c = c_in; Recall that member variables are a class s representation The description of how a member variable should behave is the representation invariant class Triangle { //... //edges are non-negative and form a triangle double a, b, c; ; Triangle.cpp Triangle.h 19

Representation invariant #include "Triangle.h" int main() { Triangle t(3,4,5); 4 3 5 // later in the program... t.c = 9; What is wrong with this code? 20

The problem with public #include "Triangle.h" int main() { Triangle t(3,4,5); 4 3 5 // later in the program... t.c = 9; Problem: class s internal representation of a triangle is no longer a triangle! We have violated the representation invariant 4 9 3 21

Representation invariant Representation type (a,b,c) Abstract type (4,5,9) (3,4,5) (4,5,3) 3 5 4 (-3,4,5) representation invariant 22

Solution: private class Triangle { //... private: //edges are non-negative and form a triangle double a, b, c; ; An ADT s member variables should be private This is an aspect of information hiding int main() { Triangle t(3,4,5); t.c = 9; //compiler error 23

Representation invariant #include "Triangle.h" int main() { Triangle t(3,4,9); //... Same problem, this time in the constructor! 24

Recall assert() assert() is a programmer s friend for debugging Does nothing if expression is true Exits and prints an error message if expression is false We can assert that the representation invariant is true #include <cassert> Triangle.cpp Triangle::Triangle(double a_in, double b_in, double c_in){ a = a_in; b = b_in; c = c_in; assert(expression); 25

Exercise Write an assertion that checks the representation invariant Edges are non-negative and form a triangle Recall: the sum of the lengths of any two sides of a triangle always exceeds the length of the third side #include <cassert> Triangle::Triangle(double a_in, double b_in, double c_in){ a = a_in; b = b_in; c = c_in; assert(expression); Triangle.cpp 26

Solution Write an assertion that checks the representation invariant Edges are non-negative and form a triangle Recall: the sum of the lengths of any two sides of a triangle always exceeds the length of the third side #include <cassert> Triangle.cpp Triangle::Triangle(double a_in, double b_in, double c_in){ a = a_in; b = b_in; c = c_in; assert( (a + b > c) && (a + c > b) && (b + c > a) ); 27

Triangle.cpp Triangle ADT //... double Triangle::area() { double s = (a + b + c) / 2; return sqrt(s*(s-a)*(s-b)*(s-c)); void Triangle::print() { cout << "a=" << a << " b=" << b << " c=" << c << "\n"; Implementations for area() and print() 28

Using our ADT We now have an abstract description of values and operations What the data type does, but not how Triangle.h We have an implementation of this ADT How the data type works Triangle.cpp Now, let s use our new ADT Graphics.cpp Cartoon credit: xkcd.com 29

Graphics.cpp A use for triangles In computer graphics, 3D surfaces can be modeled using connected triangles, called a triangle mesh Let s calculate the area of this surface Image: wikipedia.org 31

Graphics.cpp Triangle ADT #include "Triangle.h" int main() { const int SIZE = 3; Triangle mesh[size]; // fill with triangles... double area = 0; for (int i=0; i<size; ++i) { area += mesh[i].area(); cout << "total area = " << area << "\n"; $ g++ Graphics.cpp Triangle.cpp $./a.out total area = 22.3196 32

Exercise θ There is more than one way to represent a triangle a Let's change our representation from 3 edges to 2 edges and an angle: a, b, and theta Do we need to change what our ADT does? Do we need to change how our ADT does it? Do we need to change anything in Triangle.h? What? Do we need to change anything in Triangle.cpp? What? Do we need to change anything in Graphics.cpp? What? Will Alice, Bob or both need to change their code? b 33

Solution b θ a Do we need to change what our ADT does? No, don't touch public function inputs or outputs Do we need to change how our ADT does it? Yes, because internal representation is different now 34

Solution Do we need to change anything in Triangle.h? What? Yes. Only the private member variables class Triangle { //... private: //edges a and b are separated by angle theta //and form a triangle double a, b; //edges double theta; //angle ; b Triangle.h θ a 35

Solution Do we need to change anything in Triangle.cpp? What? Yes. The function implementations change. b θ a Triangle::Triangle(double a_in, double b_in, double c_in) { a = a_in; b = b_in; assert(/*...*/); theta = acos((a*a + b*b c_in*c_in) / (2*a*b)); Law of cosigns arccos( a 2 b 2 2ab c 2 ) b θ c a 36

Solution Do we need to change anything in Triangle.cpp? What? Yes. The function implementations change. b θ a Triangle::print() { double c = sqrt(a*a + b*b + 2*a*b*cos(theta)); cout << "a=" << a << " b=" << b << " c=" << c << "\n"; Law of cosigns c a 2 b 2 2ab cos b θ c a 37

Abstraction exercise Do we need to change anything in Graphics.cpp? What? No! That's the cool part Will Alice, Bob or both need to change their code? Just Alice. b θ a int main() { //... Graphics.cpp double area = 0; for (int i=0; i<size; ++i) { area += mesh[i].area(); cout << "total area = " << area << "\n"; 38

The power of abstraction b θ a We changed the implementation, but not the abstraction Modified private member variables Modified public function implementations We changed how the abstract data type worked We did not change what the abstract data type did Because the abstraction remained the same, our old code that used the abstract data type still worked This is especially important when you have many people working on one project This is a big benefit of ADTs! 39

Building on ADTs The next few lectures will build on abstract data types Subtypes and Polymorphism Using C++ derived types (AKA inheritance) to create hierarchies of Abstract Data Types Interfaces Using C++ pure virtual functions to omit the member variables Container Abstract Data Types 40