Mastering Data Abstraction or Get nit-picky on design advantages

Similar documents
Arizona s First University. More ways to show off--controlling your Creation: IP and OO ECE 373

QUIZ Friends class Y;

Functions, Pointers, and the Basics of C++ Classes

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

PIC 10A Objects/Classes

CSE 303: Concepts and Tools for Software Development

G52CPP C++ Programming Lecture 13

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Operator overloading

Object Reference and Memory Allocation. Questions:

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Ch. 11: References & the Copy-Constructor. - continued -

Short Notes of CS201

Fast Introduction to Object Oriented Programming and C++

G52CPP C++ Programming Lecture 9

CS201 - Introduction to Programming Glossary By

C++ for Java Programmers

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

The Stack, Free Store, and Global Namespace

These are notes for the third lecture; if statements and loops.

CS102 C++ Exception Handling & Namespaces

Lecture 14: more class, C++ streams

CSE 333 Midterm Exam July 24, Name UW ID#

Introduction Of Classes ( OOPS )

pointers + memory double x; string a; int x; main overhead int y; main overhead

Software Engineering /48

CS24 Week 3 Lecture 1

Pointers and References

QUIZ. What are 3 differences between C and C++ const variables?

CSE 333 Midterm Exam 7/25/16. Name UW ID#

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

AIMS Embedded Systems Programming MT 2017

Implementing an ADT with a Class

Item 4: Extensible Templates: Via Inheritance or Traits?

Lecture 13: more class, C++ memory management

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

Lab 2: ADT Design & Implementation

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Ch. 12: Operator Overloading

6. Pointers, Structs, and Arrays. 1. Juli 2011

Variables. Data Types.

CS201 Some Important Definitions

CA31-1K DIS. Pointers. TA: You Lu

QUIZ. What is wrong with this code that uses default arguments?

III. Classes (Chap. 3)

CSE 333 Final Exam 3/19/14

Absolute C++ Walter Savitch

ALL ABOUT POINTERS C/C++ POINTERS

Inheritance, and Polymorphism.

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Programming Abstractions

CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

Reliable C++ development - session 1: From C to C++ (and some C++ features)

A brief introduction to C++

1: Introduction to Object (1)

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Introducing C++ to Java Programmers

QUIZ. Source:

CSE 374 Final Exam Sample Solution 3/17/11

Object-Oriented Programming, Iouliia Skliarova

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Copying Data. Contents. Steven J. Zeil. November 13, Destructors 2

Lecturer: William W.Y. Hsu. Programming Languages

Object-Oriented Programming for Scientific Computing

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

MODERN AND LUCID C++ ADVANCED

Lecture 7. Log into Linux New documents posted to course webpage

04-19 Discussion Notes

Polymorphism Part 1 1

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Class Destructors constant member functions

Separate Compilation of Multi-File Programs

a data type is Types

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

Object Oriented Design

A student was asked to point out interface elements in this code: Answer: cout. What is wrong?

2.1 Introduction UML Preliminaries Class diagrams Modelling delegation... 4

CS Lecture #14

Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370

GEA 2017, Week 4. February 21, 2017

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Homework 6. Reading. Problems. Handout 7 CS242: Autumn November

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

C++ Constructor Insanity

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Programming in C/C Lecture 3

Transcription:

Arizona s First University. Mastering Data Abstraction or Get nit-picky on design advantages Luke: Your not my father! Vader: You mean, You re not my father Luke: What? Vader: You used the possessive, not the contraction of `you are Luke: Uggh! You don t understand me! I ll be in my room!!!

Overview A little review Data vs. Abstraction Implementation details The Handle Why recompile? Object creation control Implementation alt s Temporary objects Examining cost Proxies Component construction

A little review We have been learning how to create the architecture of software Concepts have been examined Inheritance Friends Operators Generic Programming Concepts have been applied Mmmmmmm, homework

Data What is data?

Abstraction? And what, then, is abstraction?

Abstraction vs. Data Well, here s what I think Abstraction is making something into something that it isn t Data is something that is All models are wrong, some models are useful. - George Box Therefore, data abstraction is turning something into nothing that isn t anything, or something Seriously you guys, Data abstraction is the supreme control of data A protective shield over control of data

Data abstraction Let s consider: You are designing a software system You go to your customer s office Q: What are the first words out of your mouth? a) Hey you guys, let me show you the syntax for these operators! b) You are not gonna believe how I implemented this bubble sort c) I just finally understood what protected access regions do!

Data abstraction Why do your clients care? They don t They are programmers, and your software system is actually a header file they are including Data abstraction is for the programmer, not the end user All your end user wants is functionality Your job is to provide that functionality This third sentence contains the word functionality

Use cases Compiler Uses headers Uses implementation Customer Uses compiled implementation Side-car programmer Uses headers Links in implementation using the compiler

Does size matter? Whatsa matta here? // Author: Jonathan Sprinkle // size0.h #include <cstdio> #include <string> class John; class Sherlock; class John public:! John( ) }! ~John( ) } protected:! Sherlock crap; }; #include "size0.h" int main( void )! John j;! Sherlock s;! return 0; } class Sherlock public:! Sherlock( ) }! ~Sherlock( ) } protected:! John john; };

Wait for it...wait... for... It... Scanning dependencies of target lec22-size0 [ 98%] Building CXX object src/lectures/lec22/cmakefiles/lec22-size0.dir/size0.cpp.o In file included from /Users/sprinkle/work/teaching/ece373/src/lectures/lec22/size0.cpp:1: /Users/sprinkle/work/teaching/ece373/src/lectures/lec22/size0.h:15: error: field crap has incomplete type make[2]: *** [src/lectures/lec22/cmakefiles/lec22-size0.dir/size0.cpp.o] Error 1 make[1]: *** [src/lectures/lec22/cmakefiles/lec22-size0.dir/all] Error 2 make: *** [all] Error 2 * * roughly translated from the French, No Pipe, Sherlock

Can you hear me now? // Author: Jonathan Sprinkle // size_1.h #include <cstdio> #include <string> class John; class Sherlock; class John public:! John( ) }! ~John( ) } protected:! Sherlock* crap; }; #include "size1.h" int main( void )! John j;! Sherlock s;! return 0; } class Sherlock public:! Sherlock( ) }! ~Sherlock( ) } protected:! John john; };

Good! Scanning dependencies of target lec22-size1 [100%] Building CXX object src/lectures/lec22/cmakefiles/lec22-size1.dir/size1.cpp.o Linking CXX executable lec22-size1 [100%] Built target lec22-size1

gears.insert( *monkeywrench ) // Author: Jonathan Sprinkle // size2.h #include <cstdio> #include <string> class John; class Sherlock; class John public:! John( ) }! ~John( ) } protected:! Sherlock& crap; }; #include "size2.h" int main( void )! John j;! Sherlock s;! return 0; } class Sherlock public:! Sherlock( ) }! ~Sherlock( ) } protected:! John john; };

cookies->toss( all ) Scanning dependencies of target lec22-size2 [100%] Building CXX object src/lectures/lec22/cmakefiles/lec22-size2.dir/ size2.cpp.o In file included from /Users/sprinkle/work/teaching/ece373/src/lectures/lec22/ size2.cpp:1: /Users/sprinkle/work/teaching/ece373/src/lectures/lec22/size2.h: In constructor John::John() : /Users/sprinkle/work/teaching/ece373/src/lectures/lec22/size2.h:12: error: uninitialized reference member John::crap make[2]: *** [src/lectures/lec22/cmakefiles/lec22-size2.dir/size2.cpp.o] Error 1 make[1]: *** [src/lectures/lec22/cmakefiles/lec22-size2.dir/all] Error 2 make: *** [all] Error 2

And now... // Author: Jonathan Sprinkle // size3.h #include <cstdio> #include <string> class John; class Sherlock; class John public:! John( ) crap = new Sherlock( ); }! ~John( ) } protected:! Sherlock* crap; }; #include "size3.h" int main( void )! John j;! Sherlock s;! return 0; } class Sherlock public:! Sherlock( ) }! ~Sherlock( ) } protected:! John john; };

Out-Smurfed again... Scanning dependencies of target lec22-size3 [100%] Building CXX object src/lectures/lec22/cmakefiles/lec22-size3.dir/ size3.cpp.o In file included from /Users/sprinkle/work/teaching/ece373/src/lectures/lec22/ size3.cpp:1: /Users/sprinkle/work/teaching/ece373/src/lectures/lec22/size3.h: In constructor John::John() : /Users/sprinkle/work/teaching/ece373/src/lectures/lec22/size3.h:12: error: invalid use of incomplete type struct Sherlock /Users/sprinkle/work/teaching/ece373/src/lectures/lec22/size3.h:7: error: forward declaration of struct Sherlock make[2]: *** [src/lectures/lec22/cmakefiles/lec22-size3.dir/size3.cpp.o] Error 1 make[1]: *** [src/lectures/lec22/cmakefiles/lec22-size3.dir/all] Error 2 make: *** [all] Error 2

Why, why why? Why do we care about any of this? Can t we all just get along (and use pointers) to make our lives easier? What are the benefits/drawbacks of different types of containment? What effect will our decisions have on future versions of the tool?

Pointers and the easy life Pointers make life very nice They are always the same size (on the same machine) This will be very useful, as you will see, later They are always the same thing too (just a memory space) Pointers are so cool They do not require implementation to exist in a class (thus you can have classes contain each other) They oooze complexity you can show off to your friends at parties! Yeah!

Friends and Benefits Containment can be more complex than just pointers, though While it seems tempting to have all objects contained as pointers, you should consider carefully the ramifications of choosing a pointer over a value Some objects don t make sense as pointers (e.g., the int type) A little extra space is always taken up when you use a pointer (when you look globally at the system) Dereferencing all the frickin time can be expensive, depending on what your system is doing all the frickin time

Effects of our decisions When developing software, these words really suck: You must recompile your code to affect these changes Not only relink, but recompile Why would you ever need to recompile? Size of a class changes Interface to a class changes Won t the interface always change if the size does?

Size matters (again) A: well, not always // size_4.h #include <cstdio> #include <string> class John; class Sherlock; class John public: John( ) } ~John( ) } protected: Sherlock* crap; John* pilot2bomb; }; class Sherlock public: Sherlock( ) } ~Sherlock( ) } protected: John john; }; #include "size_4.h" int main( void ) John j; Sherlock s; return 0; } Sherlock s interface stayed exactly the same, surely I won t need to recompile it...

You will need to recompile... And stop calling me Shirley!

Stupid lousy compiler... // size_4.h #include <cstdio> #include <string> class John; class Sherlock; class John public: John( ) } ~John( ) } protected: Sherlock* crap; John* pilot2bomb; }; class Sherlock public: Sherlock( ) } ~Sherlock( ) } protected: John john; }; #include "size_4.h" int main( void ) John j; Sherlock s; return 0; } The compiler needs to know exactly how big to make the John object inside the Sherlock class. Whenever the size of John changes, the size of Sherlock changes.

But wait, I thought Isn t the idea of data abstraction being able to change one without changing the others? Isn t it supposed to be really cool to be able to provide different implementations without changing class definitions? Isn t it about time you asked your doctor about [latest pill for old people]?

Form from function: the handle We shall be changed. In a moment, in the twinkling of an eye -- The Trumpet Shall Sound, Messiah

Seriously, you guys Hiding the implementation of the class within another class is one way to preserve the interface and minimize recompilation required for client classes How does all this work?

#ifndef HANDEL_H #define HANDEL_H #include <cstdio> #include <string> using namespace std; #include "singers.h" class recitative3 public:!recitative3( ) }!~recitative3( ) }! string sing( ); private:! singer singer_; }; #include <iostream> #include <cstdlib> using namespace std; #include "singers.h" #include "handel.h" string recitative3::sing( )! return singer_.sing( ); } int main( void )! recitative3 levi;! cout << levi.sing( ) << endl;! return 0; } #endif 28

#ifndef SINGERS_H #define SINGERS_H #include <string> using namespace std; class SingerImpl; class singer public:!singer( );!~singer( );! string sing( ); protected:! SingerImpl* impl; }; #endif #include "singers.h" #include "singerimpls.h" #include <cstdlib> singer::singer( )! impl = new SingerImpl( "And he shall purify the sons of Levi" ); } singer::~singer( )! delete impl; } string singer::sing( )! return impl->sing( ); } 29

#ifndef SINGERIMPLS_H #define SINGERIMPLS_H #include <string> using namespace std; //#define CHANGED class SingerImpl #ifdef CHANGED public:!singerimpl( string _str, int _times=3 );!~SingerImpl( ) }! string sing( ); protected:! string str;! int times; #else public:!singerimpl( string _str );!~SingerImpl( );! string sing( ); protected:! string str; #endif }; #endif #include "singerimpls.h" #include <cstdlib> #ifdef CHANGED SingerImpl::SingerImpl( string _str, int _times ) : times( _times ), str( _str ) } string SingerImpl::sing( )! string result;! for( int i=0; i<times; i++ )!!! result += str;!! result += "\n";!}! return result; } #else SingerImpl::SingerImpl( string _str )!: str( _str ) } string SingerImpl::sing( )! return str; } #endif 30

Ahh, the memories What did we learn? Changes to the size of SingersImpl Had to recompile singersimpl.cpp singers.cpp Did not have to recompile handelsings.cpp even though it used the recitative class, which contained an object of type singer Changes to the implementation only of SingersImpl Have to recompile singersimpl.cpp only

Good vs. Evil Advantages of Handles Clients have to only re-link to object code (recitative class) Hides all implementation from the end user (good for proprietary code) Clients do not waste time performing recompiles Disadvantages No member functions of the class can be inline All interface classes have this extra impl pointer One extra level of indirection Cost of creating/destroying the handle Evil will always triumph because GOOD is DUMB -- Spaceballs

Release-to-release binary compatibility (RRBC) Using old executable files (dll s and exe s) when portions of the system have changed Objective Adding new member functions or data members should not result in a required recompile You must follow guidelines to do this: Existing class hierarchy cannot change New virtual function declarations must come after the old ones All old virtual functions must remain in the same order (and cannot be deleted) Previously existing public and protected functions may not be deleted The total size of a class instance must remain the same

RRBC (2) So, you have to keep everything you had before, but if you add something, the size has to remain the same? // rated_r.cpp #include <iostream> void main( void ) char q[5] = ; q[0] = 87; q[1] = 84; q[2] = 70; q[3] = 63; cout << q << endl; return; }

The Handle See, this is where the handles come in Put all of your changes in with this stuff that people never see Their size will always stay the same Our implementation can change as much as we like We can add member functions to our heart s content.

Object creation control Recall those famous lists from the beginning of the class Lists of pointers Restriction: DO NOT INSERT OBJECTS CREATED ON THE STACK Why was this? Because the objects get cleaned up when they move out of scope, and you re risking bad memory What did we say? It s hard to enforce that What is the truth? It s pretty easy to enforce that

How? Make the destructor private This means that when an object goes out of scope, it cannot be deleted Thus, stack created objects will not compile Heap created objects can be compiled, and should be deleted through some special function

Now, the converse How can you restrict usage of the new operator?

The funny thing... You know what the funny thing is about designing a class hierarchy, replete with class data members, virtual functions, complex attributed return values, and wanting to optimize it given a set of criteria? It s the little differences.

Expensive operations Temporary Objects! Owww! You want to avoid these as much as possible Pass by const reference or by const pointer Do not create any temporary objects that you *may* need until after you ve done your conditional checks Use the copy constructor whenever possible You can always make this faster using copyon-write

Proxies Almost, but not quite, exactly like copy-on-write Allows for the passing along of information from some master object that is big and should not be copied lightly, or should be centralized, due to data synchronicity Specifies what should be done to objects when information is written to the base Copy-on-write Specifies that a new object should always be created Pointers Specify that the existing object should be modified??? Specify that under certain conditions the existing object should be modified, but others it should be copied locally

Making complex from the simple When you are abstracting a system that has easy to understand functionality that builds on itself, take advantage of that Good example: comparative operators