1.4 Abstract Data Types, Interfaces, and Pre- and Postconditions

Slides:



Advertisements
Similar presentations
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Advertisements

Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Introduction to Software Design Chapter 1. Chapter 1: Introduction to Software Design2 Chapter Objectives To become familiar with the software challenge.
IT PUTS THE ++ IN C++ Object Oriented Programming.
Review of C++ Programming Part II Sheng-Fang Huang.
Case Study - Fractions Timothy Budd Oregon State University.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Operator Overloading Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
C++ Lesson 1.
Introduction to Software Design
Chapter Topics The Basics of a C++ Program Data Types
CS1220 Midterm Review.
Objects as a programming concept
Abstract Data Types Programmer-created data types that specify
Basic Elements of C++.
Review: Two Programming Paradigms
C++ Classes & Object Oriented Programming
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Introduction to Classes
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Friday, January 26, 2018 Announcements… For Today… For Next Time…
– Introduction to Object Technology
Basic Elements of C++ Chapter 2.
Lecture 4-7 Classes and Objects
Lab 02 - SNAP.
10 – Iterators C++ Templates 4.6 The Iterator pgs
Object Based Programming
Introduction to Classes
3.3 Abstract Classes, Assignment, and Casting in a Hierarchy
Chapter 3 Introduction to Classes, Objects Methods and Strings
Lab 03 - Iterator.
Defining Classes and Methods
Data Abstraction: The Walls
Introduction to Classes and Objects
Defining Classes and Methods
CISC/CMPE320 - Prof. McLeod
C++ Templates L03 - Iterator 10 – Iterator.
1.4 Abstract Data Types, Interfaces, and Pre- and Postconditions
Defining Classes and Methods
CS 144 Advanced C++ Programming February 21 Class Meeting
Introduction to Classes and Objects
Presentation transcript:

1.4 Abstract Data Types, Interfaces, and Pre- and Postconditions 1.3 Defining C++ Classes 1.4 Abstract Data Types, Interfaces, and Pre- and Postconditions 07 – Software Design

Attendance Quiz #6 Software Design

Tip #7: The Rule of Seven Software Design There is a rule of nature commonly known as the 'rule of seven'. This appears in grouping situations, where control is optimized around the seven mark. It appears in a wide number of situations, from the grouping of leaves on plants, to the number of layers in the OSI network model, to the number of layers of management in the Catholic church, to the number of chunks of information that the short-term memory can comfortably handle. A simple application of the rule of seven in programming is where there might be seven chunks of code in a function, each containing seven statements, thus giving a reasonable function size of around 49 lines. The actual grouping count in many situations is actually rather inexact, and an extension to this rule is 'seven, plus or minus two'. Thus, if the size of a group is steadily increasing, when it gets to ten it can be broken into two independent groups of five, each of which now fits back into the bottom end of the five to nine range. Applying this rule of seven extension to the reasonable function size example gives a function size range of between 25 and 100 lines.

1.3, pgs. 75-97 1.3 Defining C++ Classes Class Definition The public and private Parts Class Implementation Using Class Clock The Class Person Constructors Modifier and Accessor Member Functions Operators Friends Implementing the Person Class An Application That Uses Class Person Classes as Components of Other Classes Array Data Fields Documentation Style for Classes and Functions 1.3, pgs. 75-97

Classes A class is a data type and a pattern for creating objects. Software Design A class is a data type and a pattern for creating objects. #ifndef PERSON_H #define PERSON_H #include <string> using std::string; class Person { private: string name; int age; public: Person(string, int); string getName() const; int getAge() const; string toString() const; }; #endif // PERSON_H #include <iostream> #include <string> #include <sstream> #include "person.h" using namespace std; Person::Person(string name, int age) { this->name = name; this->age = age; } string Person::getName() const { return name; } int Person::getAge() const { return age; } string Person::toString() const ostringstream out; out << name << "(" << age << ")"; return out.str(); A class definition describes the names of the class operations and the internal data contents (the data members). instance variables - each class instance (object) has its own storage, usually private to hide implementation details. Member functions (usually public) operate on any object of the class of which it is a member and have access to all the data members of the class object. A class implementation provides the implementations of the operations (the member functions).

Class Access Specifiers Software Design The private access specifier limits access of class members to the class definition. Private data fields Control access to an object's data. Prevent improper use and processing of an object's data. Allow a change of the private data representation without effecting programs that use the class. A friend function of a class has the same access privileges to private and protected data as the class member functions. A friend function is declared by the class that is granting access, so friend functions are part of the class interface, like methods. Since private members of a class are only accessible from within the class, a friend function must also be declared inside of a class, but may be implemented outside of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

Friends Software Design #include <iostream> #include <string> using namespace std; class MyClass { private: string name; public: MyClass(const string name) { this->name = name; } ~MyClass() = default; friend std::ostream& operator<< (ostream& os, const MyClass& me) os << "MyClass(" << me.name << ")"; return os; } }; int main(int argc, char* argv[]) MyClass me("Suzy Student"); cout << me << endl; return 0; Each member function has an implicit parameter named this whose value is a pointer to the object with which the member function was called. Defines an independent insertion operator function that is a friend of MyClass. The friend insertion operator function has access to MyClass’ private data members. Returns a reference to the output stream that has been modified.

UML Diagrams UML (Unified Modeling Language) classes: Software Design UML (Unified Modeling Language) classes: class Person { private: string given_name; string family_name; string ID_number; int birth_year; public: int age(); bool can_vote(); bool is_senior(); } Class Person has three string and one int data fields. The Person – string relationship (has-a) is represent by the solid diamonds in the following UML diagram:

Unified Modeling Language Software Design Name Airplane -type:string +Airplane() +setType(type:string):void #getType():string Private Public Protected Is-a Jetplane -engine:JetEngine +Jetplane(type:string,engines:int) +getNumberOfEngines():int JetEngine -number:int +JetEngine(number:int) +getNumber():int Has-a

Draw a UML representation of the classes to the left. class Animal { private: string name; public: Animal() {} void setName(string name) { this->name = name; } string getName() { return name; } }; class License int number; License(int n) : number(n) {} int getNumber() { return number; } class Dog : public Animal License license; Dog(string n, int l) : license(l) { setName(n); } int getLicense() { return license.getNumber(); } int main() Dog* dog = new Dog("Rover", 9999); cout << dog->getName() << " " << dog->getLicense(); delete dog; return 0; }

Draw a UML representation of the classes to the left. class Animal { private: string name; public: Animal() {} void setName(string name) { this->name = name; } string getName() { return name; } }; class License int number; License(int n) : number(n) {} int getNumber() { return number; } class Dog : public Animal License license; Dog(string n, int l) : license(l) { setName(n); } int getLicense() { return license.getNumber(); } int main() Dog* dog = new Dog("Rover", 9999); cout << dog->getName() << " " << dog->getLicense(); delete dog; return 0; } Animal -name:string +Animal() +setName(name:string):void +getName():string Dog -license:License +Dog(name:string,license:int) +getLicense():int License -number:int +License(number:int) +getNumber():int

Class/Function Documentation Software Design Documentation comments precede classes, member functions, and data members in a standard format. Originally was developed for the Java language. DOC++ (http://docpp.sourceforge.net/manual/) and Doxygen Enclose function descriptions within /** and */. Tags begin with the symbol @. Use one @param tag for each function parameter. Do not use a @return tag for void functions. /** Encodes a single digit of a POSTNET "A" bar code. @param digit the single digit to encode. @return a bar code of the digit using "|" as the long bar and "," as the half bar. */ string encode(int digit); The first line of the delimitated comment appears in the function summary part of the HTML page.

DOC++ Example class Intermediate Inheritance: Public Methods Software Design /** Just to make the class graph look more interesting. Here we show multiple inheritance from one docified class and a nondocified one. This is how this documentation has been generated: * / class Intermediate : public CommonBase, public NotDocified { … }; class Intermediate Just to make the class graph look more interesting. Inheritance: Inherited from CommonBase: Public Methods const Derived_Class& getB(constIntermediate& c) const Protected Fields double variable

1.4 Abstract Data Types, Interfaces, and Pre- and Postconditions Abstract Data Types (ADTs) and Interfaces An ADT for a Telephone Directory Class Contracts and ADTs Preconditions and Postconditions 1.4, pgs 98-101

Abstract Data Type (ADT) Software Design An Abstract Data Type uses encapsulation of data and methods to hide implementation details. Allows for reusable code. The user doesn't know about implementation details. The public part of a class definition defines the interface for the ADT class. Does not implement the class. Does specify the operations performed and/or how the data is represented. ADT's often can be implemented in more than one way. Each class that implements an ADT must provide the complete definition (implementation) of all operations declared in the ADT. Examples: vectors, linked lists, stacks, etc.

Pre-conditions and Post-conditions Software Design A contract between a function and the function programmer. A pre-condition is a statement of any assumptions or constraints on the function's data (input parameters) that are expected to apply before the function is performed. If a pre-condition is not met, there is no guarantee that the function will do what is expected. A post-condition is a statement that describes the results of the function execution. If pre-conditions are met and post-conditions are not, then the function is considered faulty. When implementing an ADT, we document the preconditions and postconditions in the documentation comments. Post-conditions for operations are comments with a void return type that reflect changes to the object's state. If an operation returns a value, you describe the postcondition in the @return tag.