Another Mediocre Assertion Mechanism for C++

Size: px
Start display at page:

Download "Another Mediocre Assertion Mechanism for C++"

Transcription

1 Another Mediocre Assertion Mechanism for C++ Pedro Guerreiro Departamento de Informática Faculdade de Ciências e Tecnologia Universidade Nova de Lisboa P Caparica, Portugal pg@di.fct.unl.pt Copyright notice This paper will be published in??? (ed), Proceedings TOOLS 33, IEEE The material is 2000 IEEE. Personal use of this material is permitted. However, permission to reprint/republish this material for advertising or promotional purposes or for creating new collective works for resale or redistribution to servers or lists, or to reuse any copyrighted component of this work in other works must be obtained from the IEEE.

2 Another Mediocre Assertion Mechanism for C++ Pedro Guerreiro Departamento de Informática Faculdade de Ciências e Tecnologia Universidade Nova de Lisboa P Caparica, Portugal pg@di.fct.unl.pt Abstract Assertions are a basic programming ingredient. If the language you use does not support them in a proper way, at least you must use some kind of standardized comments, to express things like preconditions for functions. The comments can be turned into executable functions by a preprocessor, but we could write the function calls directly, with the advantage of having the compiler check their syntax. This technique can be used with C++, to emulate the full set of assertions used in Eiffel to implement Design by Contract. The assertion functions are grouped in a class, class Assertions, which is inherited by the classes whose functions we want to assert. The assertions can be turned on and off object by object, during the execution of the program. The system works well under inheritance, although some guidelines must be followed when writing postconditions on the base class. The simplicity of this mechanism makes it possible to comment out all or some of the assertions automatically, through the programming environment. 1. Introduction It used to be a common programming technique to emulate programming constructs absent from the language you were using. For example, Fortran IV did not have while loops. If you wanted to do structured programming in Fortran IV, you would emulate while loops by a clever systematic pattern (did I say pattern?) of one IF, two GOTO and one CONTINUE. In COBOL, we could consider paragraphs as procedures, but they are parameterless. If you wanted to pass arguments to a paragraph, you would put aside some working storage variables to do the communication between caller and callee. If you are using ANSI C and you want to program object-orientedly, you will design your program around types defined as structures, together with the functions that operate on those structures. These emulations are always mediocre, when compared with the original, but we cannot forsake our language of decades just because a new language introduces an idea that we fancy. If you have been happy with Pascal for the last 25 years, you are not going to switch to Java for your text processing applications just because Java comes with a Tokenizer class that helps that kind of job [1]. Instead, you emulate the concept in Pascal, and go on with your work, as usual. Assertions like the ones we find in Eiffel are a fundamental programming concept, and a tool that we cannot do without for expressing, in the program text, logical properties of the program [5, 7, 8, 9]. If your language, or your programming environment, does not behave like Eiffel in that respect, then, at least, you should express those properties as comments. The intention is good, but comments are comments, their syntax is not checked, it is easy to forget to update them, and in the end this can cause some confusion.

3 A more serious technique is to use formal comments, that can be handled by a preprocessor, and generate executable instructions to be used for catching bugs while the program is being developed [3, 6]. Another technique is to use a set of macros working together with a given debugger [4]. These are laudable approaches, but sophisticated ones, and their first drawback is that they cannot be taught to novices in programming. Indeed, they involve tools and concepts foreign to the language (the syntax of the comments, the preprocessor, the debugger), and affect, to some degree, the sharpness of the assertion mechanism. On the other hand, assertions are not something to be added after the program is written: they are a part of the design process, and should be present in the very first programs students have to study or to write. We could also impose the discipline of accompanying each function m in a class by two assertion functions m_pre and m_post, and then have m call m_pre upon entry and m_post upon exit, in such a way that m would fail (raise an exception) when those functions return false. In m_pre we would program the precondition for m, and in m_post the postcondition. Actually, this is the technique used by the jcontractor library for Java, which takes advantage of Java reflection, for automatically doing the instrumentation the original methods with the provided assertions [2]. The system we designed came from the need to teach Design by Contract [7, 8] to students who do not have an Eiffel environment to work with, but are knowledgeable in C++ [11]. In order to do something meaningful, students will have to use preconditions, postconditions, invariants, in their C++ programs, for specification purposes, for implementing Design by Contract, and for catching bugs. Using the preprocessor approach was out of the question, for it would be a giant step backwards: we simply do not want to have our programs go through another preprocessor prior to being submitted to the compiler. Even if the process was fully automatic and fast, then, when writing a program, we would have to deal with errors from the preprocessor and errors from the compiler, not a pleasant prospect at all. Using the macro approach was also unthinkable: after practicing with C++ for some years, we lost the touch to handle those elaborate macro techniques. In fact, one of the good things about C++ is that it downplays the importance of macros in programming. We do not want to have to rely on them for such a noble cause. So the assertion mechanism had to come from within the language. This approach makes it intrinsically portable, and we will be able to use it in any environment. Furthermore, if the design is simple it can be adapted to other similar languages. The goals of this emulation are modest: the full set of Eiffel assertions should be available, the assertions are executable; they can be turned on and off during execution; the program should be able to recover from an assertion that failed; we must be able to cut, or comment out, all the programming text related to assertions (in order to produce a clean version), and paste it back, in the same locations (even if the clean text was edited); no other data members or functions must be added to the classes for helping the mechanism; the mechanism should work in the presence of inheritance; the old notation must be available for postconditions; clever use of C++ is allowed (exceptions, multiple inheritance, template classes, STL), but no low-level gadgets (no macros, no pointers to functions, no meddling with the type system). After this introduction to the problem and this discussion of design goals for the mechanism, we start by presenting its preconditions, postconditions and invariants. The first version does not use the old notation, which we then introduce in a second stage. With the mechanism we fully specify a generic bounded stack. Then we consider loop variants and invariants, and we exemplify with Euclid s algorithm for the greatest common divisor. We end up by

4 analyzing what happens under inheritance, using two classes for simple bank accounts. The difficulties with inheritance are not solved by the mechanism, but by methodology. 2. Preconditions, postconditions, invariants Let us consider a generic bounded stack, as it could be implemented in C++, with an internal array: template <class T> class Stack private: int _capacity; int _count; T *_items; Stack(int capacity) // Constructor _capacity = capacity; _count = 0; _items = new T[capacity]; virtual ~Stack() // Destructor delete [] _items; virtual int count() const // Number of elements return _count; virtual bool empty() const // Are there no elements? return _count == 0; virtual bool full() const // Is representation full? return _count == _capacity; virtual const T& item() const // Top element return _items[_count-1]; virtual void put(const T& x) // Add x on top _items[_count] = x; _count++; virtual void remove() // Remove top element _count--; ; Please note our conventions: all data members are private and their names start with an underline. The corresponding getters use the same name without the underline. (In this example we do not have any setters, whose names would have the prefix set.) All functions are virtual, just like it happens implicitly in Eiffel or Java. Functions (other than constructors and destructor) are either selectors, i.e., const functions returning a value, or modifiers, i.e., nonconst functions returning void. Class arguments and results are passed by const reference. There are no pointers in the interface. All functions are declared and defined in the same file. This is a header file, and there is no need for the source file. Indeed, there will be only one source file in the program: the file that contains the main function. (No, this is not the most common C++ style ) Consider, for example, function put: it has a precondition that the stack must not be full, and a postcondition that the stack is not empty, and that the top element is the element just added to the stack [8]. We could specify this using comments: virtual void put(const T& x) // Add x on top // pre!full(); _items[_count] = x;

5 _count++; // post!empty() && item() == x; This only makes sense when the effective generic parameter used for type T has the == operator. The comments are formal, and we could imagine an automatic system capable of translating them into executable functions. In an object oriented architecture, these would have to be functions from some class. On the other hand, if we have the functions, there is no need to go through the comments: we can understand pre(!full()), a function call, just like we understand the comment, with the benefit that the compiler checks it. An additional benefit is that it is executable, and can raise an exception when the argument is false. Let Assertions be the class for our assertion functions. In the Eiffel tradition we will use require and ensure instead of pre and post. Functions from this class must be able to raise exceptions, so the class declares its own exception type. When an exception is raised, we can use its associated string to give some information to the client. For making it flexible, we shall let each assertion have a second optional argument representing the label of the assertion, which will be added to the exception text. Assertions may be turned on and off, therefore there must be a boolean data member for representing that on-off state. Here is a first sketch of the class: class Assertions class Exception: public std::exception Exception(const std::string& label): exception (("Assertion violation: " + label + ".").c_str()); ; private: bool _enabled; Assertions() // Constructor. _enabled = true; virtual void assertions_enable(bool b) // Turn assertions on and off. _enabled = b; virtual void require(const std::string& label, bool b) const // Preconditions. if (_enabled &&!b) throw Assertions::Exception("require " + label); void ensure(const std::string& label, bool b) const // Postconditions. if (_enabled &&!b) throw Assertions::Exception("ensure " + label); ;

6 In order to make these functions available to class Stack, we make Stack inherit from Assertions: template <class T> class Stack: public Assertions ; We can now properly program function put, using assertions: virtual void put(const T& x) // Add x on top. require(!full(), "not full"); _items[_count] = x; _count++; ensure(!empty(), "not empty"); ensure(item() == x, "top element"); As of now, functions require and ensure are the same, except for the text they add on their own to the label. However, ensure must perform an important extra task: checking the class invariant. A class invariant is a condition that holds for all objects of that class, immediately after construction, and at all subsequent times, except when an object is being modified by a function. The class invariant for class Stack is implemented as function invariant, redefining the one inherited from Assertions. Here is the scheme: In class Assertions: virtual bool invariant() const // Default invariant. return true; void ensure(bool b, const std::string& label = "") const // Postconditions. if (_enabled &&!b) throw Assertions::Exception("ensure " + label); if (_enabled &&!invariant()) throw Assertions::Exception("invariant " + label); Class Stack has the following invariant: virtual bool invariant() const // Stack invariant. return 0 <= count() && count() <= capacity(); The invariant is systematically checked at all postconditions. This means that in function put the same condition is checked twice. If we think this should be avoided, we can merge the conditions in the two ensure calls, at the cost of losing precision in the labels. In the general case, invariants must also be checked in preconditions. However, our convention that all class arguments are passed by const reference avoids the indirect invariant effect [8], through which an operation on an object may invalidate an invariant in another. In our case, the only way to change the value of an object is by calling a modifier on that object. Therefore, invariants need to be checked only upon exit from modifiers.

7 The invariant must be verified after construction and after each modifier. Thus, each constructor and each modifier should have a postcondition. Here is the stack constructor, fully asserted: Stack(int n) // Constructor. require(n > 0, "capacity is positive"); _capacity = n; _count = 0; _items = new T[n]; ensure(capacity() == n, "capacity set"); ensure(empty(), "initially empty"); ensure(_items!= 0, "representation set"); You may notice that the last postcondition is for the benefit of the class writer only: it deals with internal representation of stacks, which should be of no concern to clients [8]. 3. Postconditions in selectors The ensure assertion is not appropriate for selectors because in these cases it is not necessary to check the invariant, and, also, more trivially, because the ensure assertion, which is typically placed at the end of the function, is useless after the function s return statement. For selectors, we need an assertion on the value of the function, after it was computed, but before the function returns it. We use a different assertion, satisfy, for this: virtual void satisfy(bool b, const std::string& label = "") const if (_enabled &&!b) throw Assertions::Exception("satisfy " + label); This requires a particular style of programming, borrowed from Eiffel, in which the result of the function is computed into a local variable result, which is then returned in the last statement of the function. Here are two examples: virtual bool empty() const // Are there no elements? bool result = _count == 0; satisfy(result == (count() == 0), "empty result"); return result; virtual bool full() const // Is representation full? bool result = _count == _capacity; satisfy(result == (count() == capacity()), "full result"); return result; These postconditions can be seen as particular cases of class invariants, stating that empty() == (count() == 0) and that full() == (count() == capacity()). Selectors which merely return the value of a private data member need no postconditions, and they can be understood as proxies of the value of the data member. These selectors are necessary in C++, and unnecessary in Eiffel.

8 4. Checking arbitrary conditions The assertions we have seen so far are used for special purposes: preconditions and postconditions. If we want to assert that an arbitrary property holds at a certain point in the program text, we use assertion check: virtual void check(bool b, const std::string& label = "") const if (_enabled &&!b) throw Assertions::Exception("check " + label); This assertion is very similar to satisfy and to require. We could live with only one of them, if we forsake the information provided to the exception message. However, the idea is that we can straightforwardly refine this mechanism in order to turn on and off assertions of a given category. We do not pursue that here, in order not to burden the presentation with several different _enableds, one for each assertion: _enabled_require, _enabled_ensure, _enabled_check, etc. 5. The old notation The old mechanism is used to compare the value of an attribute after the operation of a modifier with the initial value of that attribute. There are no miracles here: either we do some preprocessing to discover that the initial value of a given attribute is mentioned in a postcondition, or we declare that ourselves in the program. Our system, which does not do any preprocessing, uses the function observe for that. By observing an attribute we store its value together with a key. Later we can retrieve it using the function old. Let us see how functions observe and old are used in function put: virtual void put(const T& x) // Add x on top. require(!full(), "not full"); observe("count", count()); _items[_count] = x; _count++; ensure(!empty(), "not empty"); ensure(item() == x, "top element"); ensure(count() == old("count") + 1, "count was incremented"); For implementing observe and old we use class map from the standard template library, STL [10]. Class Assertions now has a second data member, a map: class Assertions private: bool _enabled; std::map<const std::string, int> _old; ; This implementation is simple, but it limits us to observe integers only. Here are the functions: virtual void observe(const std::string& tag, const int& x)

9 _old[tag] = x; virtual int old(const std::string& tag) return _old[tag]; Were it not for STL, this would require a lot of work. The fact that this old mechanism can only handle integers is a serious limitation, of course. To be able to contemplate other types, we can either have a map for each type (no, we do not want that!) or use a polymorphic map. This polymorphic map approach looks promising, and we are currently working on it. 6. Loop invariants and variants The invariants we saw were class invariants, that are supposed to hold after construction and after each modifier. We should also consider loop invariants, that hold after each step in a repetitive process. The idea is that if the invariant holds before the loop, and each step of the loop does not destroy it, then, if the loop ever terminates, it will terminate in a state where the invariant holds. The invariant is just a condition, and we could check it with assertion check. However, for clarity, we introduce another assertion, loop_invariant: virtual void loop_invariant(bool b, const std::string& label = "") const if (_enabled &&!b) throw Assertions::Exception("loop invariant " + label); In order to guarantee that the loop terminates, we can use a variant function, i.e., an integer function such that its values strictly decrease at each step in the loop, but always remain nonnegative. If such a function exists the loop must terminate. For implementing function loop_variant, which will fail if the variant function does not decrease, or if it becomes negative, we use the map. The first call to loop_variant installs the initial value, the subsequent calls compare and update the value stored in the map: virtual void loop_variant(const std::string& tag, int n) const Assertions* that = const_cast<assertions*>(this); if (_enabled) if (0 <= n && (_old.count(tag) == 0 n < that->_old[tag])) that->_old[tag] = n; else throw Assertions::Exception("variant " + tag); Note that loop_variant must be a const function, even though it must change the representation. This is because, unlike observe and old which are only used for modifiers, loop_variant may be used for selectors, which are declared const, and are not allowed to modify their objects. This is the reason why we have to throw away the constness of the map. Assertion loop_variant should be placed before the iterated statement, where it will be checked before each step. It is true that the very last step may not decrease the variant, and yet there is no failure, because the loop will have terminated. However, if the loop is entered again, the last value from the previous execution would still be stored, and that would disrupt

10 the process. This shows that we must be careful to release the variant tag, so that it will not be present in the map the next time. Here is function release, doing just that: virtual void release(const std::string& tag) const const_cast<assertions*>(this)->_old.erase(tag); The standard example for loop invariants is Euclid s algorithm for the greatest common divisor [8]. With our assertion mechanism, it looks like this: class Euclid: public Assertions virtual int greatest_common_divisor(int a, int b) const require(a > 0 && b > 0, "arguments positive"); int x = a; int y = b; loop_invariant(x > 0 && y > 0, "loop invariant"); loop_invariant(true, "if d divides x and y, d divides a and b"); while (x!= y) loop_variant("gcd loop variant", x + y); if (x < y) y -= x; else x -= y; loop_invariant(x > 0 && y > 0, "loop invariant"); loop_invariant(true, "if d divides x and y, d divides a and b"); release("gcd loop variant"); check(x == y, "values are equal"); int result = x; return result; ; Note the second loop invariant in each case. With a true condition, it functions just like a comment. 7. Inheritance In order to investigate what happens to preconditions and postconditions under inheritance let us consider the standard example: a class Account, representing simple bank accounts with which you can deposit and withdraw amounts of money, and a derived class, more specialized. Here is class Account, with our assertions in place: class Account: protected Assertions private: std::string _client; int _balance; int _minimal_balance; Account(const std::string name, int deposit) // Constructor. require(deposit > 0 && deposit >= minimal_balance(), "initial amount"); _client = name; _balance = deposit;

11 _minimal_balance = 20; ensure(client() == name && balance() == deposit, "construction ok"); virtual const std::string& client() const // Client of the account. return _client; virtual int balance() const // Balance of the account. return _balance; virtual int minimal_balance() const // Minimal balance allowed. return _minimal_balance; virtual void deposit(int amount) // Deposit amount in the account. require(amount > 0, "amount is positive"); observe("balance", balance()); _balance = _balance + amount; ensure(balance() == old("balance") + amount, "balance is now greater"); virtual void withdraw(int amount) // Withdraw amount from the account. require(amount > 0, "amount is positive"); require(balance() - amount >= minimal_balance(), "withdrawal is possible"); observe("balance", balance()); _balance = _balance - amount; ensure(balance() == old("balance") - amount, "balance is now smaller"); virtual bool invariant() const return balance() >= minimal_balance(); ; Class AccountDetailed stores all operations in two lists: a list for deposits and a list for withdrawals. Let us concentrate on the constructor and one of the modifiers, deposit: class AccountDetailed: public Account private: std::list<int> deposits; std::list<int> withdrawals; AccountDetailed(const std::string name, int deposit): // Constructor. Account(name, deposit), deposits(), withdrawals() deposits.push_front(deposit); ensure(deposits.size() == 1 && withdrawals.empty(), "construction derived ok"); virtual void deposit(int amount) // Deposit amount in the account.

12 Account::deposit(amount); observe("deposits size", deposits.size()); observe("sum deposits", sum_deposits()); deposits.push_front(amount); ensure(deposits.size() == old("deposits size") + 1, "deposit size ok"); ensure(sum_deposits() == old("sum deposits") + amount, "sum deposits"); virtual bool invariant() const return Account::invariant() && sum_deposits() - sum_withdrawals() == balance() && count_operations() == count_deposits() + count_withdrawals(); ; Function deposit first calls the base function, as usual in these situations. Is this OK? Within the base call, the precondition will be evaluated, and this causes no problem, because the precondition for the derived class must be the same. In fact, applying Design by Contract, the precondition for a redefined function may be more liberal, but not less liberal, than the original. However, in our system we have no direct way of weakening the precondition, and so, the precondition must be the same. Note that we have no explicit require in the redefined function. If we had, we would be strengthening the preconditions, against the rules of Design by Contract. After the base class function is called, its postcondition is evaluated. The precondition may involve polymorphically functions from the derived class. In this example it does not do that directly, but it does it through the invariant. Here the situation is dangerous: the base class object has been modified, but the complete derived object has not: the call to the invariant at this point is meaningless, and it may fail because the object has not been fully modified yet. Actually, that is what happen with the previous program. Once the problem is identified, the solution is obvious: one must not evaluate the postcondition on the full object, but only on the base class object. Since the postcondition is called from the base class, it s the base class we must change: virtual void deposit(int amount) // Deposit amount in the account. require(amount > 0, "amount is positive"); observe("balance", balance()); _balance = _balance + amount; Account(*this).ensure(balance() == old("balance") + amount, "balance is now greater"); The technique is to create a temporary copy of the object, guaranteed to be of the base class, and evaluate the postcondition on this object. Note that this phenomenon does not happen with the constructor. The postcondition in the base class constructor called for an object of the base class, always, and no polymorphism is involved. This example and the discussion show that our assertion mechanism can also be used with inheritance, provided the preconditions are the same in the redefined functions, and the postconditions on the base class are explicitly computed on objects of the base class. If we really need a different precondition for a redefined function then we should define the precondition as a member function of the base class, and redefine it, either more liberally or more strictly,

13 in the derived class. We then use this member function in the require calls in both the base class and the derived class, effectively making it the same, to the eyes of the assertion mechanism. This corresponds to the idea of an abstract precondition [8]. According to Design by Contract, postconditions in derived classes can only be more strict than the corresponding ones in the base class. This is the standard behavior of our system: calls of ensure for modifiers in derived classes reinforce the calls already made for the base class object. 8. Conclusion If you do not have a dog, you go hunting with a cat. C++ does not come equipped with an assertion mechanism, but the one we designed works quite well, and can be installed (and learned) quickly. With it we can implement Design by Contract on a meaningful basis, using as assertion language the boolean expressions offered by C++. The mechanism is simple and light: it can be used for some of the classes in our program, and does not penalize the others. It can be toggled an object by object basis, even during program execution. Switching it off will not speed the program dramatically, however, because the arguments of the assertions will still be evaluated. And, if they are evaluated, we may as well let them perform their mission to the end However, if you want more control, you can modify the system so that a set of switches allow to turn on and off all assertions of a given category. On the other hand, it is a simple programming exercise to comment out the assertions, selectively if necessary. If we comment out all the assertions, there will be absolutely no overhead left from the assertion mechanism to slow down the execution. Furthermore, if you think the comments obfuscate the program, they can be erased automatically, or erased and kept in a separate file for later reinsertion. The simplicity of the scheme makes all this possible. The mechanism does not have the elegance of Eiffel, nor does it offer all its facilities ( old for integers only is not much, preconditions in base classes in the presence of inheritance are a bit clumsy) but it can be an acceptable compromise, and a useful vehicle to introduce the ideas of Design by Contract to a wider audience. It is quite unlikely that C++ will evolve to incorporate an assertion mechanism like Eiffel s. It is less unlikely that such a mechanism be provided by the programming environment. Some of the ideas briefly discussed here may have a role in that context. References [1] Arnold K and J Gosling, The Java Programming Language, Addison-Wesley, Reading (Mass.),1996. [2] Karaorman M, U Hölzle and J Bruno, jcontractor: A Reflective Java Library to Support Design By Contract, Technical Report TRCS98-31, Department of Computer Science, University of California, Santa Barbara, CA [3] Kramer R, icontract The Java Design by Contract Tool, Proc. of TOOLS 98, Santa Barbara, CA August 1998., pp [4] Maker P J, [5] McKim J, Programming By Contract: Designing for Correctness, Journal of Object Oriented Programming, May [6] Meemken D, [7] Meyer B, Eiffel, the Language, Prentice-Hall, New York, [8] Meyer B, Object-Oriented Software Construction, Second Edition, Prentice-Hall, Upper Saddle River (New Jersey), [9] Mitchell R and J McKim, Extending a Method of Devising Software Contracts, Proceedings TOOLS 32, IEEE [10] Musser D and A Saini, STL Tutorial and Reference Guide, Addison-Wesley, Reading (Mass.), [11] Stroustrup B: The C++ Programming Language, Third Edition, Addison-Wesley, Reading (Mass.), 1997.

Simple Support for Design by Contract in C++ (extended version)

Simple Support for Design by Contract in C++ (extended version) Simple Support for Design by Contract in C++ (extended version) Pedro Guerreiro Departamento de Informática Faculdade de Ciências e Tecnologia Universidade Nova de Lisboa P-2825-114 Caparica, Portugal

More information

Adding Contracts to C#

Adding Contracts to C# Adding Contracts to C# Peter Lagace ABSTRACT Design by contract is a software engineering technique used to promote software reliability. In order to use design by contract the selected programming language

More information

Assertions, pre/postconditions

Assertions, pre/postconditions Programming as a contract Assertions, pre/postconditions Assertions: Section 4.2 in Savitch (p. 239) Specifying what each method does q Specify it in a comment before method's header Precondition q What

More information

Object Oriented Program Correctness with OOSimL

Object Oriented Program Correctness with OOSimL Kennesaw State University DigitalCommons@Kennesaw State University Faculty Publications 12-2009 Object Oriented Program Correctness with OOSimL José M. Garrido Kennesaw State University, jgarrido@kennesaw.edu

More information

The Contract Pattern. Design by contract

The Contract Pattern. Design by contract The Contract Pattern Copyright 1997, Michel de Champlain Permission granted to copy for PLoP 97 Conference. All other rights reserved. Michel de Champlain Department of Computer Science University of Canterbury,

More information

Assertions. Assertions - Example

Assertions. Assertions - Example References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 11/13/2003 1 Assertions Statements about input to a routine or state of a class Have two primary roles As documentation,

More information

Lecture Notes on Contracts

Lecture Notes on Contracts Lecture Notes on Contracts 15-122: Principles of Imperative Computation Frank Pfenning Lecture 2 August 30, 2012 1 Introduction For an overview the course goals and the mechanics and schedule of the course,

More information

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1 References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1 Assertions Statements about input to a routine or state of a class Have two primary roles As documentation,

More information

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012 Why Design by Contract What s the difference with Testing? CS 619 Introduction to OO Design and Development Design by Contract Fall 2012 Testing tries to diagnose (and cure) defects after the facts. Design

More information

PROCESS DEVELOPMENT METHODOLOGY The development process of an API fits the most fundamental iterative code development

PROCESS DEVELOPMENT METHODOLOGY The development process of an API fits the most fundamental iterative code development INTRODUCING API DESIGN PRINCIPLES IN CS2 Jaime Niño Computer Science, University of New Orleans New Orleans, LA 70148 504-280-7362 jaime@cs.uno.edu ABSTRACT CS2 provides a great opportunity to teach an

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

Design by Contract in Eiffel

Design by Contract in Eiffel Design by Contract in Eiffel 2002/04/15 ctchen@canthink.com.com.tw.tw Reference & Resource Bertrand Meyer, Object-Oriented Oriented Software Construction 2nd,, 1997, PH. Bertrand Meyer, Eiffel: The Language,,

More information

UC Santa Barbara. CS189A - Capstone. Christopher Kruegel Department of Computer Science UC Santa Barbara

UC Santa Barbara. CS189A - Capstone. Christopher Kruegel Department of Computer Science UC Santa Barbara CS189A - Capstone Christopher Kruegel Department of Computer Science http://www.cs.ucsb.edu/~chris/ Design by Contract Design by Contract and the language that implements the Design by Contract principles

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

Problem Solving with C++

Problem Solving with C++ GLOBAL EDITION Problem Solving with C++ NINTH EDITION Walter Savitch Kendrick Mock Ninth Edition PROBLEM SOLVING with C++ Problem Solving with C++, Global Edition Cover Title Copyright Contents Chapter

More information

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning Lecture 1 Contracts 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning In these notes we review contracts, which we use to collectively denote function contracts, loop invariants,

More information

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning Lecture 1 Contracts 15-122: Principles of Imperative Computation (Fall 2018) Frank Pfenning In these notes we review contracts, which we use to collectively denote function contracts, loop invariants,

More information

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.

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. 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. 9 Self-Test Exercises p. 11 History Note p. 12 Programming and

More information

Lecture Notes on Arrays

Lecture Notes on Arrays Lecture Notes on Arrays 15-122: Principles of Imperative Computation July 2, 2013 1 Introduction So far we have seen how to process primitive data like integers in imperative programs. That is useful,

More information

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too) CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too) HW6 NOTE: Do not use the STL map or STL pair for HW6. (It s okay to use them for the contest.)

More information

Lecture 3 Notes Arrays

Lecture 3 Notes Arrays Lecture 3 Notes Arrays 15-122: Principles of Imperative Computation (Summer 1 2015) Frank Pfenning, André Platzer 1 Introduction So far we have seen how to process primitive data like integers in imperative

More information

CSE 143. Programming is... Principles of Programming and Software Engineering. The Software Lifecycle. Software Lifecycle in HW

CSE 143. Programming is... Principles of Programming and Software Engineering. The Software Lifecycle. Software Lifecycle in HW Principles of Programming and Software Engineering Textbook: hapter 1 ++ Programming Style Guide on the web) Programming is......just the beginning! Building good software is hard Why? And what does "good"

More information

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

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

Classes, interfaces, & documentation. Review of basic building blocks

Classes, interfaces, & documentation. Review of basic building blocks Classes, interfaces, & documentation Review of basic building blocks Objects Data structures literally, storage containers for data constitute object knowledge or state Operations an object can perform

More information

Lecture Notes on Memory Layout

Lecture Notes on Memory Layout Lecture Notes on Memory Layout 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 11 1 Introduction In order to understand how programs work, we can consider the functions,

More information

Chapter 1 Getting Started

Chapter 1 Getting Started Chapter 1 Getting Started The C# class Just like all object oriented programming languages, C# supports the concept of a class. A class is a little like a data structure in that it aggregates different

More information

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

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s Session 2 - Classes in C++ Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) A C++ source file may contain: include directives #include

More information

Lecture 10 Notes Linked Lists

Lecture 10 Notes Linked Lists Lecture 10 Notes Linked Lists 15-122: Principles of Imperative Computation (Spring 2016) Frank Pfenning, Rob Simmons, André Platzer 1 Introduction In this lecture we discuss the use of linked lists to

More information

Object-Oriented Design

Object-Oriented Design Object-Oriented Design Lecturer: Raman Ramsin Lecture 15: Object-Oriented Principles 1 Open Closed Principle (OCP) Classes should be open for extension but closed for modification. OCP states that we should

More information

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p. Welcome to Teach Yourself p. viii Acknowledgments p. xv Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p. 6 Standard C++: A Programming Language and a Library p. 8

More information

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

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

a correct statement? You need to know what the statement is supposed to do.

a correct statement? You need to know what the statement is supposed to do. Using assertions for correctness How can we know that software is correct? It is only correct if it does what it is supposed to do. But how do we know what it is supposed to do? We need a specification.

More information

Java: advanced object-oriented features

Java: advanced object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: advanced object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Packages

More information

Instantiation of Template class

Instantiation of Template class Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double

More information

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

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM Objectives Defining a wellformed method to check class invariants Using assert statements to check preconditions,

More information

Wrapping a complex C++ library for Eiffel. FINAL REPORT July 1 st, 2005

Wrapping a complex C++ library for Eiffel. FINAL REPORT July 1 st, 2005 Wrapping a complex C++ library for Eiffel FINAL REPORT July 1 st, 2005 Semester project Student: Supervising Assistant: Supervising Professor: Simon Reinhard simonrei@student.ethz.ch Bernd Schoeller Bertrand

More information

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition Programming as a contract Assertions, pre/postconditions and invariants Assertions: Section 4.2 in Savitch (p. 239) Loop invariants: Section 4.5 in Rosen Specifying what each method does q Specify it in

More information

What This Course Is About Design-by-Contract (DbC)

What This Course Is About Design-by-Contract (DbC) What This Course Is About Design-by-Contract (DbC) Readings: OOSC2 Chapter 11 EECS3311 A: Software Design Fall 2018 CHEN-WEI WANG Focus is design Architecture: (many) inter-related modules Specification:

More information

The architecture of Eiffel software 3.1 OVERVIEW classes clusters systems

The architecture of Eiffel software 3.1 OVERVIEW classes clusters systems 3 Draft 5.02.00-0, 15 August 2005 (Santa Barbara). Extracted from ongoing work on future third edition of Eiffel: The Language. Copyright Bertrand Meyer 1986-2005. Access restricted to purchasers of the

More information

Method Description for Semla A Software Design Method with a Focus on Semantics

Method Description for Semla A Software Design Method with a Focus on Semantics Computer Science Method Description for Semla A Software Design Method with a Focus on Semantics Semla for Java, English version May 2000 Method Description for Semla A Software Design Method with a Focus

More information

C++ Coding Standards. 101 Rules, Guidelines, and Best Practices. Herb Sutter Andrei Alexandrescu. Boston. 'Y.'YAddison-Wesley

C++ Coding Standards. 101 Rules, Guidelines, and Best Practices. Herb Sutter Andrei Alexandrescu. Boston. 'Y.'YAddison-Wesley C++ Coding Standards 101 Rules, Guidelines, and Best Practices Herb Sutter Andrei Alexandrescu 'Y.'YAddison-Wesley Boston Contents Prefaee xi Organizational and Poliey Issues 1 o. Don't sweat the small

More information

Lecture 10 Notes Linked Lists

Lecture 10 Notes Linked Lists Lecture 10 Notes Linked Lists 15-122: Principles of Imperative Computation (Summer 1 2015) Frank Pfenning, Rob Simmons, André Platzer 1 Introduction In this lecture we discuss the use of linked lists to

More information

Lecture 7: Data Abstractions

Lecture 7: Data Abstractions Lecture 7: Data Abstractions Abstract Data Types Data Abstractions How to define them Implementation issues Abstraction functions and invariants Adequacy (and some requirements analysis) Towards Object

More information

LECTURE 03 LINKED LIST

LECTURE 03 LINKED LIST DATA STRUCTURES AND ALGORITHMS LECTURE 03 LINKED LIST IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD LINKED LISTS DEFINITION A linked list is a data structure where each object is stored in

More information

AN OVERVIEW OF C++ 1

AN OVERVIEW OF C++ 1 AN OVERVIEW OF C++ 1 OBJECTIVES Introduction What is object-oriented programming? Two versions of C++ C++ console I/O C++ comments Classes: A first look Some differences between C and C++ Introducing function

More information

Tokens, Expressions and Control Structures

Tokens, Expressions and Control Structures 3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

More information

Design By Contract in C++ Nicola Di Nisio

Design By Contract in C++ Nicola Di Nisio Design By Contract in C++ Nicola Di Nisio Design By Contract, a OO design technique first introduced by Eiffel, can be used even in C++. This papers introduces the Design By Contract as it is used in Eiffel

More information

Ambientes de Desenvolvimento Avançados

Ambientes de Desenvolvimento Avançados Ambientes de Desenvolvimento Avançados http://www.dei.isep.ipp.pt/~jtavares/adav/adav.htm Aula 6 Engenharia Informática 2006/2007 José António Tavares jrt@isep.ipp.pt 1 Components and Interfaces Part 2

More information

Designing Robust Classes

Designing Robust Classes Designing Robust Classes Learning Goals You must be able to:! specify a robust data abstraction! implement a robust class! design robust software! use Java exceptions Specifications and Implementations

More information

Design Principles for a Beginning Programming Language

Design Principles for a Beginning Programming Language Design Principles for a Beginning Programming Language John T Minor and Laxmi P Gewali School of Computer Science University of Nevada, Las Vegas Abstract: We consider the issue of designing an appropriate

More information

Object-Oriented Design

Object-Oriented Design Object-Oriented Design Department of Computer Engineering Lecture 12: Object-Oriented Principles Sharif University of Technology 1 Open Closed Principle (OCP) Classes should be open for extension but closed

More information

2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked

2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked Chapter 6 LISTS AND STRINGS 1. List Specifications 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked 3. Strings 4. Application:

More information

Operator overloading

Operator overloading 1 Introduction 2 The copy constructor 3 Operator Overloading 4 Eg 1: Adding two vectors 5 The -> operator 6 The this pointer 7 Overloading = 8 Unary operators 9 Overloading for the matrix class 10 The

More information

Patterns for polymorphic operations

Patterns for polymorphic operations Patterns for polymorphic operations Three small object structural patterns for dealing with polymorphism Alexander A. Horoshilov hor@epsylontech.com Abstract Polymorphism is one of the main elements of

More information

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED EXERCISE 11.1 1. static public final int DEFAULT_NUM_SCORES = 3; 2. Java allocates a separate set of memory cells in each instance

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

AIMS Embedded Systems Programming MT 2017

AIMS Embedded Systems Programming MT 2017 AIMS Embedded Systems Programming MT 2017 Object-Oriented Programming with C++ Daniel Kroening University of Oxford, Computer Science Department Version 1.0, 2014 Outline Classes and Objects Constructors

More information

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

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location

More information

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996. References: Topic IV Block-structured procedural languages Algol and Pascal Chapters 5 and 7, of Concepts in programming languages by J. C. Mitchell. CUP, 2003. Chapter 5 of Programming languages: Concepts

More information

Violations of the contract are exceptions, and are usually handled by special language constructs. Design by contract

Violations of the contract are exceptions, and are usually handled by special language constructs. Design by contract Specification and validation [L&G Ch. 9] Design patterns are a useful way to describe program structure. They provide a guide as to how a program fits together. Another dimension is the responsibilities

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Integrating verification in programming languages

Integrating verification in programming languages Integrating verification in programming languages Thomas Jensen, INRIA Seminar INRIA Rennes, 04/11/2015 Collège de France Chaire Algorithmes, machines et langages x / y Types For division to make sense,

More information

Week 8: Operator overloading

Week 8: Operator overloading Due to various disruptions, we did not get through all the material in the slides below. CS319: Scientific Computing (with C++) Week 8: Operator overloading 1 The copy constructor 2 Operator Overloading

More information

COEN244: Class & function templates

COEN244: Class & function templates COEN244: Class & function templates Aishy Amer Electrical & Computer Engineering Templates Function Templates Class Templates Outline Templates and inheritance Introduction to C++ Standard Template Library

More information

There are three basic elements in object oriented programming: encapsulation, inheritance and polymorphism.

There are three basic elements in object oriented programming: encapsulation, inheritance and polymorphism. More on Object Oriented Programming Concepts Functional, structured programming often results in programs that describe a hierarchy of tasks to be performed. Object oriented design, however, results in

More information

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques 1 CPSC2620 Advanced Programming Spring 2003 Instructor: Dr. Shahadat Hossain 2 Today s Agenda Administrative Matters Course Information Introduction to Programming Techniques 3 Course Assessment Lectures:

More information

AXIOMS OF AN IMPERATIVE LANGUAGE PARTIAL CORRECTNESS WEAK AND STRONG CONDITIONS. THE AXIOM FOR nop

AXIOMS OF AN IMPERATIVE LANGUAGE PARTIAL CORRECTNESS WEAK AND STRONG CONDITIONS. THE AXIOM FOR nop AXIOMS OF AN IMPERATIVE LANGUAGE We will use the same language, with the same abstract syntax that we used for operational semantics. However, we will only be concerned with the commands, since the language

More information

M Introduction to Visual Basic.NET Programming with Microsoft.NET 5 Day Course

M Introduction to Visual Basic.NET Programming with Microsoft.NET 5 Day Course Module 1: Getting Started This module introduces Visual Basic.NET and explains how it fits into the.net platform. It explains how to use the programming tools in Microsoft Visual Studio.NET and provides

More information

Write for your audience

Write for your audience Comments Write for your audience Program documentation is for programmers, not end users There are two groups of programmers, and they need different kinds of documentation Some programmers need to use

More information

III. Classes (Chap. 3)

III. Classes (Chap. 3) III. Classes III-1 III. Classes (Chap. 3) As we have seen, C++ data types can be classified as: Fundamental (or simple or scalar): A data object of one of these types is a single object. int, double, char,

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

Objectives for this class meeting. 1. Conduct review of core concepts concerning contracts and pre/post conditions

Objectives for this class meeting. 1. Conduct review of core concepts concerning contracts and pre/post conditions CSE1720 Click to edit Master Week text 01, styles Lecture 02 Second level Third level Fourth level Fifth level Winter 2015! Thursday, Jan 8, 2015 1 Objectives for this class meeting 1. Conduct review of

More information

Program Correctness and Efficiency. Chapter 2

Program Correctness and Efficiency. Chapter 2 Program Correctness and Efficiency Chapter 2 Chapter Objectives To understand the differences between the three categories of program errors To understand the effect of an uncaught exception and why you

More information

Course specification

Course specification The University of Southern Queensland Course specification Description: Object-Oriented Programming in C++ Subject Cat-nbr Class Term Mode Units Campus CSC 2402 30366 1, 2004 ONC 1.00 TWMBA Academic group:

More information

CS112 Lecture: Defining Instantiable Classes

CS112 Lecture: Defining Instantiable Classes CS112 Lecture: Defining Instantiable Classes Last revised 2/3/05 Objectives: 1. To describe the process of defining an instantiable class 2. To discuss public and private visibility modifiers. Materials:

More information

Motivation was to facilitate development of systems software, especially OS development.

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

More information

Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute.

Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute. Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute. Abelson & Sussman Use a good set of coding conventions, such as the ones given in the

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter

More information

An Introduction to C++

An Introduction to C++ An Introduction to C++ Introduction to C++ C++ classes C++ class details To create a complex type in C In the.h file Define structs to store data Declare function prototypes The.h file serves as the interface

More information

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

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++ CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 19 Introduction to C++ C++ C++ is an enormous language: All of C Classes and objects (kind of like Java, some crucial differences) Many

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

Interview Questions of C++

Interview Questions of C++ Interview Questions of C++ Q-1 What is the full form of OOPS? Ans: Object Oriented Programming System. Q-2 What is a class? Ans: Class is a blue print which reflects the entities attributes and actions.

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1.... COMP6771 Advanced C++ Programming Week 5 Part One: Exception Handling 2016 www.cse.unsw.edu.au/ cs6771 2.... Memory Management & Exception Handling.1 Part I: Exception Handling Exception objects

More information

Catching Defects: Design or Implementation Phase? Design-by-Contract (Dbc) Test-Driven Development (TDD) Motivation of this Course

Catching Defects: Design or Implementation Phase? Design-by-Contract (Dbc) Test-Driven Development (TDD) Motivation of this Course Design-by-Contract (Dbc) Test-Driven Development (TDD) Readings: OOSC2 Chapter 11 Catching Defects: Design or Implementation Phase? To minimize development costs, minimize software defects. The cost of

More information

Late-bound Pragmatical Class Methods

Late-bound Pragmatical Class Methods Late-bound Pragmatical Class Methods AXEL SCHMOLITZKY, MARK EVERED, J. LESLIE KEEDY, GISELA MENGER Department of Computer Structures University of Ulm 89069 Ulm, Germany {axel, markev, keedy, gisela@informatik.uni-ulm.de

More information

Mutating Object State and Implementing Equality

Mutating Object State and Implementing Equality Mutating Object State and Implementing Equality 6.1 Mutating Object State Goals Today we touch the void... (sounds creepy right... see the movie, or read the book, to understand how scary the void can

More information

The Eiffel language. Slides partly based on :

The Eiffel language. Slides partly based on : The Eiffel language Slides partly based on : http://se.inf.ethz.ch/courses/2015b_fall/eprog/english_index.html Eiffel, in brief Procedural, object-oriented programming language created by Bertrand Meyer

More information

Allocation & Efficiency Generic Containers Notes on Assignment 5

Allocation & Efficiency Generic Containers Notes on Assignment 5 Allocation & Efficiency Generic Containers Notes on Assignment 5 CS 311 Data Structures and Algorithms Lecture Slides Friday, October 30, 2009 Glenn G. Chappell Department of Computer Science University

More information

Chapter 1: Programming Principles

Chapter 1: Programming Principles Chapter 1: Programming Principles Object Oriented Analysis and Design Abstraction and information hiding Object oriented programming principles Unified Modeling Language Software life-cycle models Key

More information

Topic IV. Block-structured procedural languages Algol and Pascal. References:

Topic IV. Block-structured procedural languages Algol and Pascal. References: References: Topic IV Block-structured procedural languages Algol and Pascal Chapters 5 and 7, of Concepts in programming languages by J. C. Mitchell. CUP, 2003. Chapters 10( 2) and 11( 1) of Programming

More information

CS 215 Software Design Sample midterm solutions

CS 215 Software Design Sample midterm solutions Software Design Sample midterm solutions 1. The administration at Happy Valley School District is redesigning the software that manages information about its students. It has identified an abstract class

More information

UNIT TESTING OF C++ TEMPLATE METAPROGRAMS

UNIT TESTING OF C++ TEMPLATE METAPROGRAMS STUDIA UNIV. BABEŞ BOLYAI, INFORMATICA, Volume LV, Number 1, 2010 UNIT TESTING OF C++ TEMPLATE METAPROGRAMS ÁBEL SINKOVICS Abstract. Unit testing, a method for verifying a piece of software, is a widely

More information

Lecture Notes on Queues

Lecture Notes on Queues Lecture Notes on Queues 15-122: Principles of Imperative Computation Frank Pfenning and Jamie Morgenstern Lecture 9 February 14, 2012 1 Introduction In this lecture we introduce queues as a data structure

More information

Java Coding 3. Over & over again!

Java Coding 3. Over & over again! Java Coding 3 Over & over again! Repetition Java repetition statements while (condition) statement; do statement; while (condition); where for ( init; condition; update) statement; statement is any Java

More information

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE331 Winter 2014, Midterm Examination February 12, 2014 CSE331 Winter 2014, Midterm Examination February 12, 2014 Please do not turn the page until 10:30. Rules: The exam is closed-book, closed-note, etc. Please stop promptly at 11:20. There are 100 points

More information

CS 161 Computer Security

CS 161 Computer Security Wagner Spring 2014 CS 161 Computer Security 1/27 Reasoning About Code Often functions make certain assumptions about their arguments, and it is the caller s responsibility to make sure those assumptions

More information

LECTURE 18. Control Flow

LECTURE 18. Control Flow LECTURE 18 Control Flow CONTROL FLOW Sequencing: the execution of statements and evaluation of expressions is usually in the order in which they appear in a program text. Selection (or alternation): a

More information

Test-Driven Development (TDD)

Test-Driven Development (TDD) Test-Driven Development (TDD) EECS3311 A: Software Design Fall 2018 CHEN-WEI WANG DbC: Supplier DbC is supported natively in Eiffel for supplier: class ACCOUNT create make feature -- Attributes owner :

More information

Design-by-Contract (Dbc) Test-Driven Development (TDD)

Design-by-Contract (Dbc) Test-Driven Development (TDD) Design-by-Contract (Dbc) Test-Driven Development (TDD) Readings: OOSC2 Chapter 11 EECS3311: Software Design Fall 2017 CHEN-WEI WANG Terminology: Contract, Client, Supplier A supplier implements/provides

More information