September 19,

Similar documents
Networked Embedded System Patterns for C Developers. Overview of C (& C++) Programming Styles. Douglas C. Schmidt

An Overview of C++ Douglas C. Schmidt

COMP6771 Advanced C++ Programming

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

September 10,

C++ Templates. David Camp

CSI33 Data Structures

Overview of C++: Part 1

Chapter 11. Abstract Data Types and Encapsulation Concepts

COMP6771 Advanced C++ Programming

CS 247: Software Engineering Principles. C++ Templates. Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth. U Waterloo CS247 (Spring 2017) p.

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

Structuur van Computerprogramma s 2

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

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

Data Structures (INE2011)

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

Programmazione. Prof. Marco Bertini

COMP6771 Advanced C++ Programming

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

Short Notes of CS201

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

CS201 - Introduction to Programming Glossary By

use static size for this buffer

Chapter 12 - Templates

Object-Oriented Programming for Scientific Computing

Programsystemkonstruktion med C++: Övning 2. Karl Palmskog september 2010

Laboratorio di Tecnologie dell'informazione

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

Chapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved.

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

C++ Exception Handling 1

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

Before we dive in. Preprocessing Compilation Linkage

Introduction to C++ Templates and Exceptions. C++ Function Templates C++ Class Templates Exception and Exception Handler

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

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Chapter 5. ADTs Stack and Queue

CS 225. September 24 Iterators. Data Structures. Wade Fagen-Ulmschneider

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Has-a Relationships. A pen has a or contains a ball

eingebetteter Systeme

COMP 2355 Introduction to Systems Programming

COMP6771 Advanced C++ Programming

Part X. Advanced C ++

Introduction to Programming

[CS302-Data Structures] Homework 2: Stacks

Topics. bool and string types input/output library functions comments memory allocation templates classes

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types.

4/27/2014. Templates II. Warmup Write the templated Swap function. Class Templates CMSC 202

ECE 2400 Computer Systems Programming Fall 2017 Topic 15: C++ Templates

AN OVERVIEW OF C++ 1

Programming Abstractions

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

Stacks and Their Applications

Chapter 18: Stacks And Queues

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

CSE100. Advanced Data Structures. Lecture 4. (Based on Paul Kube course materials)

C and C++ 7. Exceptions Templates. Alan Mycroft

CSC 222: Computer Programming II. Spring 2004

A linear structure is an ordered (e.g., sequenced) arrangement of elements.

A linear structure is an ordered (e.g., sequenced) arrangement of elements.

CMSC 4023 Chapter 11

CMSC 341 Lecture 7. Announcements. Proj 2 up Project Preview tonight and tomorrow

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 11. Abstract Data Types and Encapsulation Concepts

STL Quick Reference for CS 241

Inheritance Modes. Controlling Inheritance 1

Using Enum Structs as Bitfields

Datastructuren André Deutz

COEN244: Class & function templates

ADT: Design & Implementation

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Linked List using a Sentinel

Instantiation of Template class

Introduction to C++ Systems Programming

Chapter 11. Abstract Data Types and Encapsulation Concepts

Data Structures using OOP C++ Lecture 9

Where do we go from here?

C++ Important Questions with Answers

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

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

EL2310 Scientific Programming

Chapter 18: Stacks And Queues

ADTs Stack and Queue. Outline

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

DATA TYPES. CS 403: Types and Classes DATA TYPES (CONT D)

Templates and Vectors

Function Templates. Consider the following function:

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Transcription:

September 19, 2013 1

Problems with previous examples Changes to the implementation will require recompilation & relinking of clients Extensions will require access to the source code Solutions Combine inheritance with dynamic binding to completely decouple interface from implementation & binding time This requires the use of C++ abstract base classes 2

Defining an abstract base class in C++ template <typename T> struct Stack { virtual ~Stack() = 0; // Need implementation! virtual void push(const T& item) = 0; virtual void pop(t& item) = 0; virtual bool is_empty() const = 0; virtual bool is_full() const = 0; void top(t& item) {/* Template Method */ pop (item); push (item); ; By using pure virtual methods we can guarantee that the compiler won t allow instantiation. 3

Inherit to create a specialized stack implementation via an STL vector #include "Stack.h" #include <vector> template <typename T> class V_Stack : public Stack<T> { public: V_Stack(size_t size = 10) : top_(0), stack_(size) { virtual void push(const T& item) { stack_[top_++] = item; virtual void pop(t& item) { item = stack_[--top_]; virtual bool is_empty() const { return top_ == 0; virtual bool is_full() const { return top_ >= stack_.size(); private: size_t top_; std::vector<t> stack_; ; 4

Inheritance can also create a linked list stack template <typename T> class Node; // forward declaration. template <typename T> class L_Stack : public Stack<T> { public: L_Stack(size_t hint = 10) {... ~L_Stack() {... // Copy constructor and assignment operator not shown virtual void push (const T& new_item) {... virtual void pop(t& top_item) {... virtual bool is_empty() const { return head_ == 0; virtual bool is_full() const { return false; private: // Head of linked list of Node<T> s. Node<T> *head_; ; 5

Node implementation template <typename T> class Node { friend template <typename T> class L_Stack; public: Node(T i, Node<T>* n = 0): item_(i), next_(n) { private: T item_; Node<T> *next_; ; 6

L_Stack(size_t hint) : head_(0) { ~L_Stack() { T junk; while (!is_empty) pop(junk); void push (const T& new_item) { head = new Node<T>(item, head); void pop(t& top_item) { top_item = head_->item; Node<T>* t = head_; head_ = head_->next; delete t; 7

Using out abstract base class, it is possible to write code that does not depend on the stack implementation Stack<int>* make_stack(bool use_v_stack) { return use_v_stack? new V_Stack<int> : new L_Stack<int>; void print_top(stack<int>* stack) { std::cout << "top = " << stack->top() << std::endl; int main (int argc, char**) { std::unique_ptr<stack<int>> sp(make_stack(argc > 1)); sp->push(10); print_top(sp.get()); 8

Moreover, we can make changes at run-time without modifying, recompiling, or relinking existing code via dynamic linking char stack_symbol[maxnamlen]; char stack_file[maxnamlen]; cin >> stack_file >> stack_factory; void* handle = ACE_OS::dlopen(stack_file); Stack<int>* (*factory)(bool) = ACE_OS::dlsym(handle, stack_factory); unique_ptr<stack<int>> sp ((*factory) (argc > 1)); sp->push(10); print_top(sp.get()); No need to stop, modify, and restart an executing application! 9

Prefer introduction of new features through the standard library rather than extending the core language Improve C++ to facilitate systems and library design, rather than to introduce new features only useful to specific applications Increase type safety by providing safer alternatives to current, unsafe techniques Increase performance and the ability to work directly with hardware Implement zero-overhead principle Make C++ easier to teach and to learn without removing any utility needed by expert programmers. 10

#include <iostream> #include <iterator> #include <vector> #include <algorithm> int main () { std::vector<int> v{10, 20, 30, 40; for (int& i : v) std::cout << i << std::endl; auto total = 0; std::for_each(v.cbegin(), v.cend(), [&total](int x) { total += x; ); std::cout << total << std::endl; return 0; 11

A major contribution to C++ is its support for defining abstract data types (ADTs) and for generic programming Classes, parameterized types, exception handling For some systems, C++ s ADT support is more important than using the OO features of the language For other systems, the use of C++ s OO features is essential to build highly flexible and extensible software Inheritance, dynamic binding, and RTTI 12